CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    5

    Debugging C++ code(Displaying char string per character)

    I've tried a bunch of alternative methods to prevent an assertion error. "not understanding" the bug and why I'm getting it are relevant here, not proper, (or more appropriate), coding methods. I would write it in another way to prevent the error, I simply want to understand what is happening during run-time that causes the situation.

    What is the bug?
    ---------------------------------------
    [assertion error]
    [expression _block_type_is_valid(phead->nBlockUse)]
    --------------------------------

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	cout << "Enter your name : ";
    	string Name;
    	cin >> Name;
    	// Add 1 to reserve space for a terminating null
    	int CharsToAllocate = Name.length() + 1;
    	// request for memory to hold copy of input
    	char* CopyOfName = new char[CharsToAllocate];
    	// strcpy copies from a null-terminated string
    	strcpy(CopyOfName, Name.c_str());
    	// Display the copied string
    	cout << "Dynamically allocated buffer contains : " << endl;
    
    	for (int placeinline = 1; placeinline <= 6; ++placeinline)
    	{
    		if (placeinline == 6)
    		{
    			cout << *CopyOfName << endl;
    			goto terminate;
    		}
    		else
    			cout << *CopyOfName << endl;
    			*CopyOfName++;
    					
    	}
    	terminate:
    	// Done using buffer? Delete
    	delete[] CopyOfName;
    
    	int end;
    	cin >> end;
    	return 0;
    }

  2. #2
    Join Date
    Jul 2013
    Posts
    576

    Re: Debugging C++ code(Displaying char string per character)

    Quote Originally Posted by JourneyJay View Post
    *CopyOfName++;
    The * in this line

    *CopyOfName++;

    doesn't do anything. The result will be this,

    CopyOfName++;

    (which probably is what you wanted but still).

    Now the problem is that by incrementing CopyOfName this won't work,

    delete[] CopyOfName;

    You need to allow the delete[] operator to work on the originally allocated array. Since CopyOfName has changed the heap will end up corrupted and you have a memory problem on your hands. So assign CopyOfName to another char pointer and increment it instead. Or alternatively, increment CopyOfName but let delete[] operate on another char pointer holding the initial CopyOfName value.

    Some comments on your code:

    Beware of the dangling else. Intentions become unclear when code indentation doesn't fit bracket structure.

    Prefer letting loops terminate naturally (as it correctly would in this case). But if you need special exit criteria prefer break over goto. Goto is considered a malign statement in structured programming and should be avoided.

    And prefer using std::string over char buffers. Go for the highest applicable language abstraction available.
    Last edited by razzle; July 18th, 2014 at 02:52 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured