Thread: Problem deleting dynamic array

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    Problem deleting dynamic array

    So I dynamically allocated an array in a simple function and when I tried to free up the memory at the end of the function I got some kind of error during run time. When I removed the 'delete' statement the program ran fine. Please advice.


    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    void drawstuff(char array[]);
    int main()
    {
    	char *b = "I am Peter";
    	drawstuff(b);
        
    }
    
    void drawstuff(char array[]) {
    	char *a = new char[10];
    	for (int i = 0; i < 11; i++)
    		a[i] = array[i];
    	std::cout << a << endl;
    	delete []a;
    	}

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by pliang
    So I dynamically allocated an array in a simple function and when I tried to free up the memory at the end of the function I got some kind of error during run time. When I removed the 'delete' statement the program ran fine. Please advice.


    Code:
    	char *a = new char[10];
    	for (int i = 0; i < 11; i++)
    This line causing run time error. You have allocated memory for 10 elements, so subscript must go from 0 to 9 and you have a[10] and that is eleventh element. So basically you try to free memory which you didn't allocated and I assume that is causing error
    I suggest you to consider what are you doing.
    I'd do the same thing somethink like this:
    Code:
    a = new char[strlen(array)+1];
    for (int i = 0; array[i] !='\0'; ++i)
        a[i]=array[i];
    a[i] = '\0';
    Last edited by Micko; 04-10-2005 at 01:53 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    Yup, that was the problem I hate it when I miscount the array indices Thanks!

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    So basically you try to free memory which you didn't allocated and I assume that is causing error
    Where?
    Code:
    char *a = new char[10];
    ...
    ...
    delete []a;
    pliang,
    You have allocated memory for 10 elements, so subscript must go from 0 to 9 and you have a[10] and that is eleventh element
    Pay attention to that comment. But, changing your loop control to 10 isn't going to work either. Generally, you can't output char arrays using cout<<. You can output cstrings using cout<<. A cstring is a char array that ends in a '\0'. The reason is simple: cout<< uses a loop to output a char array until a '\0' is encountered:
    Code:
    int i = 0;
    while(a[i] != '\0')
    {
         cout<<a[i];
         i++;
    }
    If the loop doesn't find a '\0' it happily runs past the end of the array and outputs whatever it finds. I'm not sure what ultimately makes it stop outputing characters.
    Last edited by 7stud; 04-10-2005 at 02:21 AM.

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by 7stud
    Where?
    Well, technicaly he didn't try to free memory that wasn't allocated, but on my system if memory is allocated, program goes beyond the array bounds and delete is called it's always crash.
    Maybe a correct way would be to describe this as undefined behavior!?!
    Last edited by Micko; 04-10-2005 at 03:22 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Micko
    Well, technicaly he didn't try to free memory that wasn't allocated, but on my system if memory is allocated, program goes beyond the bounds and delete is called it's always crash.
    Maybe a correct way would be to describe this as undefined behavior!?!
    Yes, it does appear to work that way. I thought if I commented out the delete statement it would still crash, but it doesn't.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    Quote Originally Posted by 7stud
    Where?
    Pay attention to that comment. But, changing your loop control to 10 isn't going to work either. Generally, you can't output char arrays using cout<<. You can output cstrings using cout<<. A cstring is a char array that ends in a '\0'. The reason is simple: cout<< uses a loop to output a char array until a '\0' is encountered:
    Code:
    int i = 0;
    while(a[i] != '\0')
    {
         cout<<a[i];
         i++;
    }
    If the loop doesn't find a '\0' it happily runs past the end of the array and outputs whatever it finds. I'm not sure what ultimately makes it stop outputing characters.
    Ya, I noticed I forgot to put in the null termination in my array, however, after playing around I found that it was exactly as Micko said, my memory allocation is fine but my array went out of bound and thus causing my program to crash at run time, it's a bit strange indeed!

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    This really is an interesting issue and I sent PM to Prelude asking for help. She replied and I decided to share this with you. Here's her answer:
    Quote Originally Posted by Prelude
    The memory manager is a brittle beast. If you write past the bounds of allocated memory then chances are good you've overwritten some of the memory manager's bookkeeping. This doesn't show up until the delete because "technically" the memory you wrote to is probably in your address space, so an access violation wouldn't be flagged immediately.

    Because operator delete[] requires the bookkeeping to properly release the memory, it panics and crashes when the values aren't exactly what it expects.
    Thanks Prelude, I'm deeply indebted to you.
    Cheers

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. deleting dynamic object problem
    By eth0 in forum C++ Programming
    Replies: 17
    Last Post: 05-19-2004, 01:17 PM
  3. Dynamic Array Problem
    By adc85 in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 02:29 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM