![]() |
Stash returns... Malloc problem?
I am expanding my stash class, and I am stuck at a problem I'm having... This is my stash.h code:
:
In main(), I am giving the following code: :
int main(){:
[Session started at 2006-05-26 17:56:43 +0300.]2)Please if you have recommendations about my code, or any other mistakes I have done, please tell me so, I want to make this class as efficient as I can. Thank you in advance. |
Look at it this way: suppose I get a pointer to dynamic memory. Then I copy that pointer to another. Then I free the first. Then I attempt to free the second (which, as a copy of the first has already been freed).
|
Ah side problem, DON'T put definitions in header files. Don't let them use namespaces either.
@David, is grumpy considering putting an article on 'C++ namespaces' in your contributor's corner? |
Quote:
Since the functions in the class do not copy the pointers, but the contents of the arrays indicated by the pointers, how does the deallocation of the 'o' object's 'ch' variable affect the 'b' object's 'ch' variable? I thought that they were a different thing. To be more clear, I thought this thing happens: When a new stash is created, it has it's own unique 'ch' pointer variable in it. So, when I use the 'insertstring' function (which uses the 'add' function) which is used in the '=' overloaded operator, as far as I can see in my code, only the contents of the pointers are copied, and not the pointers themselves. TheDark told me this would happen, but it seems I don't really know how to fix it. I am cearly missing something important here. Could you provide me with more information, or at least give me a code example? |
I think you just have to look at it like this: you may have two distinct variables, but they have the same content (a pointer). You can delete both variables (if they're dynamic), but deleting the thangy pointed to by the pointer can only happen once. Suppose now you have a variable with a pointer to dynamic memory. When you copy that, you make a new variable, you make a new pointer to different dynamic memory, you duplicate the contents of the first dynamic memory into the second. A true, deep copy, not a copy of a reference. Then you can delete either, or both.
@Ruben. There are a couple things I'm going to approach Grumpy about when I have my basic article publisher-formatter-swiss army knife thangy done. One would be the namespaces thing and one would be the unspecified/undefined behavior thing. I need to finish my part first, though, because otherwise the author's material becomes virtually unmaintainable. I also need to solidify my perception of what constitutes a separate article and what constitutes a piece of a FAQ/Gotchas-for-Newbies piece. |
Quote:
:
#ifndef STASH_HQuote:
Dawei, could you (or anyone else) provide me with a simple code example of what should I do (not the whole code) because I really don't seem to be able to fix this annoying thing. |
This is why you need a copy constructor. Let me sort of lay it out, and if that doesn't work, I'll write some code. Say you have a class with a variable that holds a reference to memory you have gotten from 'new'. If you just copy the contents of that class you have a different variable, but its contents are the same pointer to the same new'ed material. You cannot free the memory referenced by that pointer but once. Suppose now you write a copy constructor. You have a new class and it has a variable just like the old one. You do not copy the contents of that variable, because you just get the same reference. Instead of doing that, you do exactly what you did for the original: you get a fresh set of bytes from 'new'. Then you copy what was referenced by the old variable into the new memory, and put the pointer to that in the new variable, instead of a copy of the old pointer. I'm afraid I'm not being clear. Look at these images representing the original, a shallow copy, and a deep copy. I apologize for the huge images, they didn't reduce well. (Bite me, Ruben.)
http://www.daweidesigns.com/images/class.gif ----------------------- http://www.daweidesigns.com/images/shallow.gif ----------------------- http://www.daweidesigns.com/images/deepcopy.gif |
Dawei, thank you VERY much. I solved the problem. Here is my code:
:
#ifndef STASH_HIf you have noticed anything else that needs fixing or optimization, say it! :). Please check the '=' overloaded operators, and the copy constructor, because I may have missed a detail there, although they seem to work fine... |
Like I said, use header files correctly.
I'd also use C++ strings, but I don't know if you are prohibited to do that or anything. C++ strings have their own memory management, which is ideal. main.cpp: :
#include "stash.h"stash.cpp :
#include "stash.h"stash.h :
#ifndef STASH_H |
A side question: I see Soulstorm doing:
:
void stash::clearStash(){So then the following would function correctly: :
int main() |
| All times are GMT -5. The time now is 3:14 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC