Note that there are some explanatory texts on larger screens.

plurals
  1. POHeap corruption using strcat
    text
    copied!<p>One of my weaknesses is effectively using chars in C++ which is what I am trying to do right now. I have a player class in my game and within the player class, I create a playerCard object which displays various information. This works fine for a single instance of the player object (i.e. Player player) but when I attempt to push_back a player object in to a vector it all goes wrong.</p> <p>Basically, the program continues to run but the player doesn't render to the screen. When I quit the program, I then get a breakpoint error when main tries to return MSG. The comment about the breakpoint reads:</p> <pre><code> /* * If this ASSERT fails, a bad pointer has been passed in. It may be * totally bogus, or it may have been allocated from another heap. * The pointer MUST come from the 'local' heap. */ _ASSERTE(_CrtIsValidHeapPointer(pUserData)); </code></pre> <p>I have located the error to here </p> <pre><code> strcat(nameCard, nameChar); strcat(nameCard, genderChar); strcat(nameCard, ageChar); strcat(nameCard, cashHeldChar); strcat(nameCard, productWantedChar); </code></pre> <p>within the playerCard class because when I comment this out, I do not get the error. Here is the full playerCard class (Again, it is messy and probably the wrong way for going about things but I am trying to get my head round using chars/strings etc) #include "Headers.h";</p> <pre><code>class Playercard{ private: RECT textbox; LPD3DXFONT font; std::string nameStr; std::string genderStr; std::string ageStr; std::string cashHeldStr; std::string prodWantedStr; char nameCard[1000]; public: Playercard() { } void load(char* name, bool male, int age, double cash, char* prod) { if(male) { genderStr = "Gender: Male\n"; } else { genderStr = "Gender: Female\n"; } nameStr = "Name: " + static_cast&lt;std::ostringstream*&gt;( &amp;(std::ostringstream() &lt;&lt; name))-&gt;str() + "\n"; ageStr = "Age: " + static_cast&lt;std::ostringstream*&gt;( &amp;(std::ostringstream() &lt;&lt; age))-&gt;str() + "\n"; cashHeldStr = "Cash Held: " + static_cast&lt;std::ostringstream*&gt;( &amp;(std::ostringstream() &lt;&lt; cash))-&gt;str() + "\n"; prodWantedStr = "Product Wanted: " + static_cast&lt;std::ostringstream*&gt;( &amp;(std::ostringstream() &lt;&lt; prod))-&gt;str() + "\n"; char * nameChar = new char [nameStr.length()+1]; char * genderChar = new char [genderStr.length()+1]; char * ageChar = new char [ageStr.length()+1]; char * cashHeldChar = new char [cashHeldStr.length()+1]; char * productWantedChar = new char [prodWantedStr.length()+1]; strcpy(nameChar, nameStr.c_str()); strcpy(genderChar, genderStr.c_str()); strcpy(ageChar, ageStr.c_str()); strcpy(cashHeldChar, cashHeldStr.c_str()); strcpy(productWantedChar, prodWantedStr.c_str()); strcat(nameCard, nameChar); strcat(nameCard, genderChar); strcat(nameCard, ageChar); strcat(nameCard, cashHeldChar); strcat(nameCard, productWantedChar); diagFile.open("Diag.txt"); diagFile.write("Test", 100); diagFile.close(); } void setUp(int L, int T, int R, int B) { SetRect(&amp;textbox, L,T,R,B); } void draw() { font-&gt;DrawTextA(d3dSprite, nameCard, -1, &amp;textbox, DT_LEFT, D3DCOLOR_XRGB(255, 255, 255)); } LPCSTR plCard() { return nameCard; } }; </code></pre> <p>Any help would be greatly appreciated. Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload