Thread: Checking if memory has been dynamically allocated

  1. #1
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107

    Checking if memory has been dynamically allocated

    I ran into a situation while coding where I could end up needing to replace the pointer to a value that is either dynamically allocated with malloc or a pointer to a contstant value with another pointer. Is there a way I can check the status of the memory location to see which it is?

  2. #2
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Ye could test for null and the the data type at the mem location which could be pointer or actual value. Not clear on what exactly ye're asking but I hope that helps.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I ran into a situation while coding where I could end up needing to replace the pointer to a value that is either dynamically allocated with malloc or a pointer to a contstant value with another pointer. Is there a way I can check the status of the memory location to see which it is?
    No.

  4. #4
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Err... in that case what do you think is the best way to work around the issue. I'd hate to be stuck with memory leaks in my program...

  5. #5
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    ye think ye could give a simplified example of what it is you're trying to do exactly then we can give you a simplified answer in return.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  6. #6
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    The problem isn't really that complicated the issue is I have a pointer variable the is either points to malloced memory or doesn't. I need to find which one it is so I know if I have to free the memory pointed to by the pointer or not when I change the pointer to something else. Actually, I thought of a somewhat adequite soulution which would be to always malloc the memory even if it doesn't have to be, but if anyone thinks of a better soulotion let me know.

  7. #7
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Ok the usual
    Code:
     if (ptr ==NULL) do this //assuming you declared var integral_type *ptr;
    doesn't work for you?? that should let you know whether or not memory's already allocated. if you always wants to allocate memory regardless of whether it's allocated or not then you're asking for trouble in the former case. Test for the NULL pointer foist then you cans do what you wants after depending on the results. Hope that helps ya.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  8. #8
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    That wouldn't work in my case since the issue isn't wheather or not the pointer is pointing to anything, but rather what kind of thing it's pointing to. The pointer would newer be null.

  9. #9
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    ok, Now I get ya ( I thinks). Just so I'm clear on this the pointer variable itself is of type void *?? elsewise the pointer variable you declare will always point only to the data type it is declared as; try anything else and you're likely to crash your program. As for using void pointers, the only thing I can think of is, a sizeof type comparisons workaround, but that is no guarantee (in fact probably downright dumb of me to mention, but hey, it's an idea). Remember this is C not C++ where you can have class pointers that point to members in which case there's a solution to that.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  10. #10
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Err... you're still not quite getting it. Here's the thing, specifically I'm talking about a pointer to a string here. Sorry I didn't mention that earlier since that would've probobly helped you understand. The problem is, when I want to pointer to a string to point to a new string I need to know if the old one was allocated using malloc or if it's a pointer to a constant string.

  11. #11
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    That is now clear. Nope there is no way of telling in C, I would think a work around would be to test for the failure of free(); but I don't thinkyou can do that without crashing and burning. But if you do it in C++ you could try doing the afore-mentioned idea by encapsulating it in a try/catch statement. Again it's an idea seeing as c++ recognises C and you would be borrowing just that feature of C++ me thinks.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  12. #12
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Well, I already commited myself to using pure C for this program. Is there perhaps a way to simulate a try/catch in C using ASM?

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, I would say just free() it either way. The OS memory manager won't find the local variable when you call free(), and since it's still in you're programs memory space, won't cause an access violation. On the other hand, the malloc'ed memory will be found, of course, and released accordingly.

    You can see this for yourself - it won't crash:

    Code:
     int main()
    {
     int i, j, k;
    
     free(&i), free(&j), free(&k);
    }
    Hope that helps.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Oh... see, I thought doing a free on non-malloced memory would cause the program to crash. Well since that isn't an issue, I guess there's no problem .

  15. #15
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I was thinking free crashed and burned the program too after all this time i'm still humbled.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically allocating memory
    By rs07 in forum C Programming
    Replies: 6
    Last Post: 09-14-2008, 03:26 AM
  2. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM
  3. Checking if memory was successfuly allocated.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 04-13-2005, 07:12 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. deleting dynamically allocated memory...
    By heljy in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2002, 11:40 AM