Memory Allocation

Started by
14 comments, last by Khatharr 7 years, 12 months ago

sizeof(struct Node *) <- isn't this wrong??

for a node you should allocate sizeof(struct node) otherwise you allocate only pointer size space (4 or 8 bytes?)

Yes, this is the problem, you should be calling malloc(sizeof(struct Node)) .

However, you can make life easier using new and delete, like this

// in push()
Node* temp = new Node;
 
// later, in pop()
delete temp;

And, I just noticed, in pop(), why are you calling malloc() on temp again, then immediately assigning temp to headNode? Don't malloc temp in pop().

You need to understand memory allocation a little more, then you should be able to tackle this problem fine.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Advertisement
sizeof(Node)

Don't repeat the struct keyword everywhere or I will hunt you down and take all the vowels from your keyboard.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

return NULL;

You're right, that is an issue. However, I dont think thats it will solve this particular issue with my program though because when I call pop(), I make sure the function is not empty so return NULL isnt ever called. I even tried changing return NULL to return -1 and the same issue happened. Everything is working properly with no crash up until I add the free(temp) statement in the pop() method. If I uncomment that statement, it works perfect besides the fact that the memory leak isnt solved when popping.

Will node solve your issue but FYI, the correct solution in this case is quite simple:


template<typename T>
T returnDefault(void)
{
    return T();
}

This will return a specified default value for all types (int = 0, T* = nullptr, custom type = default-ctor, ...), so just replace return NULL by return T() and you should be fine in this regard.

sizeof(Node)

Don't repeat the struct keyword everywhere or I will hunt you down and take all the vowels from your keyboard.

lol


return NULL;

You're right, that is an issue. However, I dont think thats it will solve this particular issue with my program though because when I call pop(), I make sure the function is not empty so return NULL isnt ever called. I even tried changing return NULL to return -1 and the same issue happened. Everything is working properly with no crash up until I add the free(temp) statement in the pop() method. If I uncomment that statement, it works perfect besides the fact that the memory leak isnt solved when popping.

Will node solve your issue but FYI, the correct solution in this case is quite simple:


template<typename T>
T returnDefault(void)
{
    return T();
}

This will return a specified default value for all types (int = 0, T* = nullptr, custom type = default-ctor, ...), so just replace return NULL by return T() and you should be fine in this regard.

Thank you, I never learned this

sizeof(struct Node *) <- isn't this wrong??

for a node you should allocate sizeof(struct node) otherwise you allocate only pointer size space (4 or 8 bytes?)

Yes, this is the problem, you should be calling malloc(sizeof(struct Node)) .

However, you can make life easier using new and delete, like this


// in push()
Node* temp = new Node;
 
// later, in pop()
delete temp;

And, I just noticed, in pop(), why are you calling malloc() on temp again, then immediately assigning temp to headNode? Don't malloc temp in pop().

You need to understand memory allocation a little more, then you should be able to tackle this problem fine.

Yeah I def needed to learn about memory allocation more haha I looked up some tutorials and watched some video explanations and I think I am understanding it better now.

PS - Thank you to everyone that helped me out, I got it working now!

malloc works well with the term pod which is short for plain old data.

Here malloc reserve some amount of space and return it to you as a (void*). Why void* because malloc is from the days of c not c++. In those days we didn't have templates and casting was need to convert void* to something understandable. I see that you are mixing up c++ and c while it is possible, I would advise you to use the newer c++ concept instead of c, the reason is it makes coding easier to understand and work with.

Bring more Pain

The only time malloc should really be used in C++ is if you're writing a specialized allocator (like overloading new or something), lol. Even 'new' should be avoided in application code, but I think OP is still learning the bare basics, so 'new' is probably okay for now.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement