Thread: new/delete problems, please help

  1. #1
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161

    new/delete problems, please help

    Hi,
    I ran across this problem with the new/delete operators:
    I allocate an array with new then perform some operations on it.
    Then when use delete my debugger screams: 'unmapped memory exception'.
    In the same code I also allocate a second array, perform almost the same operations and it deletes allright. Both of these pointers are mebers of a class and the delete operations performs from a public method of the class. The delete-statements looks like this:
    Code:
    if(pointer!=nil)
          delete pointer;
    can anyone tell me my mistake here...?
    It's really annoying as hell!!!
    Never had these problems before...

    //btq

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    To delete an array, use:

    delete [] pointer;

  3. #3
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    ok, so I found part of the problem still no solution!
    These pointers points to Bla, a class I've made.
    I declare the as:
    Code:
    Bla *one;
    Bla *two;
    If I delete two I get the error. However, if declare it as:
    Code:
    Bla *two;
    Bla *one;
    ,I can't delete one instead...?
    How come??

    thanks
    /btq

    [edit]
    I tried use delete [] pointer; but I got the same error...

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Please post some code.

  5. #5
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    it goes a little somthing like this:
    Code:
    class Note{
        public:
        int p, v, s, d;
        void SetNote(int, int, int, int);
        Note();
    }
    
    Note::Note(){
        p=0;
        d=0;
        v=0;
        s=0;
    }
    
    class Seq{
        ...
        Note *oldN;
        Note *newN;
        int count;
        void Select();
        Seq();
        ...
    }
    
    Seq::Seq(){
    ...
    newN = new Note[count];
    
    }
    
    void Seq::Select(){
    ...
    ...
        oldN=new Note[count];
        for(i=0;i<count;i++)
            oldN[i] = newN[i];
        if(newN!=nil)
             delete newN; //<--THIS PRODUCES THE ERROR!!!
    ...
    ...
    }
    hmm...obviously this isn't the full code but I thought since the code is rather big and on another computah I only include the parts. Hopfully the above is enough to discover the error...:)

    //btq

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    you've already been given the answer to your problem!

    if you do this:

    blah = new something[number];

    then use:

    delete [] blah;

    not

    delete blah;
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >    if(newN!=nil)
    >         delete newN; //<--THIS PRODUCES THE ERROR!!!
    I've never seen nil used. Isn't it NULL? Maybe this is something I haven't learned.

    However the above code may be your problem, as you never set newN to NULL after deleting it. Try this and see what happens:
    Code:
        if(newN!=NULL)
        {
             delete [] newN; //<--THIS PRODUCES THE ERROR!!!
             newN = NULL;
        }
    Also, I'm assuming you have count initialized to some value. It isn't automatically initialized to 0.

  8. #8
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    well, it seems that I did had gotten my question answerd. The strange part is that this didn't work the first time.
    How come delete array; works sometimes?. I guess the answer is that it might work but sometimes it doesn't so you're better off with delete [] array;...? Or am I way off(as I usually am)?

    Oh, and nil is the same as null i.e:
    Code:
    #define NULL 0L
    #define nil NULL
    thanks for yer help...

    /btq

  9. #9
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    I have no idea if the above has any side-effects. Most probably not. But 0 is an integer and should be viewed that way.

    Probably is best to code #define NULL 0

    If not for anything else, at least to avoid possible implicit casts when freeing dynamic memory.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How come delete array; works sometimes?. I guess the answer is that it might work but sometimes it doesn't so you're better off with delete [] array;...? Or am I way off(as I usually am)?

    I can't explain why it works sometimes. I have to admit I've accidently forgot the [] before, and delete seemingly worked fine. However just because my program didn't bomb doesn't mean it deleted the memory properly. Behind the scenes this may cause a memory leak, which will make the operating system unstable at some time in the future. Every time your program is run, it gets loaded into a different place in memory, so it may run one time, and bomb the next time.

    It's kind of analogous to the case where a programmer writes a program which runs fine at school. They then take it home and compile it and it won't run. Is it the home compiler's fault? Probably not, there's probably a bug in their code which didn't show up on their school computer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM