Memory allocation for typedef struct

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 12
Reputation: desertstorm is an unknown quantity at this point 
Solved Threads: 0
desertstorm desertstorm is offline Offline
Newbie Poster

Memory allocation for typedef struct

 
0
  #1
Nov 29th, 2005
Hello:
I have a question regarding struct when making hash buckets eg.
typedef struct _hashNode
{
void *key;
void *value;
struct _hashNode *next;
} hashNode;

let's say when I create the table:

hashNode *nodeList = (hashNode *)malloc(sizeof(hashNode) * 200);

This is because I want a fixed list size and then each of these array spots will be a linked list or "buckets" for inserting values downwards. (I hope you understand this) Anyways, if I were to insert values into lets say nodeList[5]->key, would I have to allocate memory to key

eg. nodeList[5]->key = (void *)malloc(sizeof(void) +1);

Or does that automatically do that when I allocate memory to the nodeList[5].

thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Memory allocation for typedef struct

 
0
  #2
Nov 29th, 2005
Originally Posted by desertstorm
Or does that automatically do that when I allocate memory to the nodeList[5].
No, you need to do that.

[edit]
Except that this won't go far: sizeof(void) -- you need a type to have a size of that type.
[/edit]

Why the casts on malloc? If you're using C++, why not new or a vector? If C, stop doing that and let your compiler help you write better code.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 8
Reputation: Mistro116 is an unknown quantity at this point 
Solved Threads: 0
Mistro116 Mistro116 is offline Offline
Newbie Poster

Re: Memory allocation for typedef struct

 
0
  #3
Nov 29th, 2005
The purpose of malloc is dynamically, key word, dynamically allocate memory from the preprocessor for a DATA structure.

In this case hashNode or whatever is your data structure, so you would be mallocing a pointer to an array of hashNode structures. Each of these hashNodes have the elements you specified under the structure definition. They each have a keys, and so forth, without further mallocing.

Now if you wanted a malloc'd array of hashNodes and a malloc array of keys, that would be a little difficult to handle with, but I guess it could be possible, althought it is definately poor code.

malloc is a data structure preprocessor function, keep this in mind. It will not change or modify the elements in the data structure, it simply allocates memory.

Hope this helps,

Mistro116
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 12
Reputation: desertstorm is an unknown quantity at this point 
Solved Threads: 0
desertstorm desertstorm is offline Offline
Newbie Poster

Re: Memory allocation for typedef struct

 
0
  #4
Nov 29th, 2005
The thing is I am setting node[5]->key for example to another pointer eg. void *key. So something like this: node[5]->key = key. I am setting the pointer in the typedef struct to the pointer void *key which is an argument/parameter in the function header.

eg. void *insert(void *key)
{
node[5]->key = key;
}

node is global.

If done this way I don't have to allocate memory to node[5]->key before setting it equal to key right?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,389
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Memory allocation for typedef struct

 
0
  #5
Nov 29th, 2005
  1. node[5]->key = key;
That is wrong syntax. you have to use dot notation, not pointer notation because node[5] is NOT a pointer.
  1. node[5].key = key;
If done this way I don't have to allocate memory to node[5]->key before setting it equal to key right?
Well, maybe yes and maybe no. How is paramater key being allocated? Will the calling function reuse that memory for something else, such as does it get overwritten each time the insert function is called or is new memory allocated each time?

I find it normally best to reallocate the memory so that node has complete control over it and not count on some other function allocating/deallocating node's memory.
  1. eg. void *insert(void *key, long keysize)
  2. {
  3. node[5].key = malloc(keysize);
  4. memcpy(node[5].key,key,keysize);
  5. }
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