• Advertisement
Sign in to follow this  

std string bad ptr behaviour

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

basically in my program a std::string is not behaving like a string should and by that i mean that objMesh->ObjectName = "word"; breaks with the error Unhandled exception at 0x73a7d57c (msvcr80d.dll) in main.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd. the app breaks in iosfwd which is a locked system file. I am using MS VS C++ 2005 static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right) { // assign an element _Left = _Right; } SO the only think i can reduce it to is that objMesh is allocated like so objMesh = (OBJECTMESH*)malloc( sizeof(OBJECTMESH) ); and then i spend the next lines assigning each memeber of object mesh, but when i come to the string, at runtime it breaks. Some insight as to why a string is not working correctly please.

Share this post


Link to post
Share on other sites
Advertisement
Don't use malloc to allocate class types. Use new. Otherwise the constructor won't be called and all hell will break loose when you try using the members.

Share this post


Link to post
Share on other sites
Quote:
Original post by ashstampede
the constructor for the object or its members? if for the object Its a structure that I assign memeber stuff manually. I am having to use allocate because i resize pointer arrays later on.

When i had new, the resize didnt work.

Scratch that,


The constructor of the object calls the constructor for the members. If you need a dynamic array try using std::vector.

Share this post


Link to post
Share on other sites
I figured that was the reason, so my problem is bigger than this question, will repost the larger problem at hand.

Share this post


Link to post
Share on other sites
yeah I went the vector root and that didnt work at all, well the algorithm didnt work in a Vector situation, I originally set out to change some C model loading code to c++. so went with c++ pointer arrays declared with new, when i came to resizing the it corrupted the heap. So i tried vectors and that was a no go simply based on the algorith used in the original.

Share this post


Link to post
Share on other sites
Quote:
Original post by ashstampede

objMesh->ObjectName = "word";



Looks like your objMesh is not properly constructed.

Share this post


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

  • Advertisement