C
Christopher Benson-Manica
Is adding 0 to a pointer to non-void that is equal to NULL legal?
int *p=NULL;
p+=0;
int *p=NULL;
p+=0;
Christopher said:Is adding 0 to a pointer to non-void that is equal to NULL legal?
int *p=NULL;
p+=0;
Is adding 0 to a pointer to non-void that is equal to NULL legal?
int *p=NULL;
p+=0;
Christopher Benson-Manica said:Is adding 0 to a pointer to non-void that is equal to NULL legal?
int *p=NULL;
p+=0;
Christopher said:Is adding 0 to a pointer to non-void that is equal to NULL legal?
int *p=NULL;
p+=0;
Eric Sosman said:... but I can't recall being tempted to do any such thing
with a NULL value. What's up?
Christopher Benson-Manica said:Is adding 0 to a pointer to non-void that is equal to NULL legal?
int *p=NULL;
p+=0;
A pointer is nothiner else than a piece of memory with the right size to
store a memory address. Normally this is 4 bytes, but this may be different
on other different computers. You can do anything with this memory you can
do with other memory. This includes adding things to it - in this case 0.
The problem which make things illegal isn't that the pointer *is* NULL (=
0), but that the data the pointer points to is accessed. The pointer points
to the memory address 0, which can't be accessed.
So p += 0; would be valid, but it would keep the pointer to 0 (NULL). So
accessing this memory still is invalid. But with the pointer itself, you
can do anything. As long as the data the pointer points to isn't accessed.
A pointer is nothiner else than a piece of memory with the right size to
store a memory address. Normally this is 4 bytes, but this may be different
on other different computers.
You can do anything with this memory you can
do with other memory. This includes adding things to it - in this case 0.
The problem which make things illegal isn't that the pointer *is* NULL (=
0), but that the data the pointer points to is accessed. The pointer points
to the memory address 0, which can't be accessed.
So p += 0; would be valid, but it would keep the pointer to 0 (NULL). So
accessing this memory still is invalid. But with the pointer itself, you
can do anything. As long as the data the pointer points to isn't accessed.
Christopher Benson-Manica said:bar( buf?buf:"", buf?bufsize:0 );
Isn't it true that in most applications "NULL" is defined as 0 ? SoKeith Thompson said:"Spoofed Existence (astalavista.net)"
4 bytes is a common size for pointers, but there's nothing abnormal
about other sizes.
0.
You can do anything you want to with the *memory* (e.g., if you treat
it as an array of unsigned char), but the language limits what you can
do with it as a pointer. In particular, performing arithmetic on a
null pointer invokes undefined behavior, as has been explained
elsewhere in this thread.
No, the problem is precisely that it's a null pointer. As far as the
C language is concerned, a null pointer doesn't point to "memory
address 0"; it doesn't point to *anything*. (A null pointer is not
necessarily represented as all-bits-zero; see the FAQ.)
accessed.
That's incorrect. On many systems, p += 0; will result in p being
equal to NULL; that's just one of the many possible consequences of
undefined behavior.
(such as said:essentially in most applications the null pointer would be pointing to
address 0. Correct me if I am wrong.
As long as the data the pointer points to isn't
accessed.
DD said:Isn't it true that in most applications "NULL" is defined as 0 ? So
essentially in most applications the null pointer would be pointing to
address 0. Correct me if I am wrong.
Christian said:"Spoofed Existence (astalavista.net)"
Warning to every reader: What follows is absolutely wrong.
accessed.
Please stop giving incorrect advice.
I'm sorry. The compiler I use, gcc 3.3.4, defines NULL as (void*)0. Also,
gcc can use mathematic expressions on it, although some things will need
some changes.
The compiler I use, gcc 3.3.4, defines NULL as (void*)0.
Also,
gcc can use mathematic expressions on it, although some things will need
some changes.
So I assumed this was just a standard.
Well, I never read about any
standards, and I basically learned C by trying things, and by reading. But
I never really learned about other compilers than gcc. Might be a good idea
to do it sometime.
Christian said:If you learn about the C Standard itself instead of other compilers,
that would have the advantage that you learn what exactly is
_guaranteed_ to work and what is _not guaranteed_ to work. If something
is guaranteed to work then you don't have to learn about other
compilers, it will work.
I think most modern C compilers are pretty much compliant, right?
A pointer is nothiner else than a piece of memory with the right size to
store a memory address.
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.