0


In this question the usage of malloc for character arrays is explained in a detailed way.
When should I use malloc in C and when don't I?


Is this same for structures in C?
For example consider the following definition:

struct node 
{ 
  int x;
  struct node * link;
}
typedef struct node * NODE;

Consider the following two usage of the above structure:

1)

NODE temp = (NODE) malloc(sizeof(struct node));
temp->x =5;
temp->link = NULL;

2)

struct node node1, *temp;
node1.x = 5;
node1.link = NULL;
temp = &node1;

Can I use the declaration of temp from the second example and modify the node1.link point to another structure struct node node2 by using temp->link = &node2 (pointer to node2 structure)? Here, this implementation is used for creating a tree data structure.

Will the structures also follow the same rules as like arrays as stated in the above link? Because many implementations I have seen followed the first usage. Is there any specific reason for using malloc ?

Community
  • 1
  • 1
Hulk
  • 97
  • 3
  • 16
  • first don't cast returned address from malloc and calloc – Grijesh Chauhan Jan 03 '14 at 06:36
  • And that `typedef struct node * NODE;`.... – moeCake Jan 03 '14 at 06:37
  • @GrijeshChauhan For many examples I see for malloc, has a casting specified for the pointer returned from malloc. I thought it is the best practice. Can you please tell me when to use and when not use casting for malloc? – Hulk Jan 03 '14 at 06:56
  • Well I got the answer here it is: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/14879184#14879184 – Hulk Jan 03 '14 at 07:15

5 Answers5

0

the first usage use the heap memory which is almost as large as you phsical memory, but the second usage use the stack momory which is quite scare( normally 8M )

michaeltang
  • 2,850
  • 15
  • 18
0

If you allocate memory dynamically, you must deallocate that memory programatically by calling free().

Rahul
  • 3,479
  • 3
  • 16
  • 28
0

Yes. You can do "temp->link = &node2;", because temp points to node1.

Yes, this rule is very general in C, so it can work for arrays.

When malloc is used, the space is allocated in the heap rather than the stack. This allows you to allocate more space for large data structure. The downside is that you also need to "free" the memory after the usage. Otherwise, memory leak will occur.

wayi
  • 513
  • 1
  • 6
  • 14
0

You can do what you describe in #2, but remember that it will only work as long as node1 is in scope. If node1 is a local variable in a function, then when the function returns, the memory location it refers to will be reclaimed and used for something else. Any other pointers in your program which still point to &node1 will no longer be valid. One of the advantages of using malloc to allocate memory dynamically is that it remains valid until you call free to dispose of it explicitly.

Tim Pierce
  • 5,514
  • 1
  • 15
  • 31
0

In first you are allocating memory dynamically it will allocate the memory from heap. If you store the data in heap memory it will stay until you call free else it will memory will deallocate when program terminate.

your second one store the value in stack its scope is local to the function.

Your example seems that you are working on linked list so you may need to add or delete the node at any time, so using dynamic allocation is the best option.

sujin
  • 2,813
  • 2
  • 21
  • 33