Checking if a pointer is allocated

This is a discussion on Checking if a pointer is allocated within the C++ Programming forums, part of the General Programming Boards category; How can I check if my pointer has allocated memory? I want to use delete [] on it, but I ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Checking if a pointer is allocated

    How can I check if my pointer has allocated memory?

    I want to use delete [] on it, but I can't unless I know its already allocated... and a bool won't work well... how can I check a memory address to see if

    address = new char[blah]

    has been executed?

    Code:
    			if (operand.charptr != NULL)
    			{
    				cout << "Freed";
    				delete [] operand.charptr;
    			}
    			if (operand.charptr == NULL)
    			{
    				operand.charptr = new char[strlen(temp)];
    				strcpy(operand.charptr,temp);
    			}
    Thats what I was trying to do.

  2. #2
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    well usually you do

    Code:
    int *ptr = new int;
    
    if(ptr==NULL){
       cout<<"Allocation error";
    }

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    how would I do what I'm trying to do?

    It doesn't seem to ever do the delete because it never outputs the string

  4. #4
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    well it looks like you're trying to delete the pointer if there already is one, and then create a new one. you can just take out the if statements and do


    Code:
    delete [] operand.charptr;
    operand.charptr = new char[strlen(temp)];
    strcpy(operand.charptr,temp);
    because even with the if statements, it will do the same thing

    and im not quite sure, you might have to do
    operand.charptr = new char[strlen(temp)+1];
    to accomodate for the null character at the end

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    eh yeah.

    Let me explain again:

    The first time this is run, there is NO allocation.

    Every time after, there is. I can't delete it the first time, obviously

  6. #6
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    oh, then you don't need a delete[] if you're allocating it for the first time..you only can delete if you've allocated it with new at least one time already

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by the Wookie
    oh, then you don't need a delete[] if you're allocating it for the first time..you only can delete if you've allocated it with new at least one time already
    I know, which is why I have this post. . .

    I need to know how to check whether or not delete[] should be used.

    Originally posted by Trauts
    The first time this is run, there is NO allocation.

  8. #8
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    you dont need it if its the first time though

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by the Wookie
    you dont need it if its the first time though
    I KNOW THAT!!! This might be run more than once, however, and I want to free the memory, but I can't do that the first time! The second time I absolutely have to.

  10. #10
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    lol ok, its in a loop or something? what do you mean run more than once?

  11. #11
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Initially set the pointer to NULL. delete and delete[] take no action on a NULL pointer. So first time through itt'l be NULL, and delete'ing it will do nothing.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    create a new int that counts runtimes... if it's set to zero, don't delete, if it's set to anything other (true) then delete... just increment it after every run... there's probably better ways than that though...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    Originally posted by Eibro
    Initially set the pointer to NULL. delete and delete[] take no action on a NULL pointer. So first time through itt'l be NULL, and delete'ing it will do nothing.
    thats what i said..apparently thats not what he wanted

  14. #14
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Code:
    // operand.charptr needs to be initialized to NULL
    
    if( operand.charptr != NULL )
    {
    	cout << "Freed";
    	delete [] operand.charptr;
            operand.charptr = NULL; // new !
    }
    if( operand.charptr == NULL ) // always true now, obsolete
    {
    	operand.charptr = new char[strlen(temp)];
    	strcpy(operand.charptr,temp);
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. 2D Dynamically allocated pointer arrays
    By Lionmane in forum C Programming
    Replies: 37
    Last Post: 06-11-2005, 10:39 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

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