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;
Printable View
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;
New throws a bad_alloc exception on failure.
Some compilers don't propely implement this (ie MSVC++ 6), checking if your pointer is NULL is another way to see if allocating failed.Code:try {
char *mychar = new char[256];
} catch (bad_alloc ba) {
cout << ba.what() << endl;
}
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)
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.
>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
>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