943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12443
  • C++ RSS
Nov 29th, 2005
0

Memory allocation for typedef struct

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desertstorm is offline Offline
12 posts
since Sep 2005
Nov 29th, 2005
0

Re: Memory allocation for typedef struct

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 29th, 2005
0

Re: Memory allocation for typedef struct

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mistro116 is offline Offline
8 posts
since Nov 2005
Nov 29th, 2005
0

Re: Memory allocation for typedef struct

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desertstorm is offline Offline
12 posts
since Sep 2005
Nov 29th, 2005
0

Re: Memory allocation for typedef struct

C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. node[5].key = key;
Quote ...
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.
C++ Syntax (Toggle Plain Text)
  1. eg. void *insert(void *key, long keysize)
  2. {
  3. node[5].key = malloc(keysize);
  4. memcpy(node[5].key,key,keysize);
  5. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Separating socket code from GUI code
Next Thread in C++ Forum Timeline: please some one check this code and help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC