Jump to content
  • Advertisement
Sign in to follow this  
CyberSlag5k

Dynamic string array problems

This topic is 4886 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

I'm trying to store a list of the names of my textures as I load each texture, but for some reason I'm having problem with the array. The idea is I pass my texture loading function the file name, a pointer to the existing list of texture IDs, the number of textures added so far, and a pointer to the existing list of texture names (stored as strings). Each time the function is called, the first thing it does is call realloc on both the list of texture IDs and the list of texture names, such that the array holds one more element. Next, it is supposed to copy the name of the file into the new space created. It doesn't seem to like that part, though.
bool loadTexture(char* file_name, GLuint* &texture_list, int &num_textures, string* &texture_names)
{
	texture_list = (GLuint*)realloc(texture_list, sizeof(GLuint) * (num_textures + 1));
	texture_names = (string*)realloc(texture_names, sizeof(string) * (num_textures + 1));
	texture_names[num_textures] = "hello";

This is, of course, only part of the function, it does alot more (including incrementing num_textures). It crashes on "hello" assignment line, though, saying: Unhandled exception at 0x1023b425 (msvcr70d.dll) in SpaceSim.exe: 0xC0000005: Access violation reading location 0xbaadf001. Ironic that the address of the location comes out to say "baad". Anyway, this happens on the first call to the loadTexture function, where num_textures is initially zero and texture_names is NULL (thus realloc should act just like malloc, or so says MSDN). I can't seem to figure out what's wrong. The realloc code seems ok. Can anyone give me some ideas on how to flush out the problem? Or perhaps Drew_Benton or EvilSteve would like to say something like "Well it's quite simple. Your problem is...". In fact, I think I'd like that better, though I encourage everyone to beat drew or steve to the punch. boolean, perhaps? One of the mods? Or even an AP who's just surfing the site for the first time and sees the simple error in my code (in that case welcome to GameDev!). All suppliers of miraculous solutions are welcome! Thanks in advance, guys!

Share this post


Link to post
Share on other sites
Advertisement
If your using C then ignore this post.

I think the problem is your using string and realloc together. realloc will copy the memory to another place if theres not enough room at the current address. This may make any pointers inside a C++ object invalid. Also the constructor of string isn't being called and I think this is the problem your having. Use std::vector, char*, or placement new.

Share this post


Link to post
Share on other sites
I don't know how your string object should behave, but from what I see, you're increasing the size of an array of pointers... but you seem to fail to allocate memory for the string itself. Now I guess, depending on the compiler your "hello" string should be stored somewhere global as a const char* and reused where necessary...

What would be helpful is to know what your string object is?

If its a char* hidden in a typedef, why don't you strcpy to it?

Anyway, I hope someone will be able to help you, or post more code, I might be able to help out

Good luck

Eric

Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
realloc will copy the memory to another place if theres not enough room at the current address. This may make any pointers inside a C++ object invalid. Also the constructor of string isn't being called and I think this is the problem your having.


Indeed, in C++ only POD-types can be used with C memory rountines directly, if you still wanted to do it that then you'll need placement new operator how-ever you must explicitly invoke destructor (for user-defined types like std::string) before deallocating memory:


#include <new> // placement new

using std::string;

//.....

new(texture_names + num_textures) string("hello"); // placement new, constructs only

//.....

texture_names[num_textures].~string(); // explicit destruction, you must!

//.....



You'll be off better using std::vector as its guaranteed to store elements contiguously and takes care of all the low-level stuff like the above. You can allocate a chunk unitialized memory (using reserve) and incrementally initialize elements later on (using say push_back). You can even use a different allocator type with the standard library containers (even for std::string which is just a type alias for std::basic_string).

Share this post


Link to post
Share on other sites
Quote:
Original post by xEricx
I don't know how your string object should behave, but from what I see, you're increasing the size of an array of pointers... but you seem to fail to allocate memory for the string itself.


He doesn't have an array of pointers, he has a C-style dynamic array of std::strings, but only allocates a chunk of unitialized memory using realloc, each elements needs to be constructed which you can do this via in-place construction using placement new operator.

Share this post


Link to post
Share on other sites
Thanks guys. That clears up why things are blowing up on me. I shall take the appropriate measures.

And just out of curioisity, what does POD stand for?

Share this post


Link to post
Share on other sites
POD means Plain Old Data.

Thanks snk for clearing that up... I never use #using for clarity, so for some reason string didn't trigger in my brain that it was a std::string... I guess I'm tired =)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!