| | |
Memory allocation for typedef struct
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2005
Posts: 12
Reputation:
Solved Threads: 0
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
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
•
•
•
•
Originally Posted by desertstorm
Or does that automatically do that when I allocate memory to the nodeList[5].
[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
•
•
Join Date: Nov 2005
Posts: 8
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2005
Posts: 12
Reputation:
Solved Threads: 0
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?
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?
C++ Syntax (Toggle Plain Text)
node[5]->key = key;
C++ Syntax (Toggle Plain Text)
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?
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)
eg. void *insert(void *key, long keysize) { node[5].key = malloc(keysize); memcpy(node[5].key,key,keysize); }
![]() |
Similar Threads
- why memory allocation (C)
- memory allocation ptr to array? how? (C)
- Why use Dynamic and Static Memory Allocation... (C)
- memory allocation (C)
- Dynamic memory allocation homework (C++)
- How to use alloc's memory allocation functions? (C)
Other Threads in the C++ Forum
- Previous Thread: Separating socket code from GUI code
- Next Thread: please some one check this code and help me
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






