Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer to pointer issues...

Started by Alekzanther Aug 2, 2006 at 12:45 PM 14 replies 1.6k views
Original Post
Alekzanther
Alekzanther
Using C with classes in VS 2003. I've made a little system where an array of pointers to chars (like: char *names[MAX_NAMES]) is used to allocate an unknown amount of names (less than MAX_NAMES) of unknown length. There are other pointers to pointers that share the same names from elsewhere in the program, so that it's easy to change a name and have the name "changed" anywhere it is used in the program. Like: char **ptp; //from somewhere else ptp = &names[ NameAskedFor ]; It all works well when running the application normally but when it's time to shut it down it crashes sometimes (and sometimes not) - but that's only when i've changed the names in the array of pointers during the run. When I change the names in the array, I delete the pointer to the name I want to change and assign it a new array of chars depending on the size of the new name in bytes and so forth - nothing strange really. Is there something I'm doing wrong when working this way? It crashes in the very end of the application. Also I'm using a singleton class that stores the array of pointers to chars and a dynamic amount of menus that uses these names through their own pointer to pointer. When the application is shuting down I call an uninitalize function for the singleton name-class (with no crashes there wether or not i've changed names during the run) and delete the menus as well without errors. The crash occurs at the very end of the application. Sorry for the long "question", but I just want to know if I should worry about the way I'm treating the pointers to the pointers/array of pointers or if I should aim to solve the problem elsewhere in the program? Is it screwing with the stack or something?
Zakwayda
Zakwayda
Based on what you describe, I'd be surprised if your app didn't crash :)

It's hard to say what the specific problem is, but the first thing that jumps out at me is that it sounds like you're re-allocating character strings when (possibly) other pointers are referencing that same character string. Remember that whenever you re-allocate, all pointers that reference that memory are invalidated.

Any reason you're using C with classes? Just curious - what you're doing would be much easier to code and much less error-prone in C++ (or any of several other languages, for that matter).

[Edit: Deyja is right - misread the post.]

[Edited by - jyk on August 2, 2006 5:53:15 PM]
LessBread
LessBread
Quote:
Original post by Alekzanther
There are other pointers to pointers that share the same names from elsewhere in the program, so that it's easy to change a name and have the name "changed" anywhere it is used in the program. Like:

char **ptp; //from somewhere else

ptp = &names[ NameAskedFor ];


Why do you need a pointer pointer to access a particular name?

Wouldn't a pointer to char be sufficient?

char *name = names[ NameAskedFor ];
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Deyja
Deyja
Na, he's holding pointers to pointers, so he can change the value of the nested pointer and everything holding the outer pointer 'magically' knows the new value.

I would suggest, as a first step, just comment-out the code that cleans out the names list at shutdown. I've found that shutdown bugs are usually caused by trying to clean up some resource twice, or some module A whos' destruction code uses bits of module B... except module B was destroyed first.
Alekzanther
Alekzanther
Thank you all for taking your time to read and answer my question.

Quote:
Original post by jyk
...
Any reason you're using C with classes? Just curious - what you're doing would be much easier to code and much less error-prone in C++ (or any of several other languages, for that matter).
...


It's really compiled as C++, I just skip namespaces and STL and so on. I read some post before on this forum where someone (in the same situation as me) got "yelled at" for not saying he didn't use STL and that he should've said he was basically using C with classes instead of C++. I just said I used C with classes so that I wouldn't get a lot of suggestions pointing towards the vector class or whatever.

Quote:
Original post by Deyja
...
I would suggest, as a first step, just comment-out the code that cleans out the names list at shutdown. I've found that shutdown bugs are usually caused by trying to clean up some resource twice, or some module A whos' destruction code uses bits of module B... except module B was destroyed first.


Yeah I thought so at first, but it didn't make any sense since there's really no difference between changing the names and not changing the names, I just reallocate the pointers one or more times more when changing the names during the run.

However, I found the problem when looking at the lua uninitialization code, so you were right although it had nothing to do with the pointer to pointer situation. There's some bug in lua_close apparently that sometimes deletes stuff that gets deleted by the app again on shutdown. Thanks Deyja.
snk_kid
snk_kid
Quote:
Original post by Alekzanther
I just skip namespaces and STL and so on.


Why would you want to do that? you want to increase the chances of name collisions and/or add pointless prefixes to every name?

I can understand wanting to use only a subset of C++ but that should be all the more reason to use the standard library.
Alekzanther
Alekzanther
Quote:
Original post by snk_kid
Quote:
Original post by Alekzanther
I just skip namespaces and STL and so on.


Why would you want to do that? you want to increase the chances of name collisions and/or add pointless prefixes to every name?

I can understand wanting to use only a subset of C++ but that should be all the more reason to use the standard library.

I don't use the standard library because I've heard (and experienced rather dramatically) that it is slow in it's places. And I, at least how I structure things, don't experience any name collisions or uses of pointless prefixes at all.

However, that's off-topic, so you should either start a new topic discussing it or PM me if you've got any questions aimed at me specifically. :) I don't mean to be rude or something, it's just that this topic is related to something else and I don't want something OT to escalate here.



Zahlman
Zahlman
Quote:
Original post by Alekzanther
I don't use the standard library because I've heard (and experienced rather dramatically) that it is slow in it's places. And I, at least how I structure things, don't experience any name collisions or uses of pointless prefixes at all.


So you're ignoring *core language features* and the language's *standard library*, and taking more work upon yourself, because you've "heard" that it's "slow". Never mind the time you spend making it work that you could be spending on optimizing your algorithms (which is where the big savings are likely to come in). Never mind that namespaces have nothing to do with that and can't possibly introduce any performance penalty, because all the variable name resolution has to be done at compile time. Never mind that passing by reference instead of by pointer can't be slower (because in the worst case it is just a form of pointer that is hidden from you) and could (though unlikely) be faster (because it gives the compiler more information)

Never mind that no sane programmer in any language other than C or C++ would ever consider that kind of ignorance (what is it about the C/C++ community anyway?).

As for what you're "doing wrong", we're not psychic; show some actual code.

Quote:
However, that's off-topic, so you should either start a new topic discussing it or PM me if you've got any questions aimed at me specifically. :) I don't mean to be rude or something, it's just that this topic is related to something else and I don't want something OT to escalate here.


No, it really isn't off-topic, assuming you want your code to, you know, work.
Palidine
Palidine
C++ fundimentalist arguments aside... (you obviously don't actually need the STL to get C++ apps working. IMHO it's easier/better to use it but whatever)

Couple things I could see going wrong:

1) You're writing over the bounds of your char* strings somewhere so that when you clean it up you crash with "damage after memory block" errors.

2) As deyja suggests, you're double deleting pointers. Your app will generally crash if you try to delete a pointer 2x. A potential bandaid is that when you free the memory in your char** array, set each pointer to NULL after you free the memory. calling delete(NULL) or free(NULL) is actually valid and will not crash.

I assume you are running in the debugger. On exactly what line does your app crash? post the code at and around that line number. It's crashing inside the delete/free operator, i know, but just walk up the stack till you get to your code and post that.

-me
Alekzanther
Alekzanther
Quote:
Original post by Zahlman
...
So you're ignoring *core language features* and the language's *standard library*, and taking more work upon yourself, because you've "heard" that it's "slow"
...

As I said, please take it elsewhere - oh, and please take you sarcasm there as well. Really not ignoring the whole core as that would not work (as you might've noticed if you read carefully); just namespaces and some of the slow classes. Calm down please.
Quote:
Original post by Zahlman
...
As for what you're "doing wrong", we're not psychic; show some actual code.
...

It's really a pretty general question about pointer to pointer issues, so if you've got no bad experience with the use of them, please keep your bad attitude elsewhere.

Quote:
Original post by Zahlman
...
No, it really isn't off-topic, assuming you want your code to, you know, work.

Surely when asking generally about issues that could arise from the situation described in the real question - that is general pointer to pointer operations and issues - it's really not a question about wether or not I'm using STL classes or namespaces. Please accept that I do not want to turn this into some crusade for or against those.

I don't know if you, Zahlman, feel more superior or something with that attitude of yours, but it's really not contributing to the discussion or the forum in a positive way. If you want to yell at me or be sarcastic at me for whatever reason just go ahead via PM. We discuss experiences with pointer to pointer situations and the like here, thanks beforehand.

Quote:
Original post by Palidine
...
Couple things I could see going wrong:
...

Thank you for your suggestions. I've already made sure those are not the issues. The problem has been solved already and it hasn't got anything to do with pointer to pointer issues as I first thought:
Quote:
Original post by Alekzanther
...
There's some bug in lua_close apparently that sometimes deletes stuff that gets deleted by the app again on shutdown.


It was kind of a double delete situation, although not pointer to pointer related as you see. There's no problem anymore then, thank you all - those of you who really wanted to contribute with your experiences given the info - for taking your time.
Deyja
Deyja
Quote:
There's some bug in lua_close apparently that sometimes deletes stuff that gets deleted by the app again on shutdown.
I doubt very much that it was a bug in LUA. I would suggest, instead, that LUA really was supposed to delete that stuff; and the documentation probably tells you so. If you (Or LUA, for that matter) used the simple C++ idiom of RAII, you would never have had this problem.

Quote:
don't use the standard library because I've heard (and experienced rather dramatically) that it is slow in it's places.
No you havn't. You used some container wrong; by, say, constantly removing from the middle of a vector; and assumed the whole library sucked just because you didn't know how to use it. This myth has existed since the standard was first created, and it's always been just that: A myth. Even the worst implementations ever are magnitudes faster than the naive implementations the average programmer will produce of the algorithms they employ. With anything modern, you simply cannot beat them. They are written by people with a working knowledge of the internals of the target compiler, and the compilers themselves know how to pull tricks with standard constructs that they won't even try with your home-brew code.
Alekzanther
Alekzanther
This is getting rather ridiculous.
Quote:
Original post by Deyja
Quote:
There's some bug in lua_close apparently that sometimes deletes stuff that gets deleted by the app again on shutdown.
I doubt very much that it was a bug in LUA. I would suggest, instead, that LUA really was supposed to delete that stuff; and the documentation probably tells you so. If you (Or LUA, for that matter) used the simple C++ idiom of RAII, you would never have had this problem.
...
Quote:
From Lua 5.1 Reference Manual
... void lua_close (lua_State *L);...
On several platforms, you may not need to call this function, because all resources are naturally released when the host program ends...
Sure thing I said and all problems dissappeared, no more "random" crashes on termination. No crashes at all for that matter. Case closed. RAII is great btw, if you make huge projects that require really good handling of dynamic stuff. Thanks for throwing it in my face that I'm a lesser being with no knowledge at all because I ask about a scenario with pointers to pointers. Sometimes a project isn't big enough for a wide use of RAII, but thanks anyway.

Quote:
Original post by Deyja
Quote:
don't use the standard library because I've heard (and experienced rather dramatically) that it is slow in it's places.
No you havn't...
My God... I don't care what you think of STL and non-STL users and why STL is so great and why Einstein used STL and why God is on STL:s side, can't you just see that? I'm not throwing shit at all of you because you like something that I'm not fond of. I bet it's great for you - but you can't judge me based on what you know (unless you're God) so please keep that stuff out of the discussion! Why don't we all from the West (you know, the superior ones) just get a little bit EXTRA judgemental - for the group feeling's sake of it - and ban muslims from our contries while we're at it? You know they're terrorists right? Sure you do, you live in the US. Oh WAIT that was judgemental of me! Shit, you're really influencing me positively here!

Start a new thread if you want a crusade. Thanks beforehand, again, for the third time or something. I'll not reply to any STL arguments or thoughts in this topic anymore, it's driving me nuts.
CrazyCdn
CrazyCdn
Quote:
This is getting rather ridiculous.


Yes you are. But it's ok, put your head back in the sand. Calm down, take your ritalin and relax. We know your container implementation is 2x+ faster then the STL. You know more then then most programmers about writing efficent containers for your compiler.

Now to the rest of you. Please, he's a container programming god, leave him be. His blinders are firmly in place and will not be removed.

Quote:
Start a new thread if you want a crusade. Thanks beforehand, again, for the third time or something. I'll not reply to any STL arguments or thoughts in this topic anymore, it's driving me nuts.


They say ignorance is bliss, this is the happiest person I've seen on these forums in a while now!

Quote:
My God... I don't care what you think of STL and non-STL users and why STL is so great and why Einstein...


Oh, nevermind... ;-)

I only posted because you told us not too. It's like telling a child not to touch something. I'll never grow up, and you cannot make me!

Edit: I should point out I was a late comer to the STL world for the reasons you think it's bad. Sadly, you're poorly informed. I ended up doing some real world tests with a friends engine and the speed ups over my "optomized" custom containers was about 400% using STL properly (key phrase here, properly).

[Edited by - Mike2343 on August 4, 2006 12:59:43 AM]
"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin
MaulingMonkey
MaulingMonkey
Zalhman's trying to help you by pointing out why the reasons you're avoiding the standard library are in fact really really bad ones.

Since any advice I could give would involve either writing better code using the standard library (which you seem unwilling to take advice with respect to), or debugging the specific code you currently have (which you havn't posted), I'm completely unable to help you at this point.

At least, without expending assine amounts of effort trying to be pyschic over what's most likely something rather simple. And I, at least, am not willing to make things harder for no good reason. (Yes, that's another jab at avoiding the STL).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.