Thread: delete[] problem

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    delete[] problem

    Whenever i try to execute my code, I get the following error:
    Debug Error!

    Program: <Program Path>

    DAMAGE: after Normal block (#44) at 0x002F2AC8.
    Here's my code (only relevant code posted):
    Code:
    for(int i = 1; i < 4; i++)
    {
    	char * tempStr = new char[strlen(argv[i])];
    	strcpy(tempStr, argv[i]);
    	
    	for(int j = 0; j < strlen(argv[i]); j++)
    	{						
    		if(argv[i][j] == 'A') tempStr[j] = '0' + a;
    		if(argv[i][j] == 'B') tempStr[j] = '0' + b;
    		if(argv[i][j] == 'C') tempStr[j] = '0' + c;
    	}
    
    	Numbers[i - 1] = atol(tempStr);
    
    	delete[] tempStr; //Error on this line.
    
    	tempStr = NULL;
    }
    I'm using MSVC++ 6 Ent, btw.
    Last edited by XSquared; 10-05-2002 at 03:21 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Silent but deadly

    Code:
    char * tempStr = new char[strlen(argv[i])];
    strcpy(tempStr, argv[i]);
    So say argv[1] is 10 chars plus NULL....you make room for 10 chars (strlen())...and the NULL goes into the 11th char - no-man's land

    This is then picked up in delete []....and the debug build will issue an error

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Thanks a lot. It works now.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. delete[] problem with release config...
    By mikahell in forum C++ Programming
    Replies: 8
    Last Post: 08-21-2006, 10:37 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM