Sign in to follow this  

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

This topic is 4396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 worked
assert(ptr != NULL);

Share this post


Link to post
Share on other sites
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++ version
MyStruct 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this