Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ can't delete char*, keeps corrupting heap
    primarykey
    data
    text
    <p>Language: C++, Compiler: MSVS (/Za) and g++ (yes, it must work on both), Level: beginner</p> <p>I'm attempting to delete the data from a char* so that I can reallocate it and continue my program. I create a new char*, assign the value of the command line parameter to it, do some validation, then if the validation fails it should deallocate the char* and let me assign new data to the var however I'm getting a "Heap corruption detected" error in visual studio. I'm curious about both the fix for my current code and any other ways this could be done more clearly/concisely.</p> <p>In main():</p> <pre><code>//... //make non constant versions to be messed with char* ba = new char[ strlen(argv[4]) + 1 ]; char* num = new char[ strlen(argv[2]) + 1 ]; //make non constant versions of commandline stuff nonConstant( argv[4], argv[2], ba, num ); //do the conversion and printing convert( ba, num ); //... </code></pre> <p>convert does this:</p> <pre><code>//... if( error ) { getNew(ba, num, &amp;error); } //... </code></pre> <p>and here is getNew:</p> <pre><code>void getNew( char* ba, char* num, bool error ) { //if there's an error in their numbers let them input new stuff and check to see if that's valid while( error ) { //tell the user that the input was bad an prompt for more, use getline because strings are weird //cin stuff here (this part works, no problems) //free up base and num so I can reassign them delete[] ba; //&lt;--this line fails delete[] num; //set lengths = to lengths of input + 1 for \0 ba = new char[ inputBa.length() + 1 ]; num = new char[ inputNum.length() + 1 ]; //do the assignment of new input back to base and num inputBa.copy( ba, inputBa.length(), 0 ); inputNum.copy( num, inputNum.length(), 0 ); //ensure that the input was good this time validateInput( ba, num, error ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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