473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

check if char* is a ptr to mem or nirvana

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
Nov 14 '05 #1
6 3208
Peter Dunker wrote:
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

Nov 14 '05 #2
In <c6**********@piggy.rz.tu-ilmenau.de> "Peter Dunker" <Pe**********@stud.tu-ilmenau.de> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3
On Wed, 21 Apr 2004 17:23:18 +0200, - Kees van der Bent -
<kv*@mbalance.moc> wrote in comp.lang.c:
Peter Dunker wrote:
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,


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.
;
Kees


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
kal
"Peter Dunker" <Pe**********@stud.tu-ilmenau.de> wrote in message news:<c6**********@piggy.rz.tu-ilmenau.de>...

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>
Nov 14 '05 #5
kal wrote:
"Peter Dunker" <Pe**********@stud.tu-ilmenau.de> wrote in message news:<c6**********@piggy.rz.tu-ilmenau.de>...
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.

--
++acr@,ka"
Nov 14 '05 #6
"Peter Dunker" <Pe**********@stud.tu-ilmenau.de> wrote in message
news:c6**********@piggy.rz.tu-ilmenau.de...
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

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
1
by: Patrick Gunia | last post by:
Hi, i´m trying to build a xml - parser, which should simply list all used tokens an dattributes including their values. So far, so good, this works, but now i try to check for illegal phrases in...
2
by: CR | last post by:
having a problem with the final strcpy of filename from *argv into a structure containing an array with 20 elements called fname It seems that strcpy & strncpy aren't stopping after null is found...
2
by: Nirvana | last post by:
What's value of uninitialized char declared outside of any function ? cheers
3
by: Al | last post by:
Hello, Sorry for the silly question: How do I insert Check Mark into RichTextBox? I can’t find the ascii code for it. Thanks Alex
3
by: kathy | last post by:
I want to know what is the easy way to check if a string is a number or not? the number can be int, float, double, scientific,... what is the easy way for only interger?
7
by: xiaolim | last post by:
hi, sorry to disturb again, currently i have an assignment on stacks and as show in the the title, i need to use stacks to determine palindrome. i've done part of my code, here it is: //...
55
by: lovecreatesbea... | last post by:
Do you check all error conditions for all library calls in you code? Is it necessary to check all errors? Is it convenient to check all errors (or how to make code clean and readable with mass of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.