[C++] Question about dynamic memory assignation to a pointer of a pointer

Started by
4 comments, last by chbrules 18 years, 4 months ago
Maybe the title is a bit confusing. What I have is a struct typedef called sMap. I make a pointer of that datatype:
sMap *prgMap;
So later on I can create an array when I read my data. I have a seperate function outside the class where I define it (In my file loading class), so I have to pass the value to it, like so:
int LoadMap(sMap *prgMap){...}
So inside that function I read the size that I need to allocate from the file, then proceed to do as such:
prgMap = new sMap[(Header.size)];
Now my program compiles just fine, yet when I run it, I get a run-time crash. My debugger somehow isn't... well, debugging anymore. Any time I debug something I get a message about how there is no code here, do you want to view the disassembly? And it just worked the other day properly. That's a seperate issue, maybe someone could help me on that too. At any rate, I'm wondering that maybe this method isn't being allocated properly due to me passing a pointer to a function and the function not allocating that original pointer the appropriate value? Thanks in advanced! P.S. I'm switching over from a vector<> system to save overhead and whatnot, so it worked perfectly before this.
-Conrad
Advertisement
Try passing in a double pointer like so:

int LoadMap(sMap **prgMap){   ...   *prgMap = new sMap[(Header.size)]}


Then you can call it like this:

sMap *ptr = NULL;LoadMap(&ptr);//check that it workedassert(ptr != NULL);

Quote:Original post by chbrules
Maybe the title is a bit confusing. What I have is a struct typedef called sMap.

You do realise that such typedefs are only useful for C compatability (or rather C ease of use compatability), right? In C++ structs can be referred to by their names without needing to use the struct keyword, i.e.:
struct MyStruct{};// C version// struct MyStruct myStruct;// C++ versionMyStruct myStruct;
Quote:At any rate, I'm wondering that maybe this method isn't being allocated properly due to me passing a pointer to a function and the function not allocating that original pointer the appropriate value? Thanks in advanced!

Yes, you're copying the value of the pointer and modifying the copy. You would need to pass prgMap by reference (sMap * &) if this was really what you wanted to do.

Quote:P.S. I'm switching over from a vector<> system to save overhead and whatnot, so it worked perfectly before this.

What overhead? Of course, you used a profiler with a release build to determine that std::vector was your bottleneck before trying to optimise it, right? (Hint: std::vector has only one area that could even possibly qualify as significant overhead, and that can be trivially worked around via reserve.) Just use std::vector.

Enigma
I have it as a typedef since I use it throughout my program. Can you call a globally defined struct anywhere in the program to use for objects? This I've never heard of, hence why I globally used a typedef.

I've always heard that vectors had to deal with a lot more processing to handle the elements of the vector. I guess using the [] member operator would work just as well for the vector as it does arrays. I'm not a professional, just trying to speed up my game engine any way possible. Thank you.
-Conrad
In debug mode, the vector may be a lot slower. In release mode, all the training wheels are off and the vector performs as quickly as an array, when doing array-like things - because it effectively *is* one. The object does add one level of indirection to the array, but the optimizer can easily lift that dereference out of loops, etc. such that the cost is effectively nil.

However, you would still need to pass a std::vector by non-const reference in order for changes to it to be "seen" by the calling function.
Thank you, Zahlman, I hadn't realized that. I learn something new every day. :)
-Conrad

This topic is closed to new replies.

Advertisement