You are not logged in.

#1 2013-01-26 04:06:49

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

[C Programming Language] How to check if the memory location speci ...

How do I check if the memory location specified in the pointer is freed and available or not? in C.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#2 2013-01-26 04:11:26

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 3,570
Website

Re: [C Programming Language] How to check if the memory location speci ...

As far as I know, you can't. You are responsible for keeping track of allocated memory yourself.

What are you really trying to do?

Last edited by drcouzelis (2013-01-26 04:16:33)

Offline

#3 2013-01-26 04:12:19

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,751
Website

Re: [C Programming Language] How to check if the memory location speci ...

What do you mean available?  Can you give an example?

A memory address is just an address - it is up to you to keep track of whether space has been allocated at that location and what data may be stored there.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#4 2013-01-26 04:34:06

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: [C Programming Language] How to check if the memory location speci ...

drcouzelis wrote:

As far as I know, you can't. You are responsible for keeping track of allocated memory yourself.

What are you really trying to do?

I want to see if the memory address is really available for other use after calling "free()".

It's just an experiment.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#5 2013-01-26 04:37:00

digirium
Member
Registered: 2012-11-15
Posts: 51

Re: [C Programming Language] How to check if the memory location speci ...

You might try mprobe() or mcheck() but that only tells you if the memory pointed to is available right at that moment within the context of the thread. Another thread may allocate the memory before you do anything so have to be careful. And you cannot rely on a positive either, the memory may be reallocated to something else and being used by some different code, cannot assume its still being used for the same purpose.

Offline

#6 2013-01-26 04:38:20

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,751
Website

Re: [C Programming Language] How to check if the memory location speci ...

No, it is most definitely not available for use after you free it.  You should only free it when you no longer want to use it.

Unless, of course, you mean you wish to use the same pointer for a new (m/c)alloc.  But in this case that function will return a *new* memory address.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#7 2013-01-26 04:54:42

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: [C Programming Language] How to check if the memory location speci ...

Got it, thanks alot.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#8 2013-02-07 04:32:42

Paruga
Member
From: Germany
Registered: 2013-02-07
Posts: 8
Website

Re: [C Programming Language] How to check if the memory location speci ...

digirium wrote:

You might try mprobe() or mcheck() but that only tells you if the memory pointed to is available right at that moment within the context of the thread. Another thread may allocate the memory before you do anything so have to be careful. And you cannot rely on a positive either, the memory may be reallocated to something else and being used by some different code, cannot assume its still being used for the same purpose.

You might also try to wrap it into an exception - if use is granted its OK, otherwise it spits out an exception. Not sure though.


---sometimes the obvious isn't there!---

Offline

#9 2013-02-07 11:46:05

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: [C Programming Language] How to check if the memory location speci ...

Furthermore, you can't check if a pointer has been freed or not, as the value of the pointer is unchanged. I find it's often useful to have a "checked free" macro like this:

#define xfree(ptr) if(ptr != NULL) { free(ptr); ptr = NULL; }

Now you're safe from accidental double-frees (although, their presence indicates something is wrong with your logic somewhere, even if they're harmless) and you can easily see if a pointer has been freed (which, again, should really not be in doubt, but it's nice to be sure).

Last edited by Barrucadu (2013-02-07 11:47:36)

Offline

#10 2013-02-07 12:03:04

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 3,570
Website

Re: [C Programming Language] How to check if the memory location speci ...

Paruga wrote:

You might also try to wrap it into an exception - if use is granted its OK, otherwise it spits out an exception. Not sure though.

Are you using a different C than I'm using? Because my C doesn't have exception handling. yikes

Offline

#11 2013-02-07 12:19:04

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [C Programming Language] How to check if the memory location speci ...

drcouzelis wrote:

Are you using a different C than I'm using? Because my C doesn't have exception handling. yikes

It kind of does: https://en.wikipedia.org/wiki/Exception … g_syntax#C


'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard

Offline

#12 2013-02-07 13:27:54

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,751
Website

Re: [C Programming Language] How to check if the memory location speci ...

Oh dear, that is hideous.  In any case, that is showing how to *implement* exception handling in C.  There is no exception handling for functions like free - in the example linked one would have to still wrap the free function in something like Barracuda's example.  Once that has been done, I see no point in the -jmp handling.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#13 2013-02-07 14:22:18

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [C Programming Language] How to check if the memory location speci ...

Trilby wrote:

Oh dear, that is hideous.

Hence 'kind of'.


'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard

Offline

#14 2013-02-07 14:51:38

digirium
Member
Registered: 2012-11-15
Posts: 51

Re: [C Programming Language] How to check if the memory location speci ...

I suppose handling a signal like SEGV then longjmp'ing to a diagnosis routine for memory allocation is possible. If you were prototyping some code you might do that to figure something out. But that would be like Plan Z, lol. Returning to the start of this discussion, Plan A might be imagined as something like having a library of routines on top of malloc/free to which you could add diagnostic functions i.e. "is this pointer still being used or not?".

Offline

#15 2013-02-08 22:52:42

saline
Member
Registered: 2010-02-20
Posts: 86

Re: [C Programming Language] How to check if the memory location speci ...

Barrucadu wrote:

Furthermore, you can't check if a pointer has been freed or not, as the value of the pointer is unchanged. I find it's often useful to have a "checked free" macro like this:

#define xfree(ptr) if(ptr != NULL) { free(ptr); ptr = NULL; }

Now you're safe from accidental double-frees (although, their presence indicates something is wrong with your logic somewhere, even if they're harmless) and you can easily see if a pointer has been freed (which, again, should really not be in doubt, but it's nice to be sure).

free does nothing if the pointer is already NULL, so the if statement is pointless.  Use this:

#define xfree(ptr) do { free(ptr); ptr = NULL; } while (0);

Offline

Board footer

Powered by FluxBB