Malloc a struct

Reply

Join Date: Oct 2008
Posts: 5
Reputation: dusse is an unknown quantity at this point 
Solved Threads: 0
dusse dusse is offline Offline
Newbie Poster

Malloc a struct

 
0
  #1
Nov 21st, 2008
Hello guys..
I´m trying to do something like the code below ..
  1. struct node{
  2. int a;
  3. char *s;
  4. };
  5.  
  6. void main()
  7. {
  8. node *x;
  9. int sMAX = 3;
  10. int nodeMAX = 10;
  11.  
  12. x = (node*)malloc((sizeof(int)+sizeof(char)*sMAX)*nodeMAX);
  13. ...
  14. }
in other words I want to alloc dynamically the number of nodes and also the length of s in node struct
I tried a lot of things like:
  1. x = (node*)malloc((sizeof(node)*nodeMAX);
  2. x = (node*)malloc((sizeof(struct node)*nodeMAX);
  3. x = (node*)malloc((sizeof(node)+sizeof(char)*sMAX)*nodeMAX);
and my code do not run
Is it possible to do what I want?
thanks.
Last edited by Narue; Nov 21st, 2008 at 11:20 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Malloc a struct

 
0
  #2
Nov 21st, 2008
>I want to alloc dynamically the number of nodes and also the length of s in node struct
Do it separately:
  1. struct node *x = malloc ( nodeMAX * sizeof *x );
  2. int i;
  3.  
  4. for ( i = 0; i < nodeMAX; i++ )
  5. x->s = malloc ( sMAX );
>Is it possible to do what I want?
In C89, not without invoking undefined behavior. In C99 the "struct hack" has been blessed and is directly supported as a flexible array member:
  1. struct node{
  2. int a;
  3. char s[];
  4. };
  5.  
  6. struct node *x = malloc ( nodeMAX * ( sizeof *x + sMAX ) );
Last edited by Narue; Nov 21st, 2008 at 1:32 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Malloc a struct

 
0
  #3
Nov 21st, 2008
Also, in C, shouldn't node *x; be struct node *x; ?

Another note - main returns int.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Malloc a struct

 
0
  #4
Nov 21st, 2008
>Also, in C, shouldn't node *x; be struct node *x; ?
Indeed. I've been working in mixed C/C++ too much these days.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC