delete []array; //<-- not working...plz help

This is a discussion on delete []array; //<-- not working...plz help within the C++ Programming forums, part of the General Programming Boards category; The delete []array statement gives me an error when I run this program. The error is the attached picture. I ...

  1. #1
    Registered User KonArtis's Avatar
    Join Date
    Mar 2003
    Posts
    34

    Arrow delete []array; //<-- not working...plz help

    The delete []array statement gives me an error when I run this program. The error is the attached picture. I have no idea why this is happening. Can anyone help?

    Thanks,
    Joe

    ---------

    int main()
    {
    int *array,size=10,i=0;

    array= new int[size];
    if (array!=NULL){
    while (i<=size)
    {
    array[i]=i+10;
    cout << array[i] << endl;
    i++;
    }

    delete []array;
    };
    return 0;
    }
    Attached Images Attached Images  

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    When you allocate an array of size 10, that means you can access elements 0-9.........ie. "i<=size" is wrong.

    Use code tags next time you post code.

    gg

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use < not <= . You are walking past end of your array. Remember in c/c++ array bounds start at 0.
    Code:
    int main()
    {
    int *array,size=10,i=0;
    
    array= new int[size];
    if (array!=NULL){
    while (i<size)
    {
    array[i]=i+10;
    cout << array[i] << endl;
    i++;
    }
    delete []array;
    }
    return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User KonArtis's Avatar
    Join Date
    Mar 2003
    Posts
    34

    Thumbs up

    THANKS!!! That worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-18-2008, 03:06 AM
  2. cant get this delete node working
    By qubit67 in forum C Programming
    Replies: 3
    Last Post: 04-25-2007, 02:36 AM
  3. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 10:17 PM
  4. i have a pointer that it aint working. plz help.
    By adventurer in forum C++ Programming
    Replies: 7
    Last Post: 05-01-2002, 05:58 PM
  5. delete isn't working for me!
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 10-06-2001, 09:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21