Thread: determine if mem has been (de)allocated

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    determine if mem has been (de)allocated

    Is there a function call to determine if some location in memory has been allocated:

    int *p;
    p = new int;
    if (HasBeenAllocated(p))
    cout << "yes" << endl;
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    New throws a bad_alloc exception on failure.

    Code:
    try {
        char *mychar = new char[256];
    } catch (bad_alloc ba) {
        cout << ba.what() << endl;
    }
    Some compilers don't propely implement this (ie MSVC++ 6), checking if your pointer is NULL is another way to see if allocating failed.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    I dont mean whether an allocation failed...

    I guess an example would explain it better. I have some memory that is allocated and is pointed to by multiple pionters. When it comes time to delete that memory, I need to know whether that memory has been deleted by a previous pointer or not. An easy solution would be to have some function that will determine if an address points to memory that has been allocated. I wanted to know whether a function exists that will determine that (there must be some table of allocated addresses somewhere)
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Sorry, your function name threw me off... HasBeenAllocated().
    Curious, can you explain why you need to do this? There's always another way... this seems like bad design.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >When it comes time to delete that memory, I need to know whether that memory has been deleted by a previous pointer or not.
    You can't do this with raw pointers. What you want to do is create a smart pointer class that maintains a reference count. Only if the reference count drops to 1 do you actually free the memory.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >Curious, can you explain why you need to do this?
    I wrote a program a while ago that builds a database of around 100,000+ files. It first stores the file info (filename, path, crc, size) in a linked list, then creates an array of pointers to the data in the node and sorts it by crc (so I can detect duplicates). Since many files could be in the same subdirectory, the path entry stores a pointer to a path, and many files might share that pointer(to conserve memory)

    I wrote it a while ago and didnt comment it so I dont want to change too much not knowing what the effects might be elsewhere(I dont have the time, or the will, to rewrite too much (any) of it). I just now decided to add the feature to remove all duplicate files (I assume no file will have the same crc and filesize that arent identical). When I determine a file is a duplicate, I need to delete it, then remove it from the list, but I dont what to delete a path that another node migh be using.

    As I was typing this I thought of a simple solution, just check the previous and succeeding nodes to see if they point to the same path... since any node with the same path will be next to each other (damn, now I have to make my list doubly linked, [edit: and change the array to point to the actual nodes]).

    >What you want to do is create a smart pointer class
    that sounds like a good idea, but dont want to rewrite more than absolutely necessary
    Last edited by *ClownPimp*; 11-09-2002 at 11:35 AM.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determine the largest and the smallest
    By JC33 in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2007, 05:51 AM
  2. Mem alloc
    By dazellerington in forum C Programming
    Replies: 2
    Last Post: 05-06-2007, 02:55 PM
  3. Determine length of an array
    By ulillillia in forum C Programming
    Replies: 7
    Last Post: 04-21-2007, 08:32 PM
  4. Replies: 6
    Last Post: 06-02-2006, 08:32 AM
  5. determine how many 0's are in a number (value)
    By pyazarian in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2005, 01:20 PM