1

I am working on a project where I need to malloc some data. I am trying to reduce the heap peak of my program by changing around the main structure values I use. I am using a linked list. My struct is like:

struct myS{

int a,b;
float a,b;
struct myS *next;

};

I was thinking that instead of mallocing the struct pointer I would store it in a global array since I dont have much data. How would I do this ?

1
  • If you really don't have much data, don't do it. This sounds like premature optimization. If you have a lot of data, don't do it like that, but first read up on replacements for malloc and stuff like that. Commented Apr 14, 2012 at 6:59

2 Answers 2

1

If you have upper bound for the number of elements you are going to need, you can create a global array [not dynamically allocated], let it be struct myS heap[], and an integer idx, initialized to 0. Once you allocate an element, you will need to increase idx, and attach this element to the requester.

Note - it is a good solution only if you are not expecting to delete elements [or you can afford to allocate each element only once].
If you do need delete, you will need to allocate an extra array that tells you which elements are currently in use, make idx circular [increase with idx = (idx + 1) % size], and check if each element is allocated before giving it, but as I say - it will probably be more time consuming!

code snap [not supporting deletes]:

struct myS heap[SIZE];
int idx = 0;

...

struct myS* allocate() {
  return &(heap[idx++]);
}

Note: The above code snap is dangerous - it might overflow if you try to allocate more elements then you have in SIZE.

Sign up to request clarification or add additional context in comments.

2 Comments

If you maintain a list of free nodes within the array, initially consisting of the entire array, you don't need to do a traversal to find an unallocated node.
@KeithThompson: thanks, that indeed can be a good optimization! But I believe the OP is actually after the simpler version of allocating each element only once, I'll edit and try to add the solution you are describing if he'll give an indication that this is indeed what he is after..
0

You could do something like this:

struct myS myArray[ARRAY_SIZE];

/* ... */

struct myS *head = &myArray[0];

head->next = &myArray[1];
head->next->next = &myArray[2];

/* etc... */

The array indexes used doesn't have to be sequential, e.g. head can be index 3 and head->next can be index 21

If you want to initialize the list to use all entries in the array at once, you could do it in a loop:

struct myS *node = head;
for (int i = 1; i < ARRAY_SIZE; i++)
{
    node->next = &myArray[i];
    node = &myArray[i];
}
node->next = NULL;  /* Make sure the tail of the list doesn't have a 'next' pointer */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.