I get confused why you use dynamic allocation and the concept of allocating enough memory for the data. So we are covering linked lists in my class and there is this code:
NODE *BuildTree(NODE *p, const char *str)
{
if (p == NULL)
size_t length = strlen(str) + 1;
p = (NODE *)malloc(sizeof(NODE));
p->string = (char *)malloc(length);
....
}
typdef struct Node NODE;
struct NODE
{
char *string;
size_t count;
NODE *left, *right;
};
The code is supposed to create a tree with strings that is entered from the user. I am assuming you dynamically allocate the NODE because you obviously do not know how many children you are going to need. But then, why do you need to alocate the size for the string str that is goign to be entered? Or is that done because of the way the struct is defined and you want to allocate the exact amt, vs if the struct said size_t count[50], and just had wasted memory. Is that the idea? Thanks!