check if char* is a ptr to mem or nirvana

P

Peter Dunker

Hello,

I think I have a newbee question.

If I create a char pointer at the beginning of function and use it only
in a special case:

char *name;

if (a = 1)
name = "Hello";


Now I will check if the name pointer was set,
ist that possible ?

if (name ## is set ##)

strlen(name) -> Error
strcmp(name) -> Error

I know that I can set the pointer to 0.
But is there another solution ?

Check if the pointed memory is allocated ?

Thx
Peter
 
K

- Kees van der Bent -

Peter said:
If I create a char pointer at the beginning of function and use it only
in a special case:

char *name;

if (a = 1)

This condition is always true (a common coding error). Use ==.
name = "Hello";


Now I will check if the name pointer was set,
ist that possible ?

if (name ## is set ##)

strlen(name) -> Error
strcmp(name) -> Error

I know that I can set the pointer to 0.
But is there another solution ?

Check if the pointed memory is allocated ?

No memory is allocated here. So you might check if
the pointed address is valid. However, even when it
would be possible to check the address for validity,
you still would not know if it is "Hello"'s address.
Depending on the context where <name> is defined, it
is either 0 or may contain (register or stack) garbage.
This garbage value might be a valid address too.

So, setting <name> to 0 would work. Alternatively you
could:

char *hello = "hello";
char *name;

if (a == 1)
name = hello;

if (name == hello)
;


Kees
 
D

Dan Pop

In said:
If I create a char pointer at the beginning of function and use it only
in a special case:

char *name;

if (a = 1)

This looks like a test, but it ain't one.
name = "Hello";


Now I will check if the name pointer was set,
ist that possible ?

if (name ## is set ##)

strlen(name) -> Error
strcmp(name) -> Error

I know that I can set the pointer to 0.
But is there another solution ?

Isn't initialising the pointer with 0 good enough for you? If, for
whatever reason it isn't, you can create your own default pointer
initialiser:

union {int i; char c; short s; long l; float f; double d;} nil;

char *name = &nil.c;
int *p = &nil.i;

But you *must* initialise the pointer at the point of definition and
as soon as its current value becomes obsolete/indeterminate.
Check if the pointed memory is allocated ?

This, you cannot do. All you can do is assume that if the pointer doesn't
compare equal to its default initialiser, it points to what it is
supposed to point. Of course, it takes a lot of programming discipline
to make this assumption work.

In practice, the people who are disciplined enough to make it work,
are also disciplined enough not to need it at all, so most of them just
don't bother. Once you get pointer values from code you haven't written
yourself, there is NO portable method of checking their integrity.

Dan
 
J

Jack Klein

This condition is always true (a common coding error). Use ==.


No memory is allocated here. So you might check if
the pointed address is valid. However, even when it
would be possible to check the address for validity,

There is no way to check an uninitialized pointer for validity. Any
use of its value, including passing it to a function or testing for
NULL, produces undefined behavior.
you still would not know if it is "Hello"'s address.
Depending on the context where <name> is defined, it
is either 0 or may contain (register or stack) garbage.
This garbage value might be a valid address too.

So, setting <name> to 0 would work. Alternatively you
could:

char *hello = "hello";
char *name;

if (a == 1)
name = hello;

if (name == hello)

This test produces undefined behavior if name has not been
initialized.

Perhaps you should consult a good reference on C about the perils of
examining the values of uninitialized pointers.
 
K

kal

Peter Dunker said:
Check if the pointed memory is allocated ?

I doubt if "C" provides this functionality.

<OT>
Win32 has "IsBadStringPtr" and similar functions. Even then one
can only ascertain if the memory pointed to is accessible and not
if the memory was _allocated_.

Dig deep into the memory management routines (OS specific.)
There should be something there.
</OT>
 
S

Sam Dennis

kal said:
Peter Dunker said:
Check if the pointed memory is allocated ?

I doubt if "C" provides this functionality.
[OS-specific memory location validity check functions]

That isn't a very good idea. The standard does say that using an
uninitialised value is undefined behaviour for a reason, and your
method's answer is pretty useless as, even if such facilities are
available, the value in the unitialised memory/register/ether may
point to other applications' data, code, memory-mapped registers,
permanent storage; anything, in fact, potentially.

This isn't just an issue for mythical or long dead machines; it's
an appallingly bad idea anywhere, and the only legitimate use for
such functions, AFAICS, is protection for debuggers.
 
S

Stephen Sprunk

Peter Dunker said:
If I create a char pointer at the beginning of function and use it only
in a special case:

char *name;

if (a = 1)
name = "Hello";


Now I will check if the name pointer was set,
ist that possible ?

if (name ## is set ##)

strlen(name) -> Error
strcmp(name) -> Error

I know that I can set the pointer to 0.

char *name = NULL;

if (a == 1)
name = "Hello";

if (name)
do_whatever;
But is there another solution ?

Check if the pointed memory is allocated ?

C doesn't define any way to determine if a given address is valid, and even
testing an invalid pointer invokes undefined behavior, though it may appear
to work on many common systems.

S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top