473,326 Members | 2,168 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,326 software developers and data experts.

pointer values

given: char *p = NULL + 1;

Does the above expression produce undefined behavior?

Where would the section be in the standard that
describes this?

--
conrad

Mar 3 '07 #1
8 1567
conrad wrote:
given: char *p = NULL + 1;

Does the above expression produce undefined behavior?
No; it's a constraint violation requiring a diagnostic.

There are two possibilities: NULL is either `(void*)0'
or plain `0' (or some other implementation-specific construct
equivalent to one of these). So:

- If NULL is `(void*)0', the expression `(void*)0 + 1'
is invalid: You cannot do pointer arithmetic with a pointer
to an incomplete type.

- If NULL is plain `0', the expression `0 + 1' has type
`int', which cannot be converted silently to `char*'.
Where would the section be in the standard that
describes this?
6.3.2.3, 7.17, 6.5.6, etc.

--
Eric Sosman
es*****@acm-dot-org.invalid

Mar 3 '07 #2
Eric Sosman <es*****@acm-dot-org.invalidwrites:
conrad wrote:
>given: char *p = NULL + 1;
Does the above expression produce undefined behavior?

No; it's a constraint violation requiring a diagnostic.
[...]

True, but here's an example that illustrates what I think the OP is
really asking:

char *p0 = NULL;
char *p1 = p0 + 1;

Or:

char *p = (char*)NULL + 1;

C99 6.5.6p8 defines the semantics of pointer+integer addition. It
doesn't define the semantics when the pointer operand is a null
pointer, so the behavior is undefined by omission.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 3 '07 #3
Keith Thompson wrote:
Eric Sosman <es*****@acm-dot-org.invalidwrites:
>conrad wrote:
>>given: char *p = NULL + 1;
Does the above expression produce undefined behavior?

No; it's a constraint violation requiring a diagnostic.
[...]

True, but here's an example that illustrates what I think the OP
is really asking:

char *p0 = NULL;
char *p1 = p0 + 1;

Or:

char *p = (char*)NULL + 1;

C99 6.5.6p8 defines the semantics of pointer+integer addition.
It doesn't define the semantics when the pointer operand is a
null pointer, so the behavior is undefined by omission.
However the "+ 1" above requires knowing the size of the object to
which the NULL points, which doesn't exist, and should trigger some
sort of complaint. I also suspect the cast of NULL should be a
no-op.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 4 '07 #4
#include <stdio.h>

int main(void)
{
char *p = (char *)NULL + 1;
printf("p = %p\n", (void *)p);

return 0;
}

In both VC++ 2005 Express Edition and gcc under Redhat Linux, no
warning or error is reported. Both print hexadecimal 1 (one).

So can we conclude that, by doing the casting of NULL, the size of +1
is known ?

Mar 5 '07 #5
"CBFalconer" <cb********@yahoo.comwrote in message
news:45***************@yahoo.com...
Keith Thompson wrote:
>True, but here's an example that illustrates what I think the OP
is really asking:

char *p0 = NULL;
char *p1 = p0 + 1;

Or:

char *p = (char*)NULL + 1;

C99 6.5.6p8 defines the semantics of pointer+integer addition.
It doesn't define the semantics when the pointer operand is a
null pointer, so the behavior is undefined by omission.

However the "+ 1" above requires knowing the size of the object to
which the NULL points, which doesn't exist, and should trigger some
sort of complaint. I also suspect the cast of NULL should be a
no-op.
No, the "+1" requires that the size of the type that "(char*)NULL" points to
is known. Since that type is (char), and sizeof(char) is defined to be 1,
the theoretical result is known.

However, actually performing this operation invokes UB since pointer math is
only defined between the start of an object and one past the end of that
object -- and NULL does not point to an object in the first place. No
object, no valid pointer math.

Still, most systems will end up giving the result "(char *)1". There are
some systems that will trap on it, though; I have no clue how they implement
offsetof(), since abusing this UB is the usual method.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov

--
Posted via a free Usenet account from http://www.teranews.com

Mar 5 '07 #6
su**************@yahoo.com, India wrote:
#include <stdio.h>

int main(void)
{
char *p = (char *)NULL + 1;
printf("p = %p\n", (void *)p);

return 0;
}

In both VC++ 2005 Express Edition and gcc under Redhat Linux, no
warning or error is reported. Both print hexadecimal 1 (one).
That's because you've gagged the compiler with your cast to char
pointer.
So can we conclude that, by doing the casting of NULL, the size of +1
is known ?
No. By casting your forcing the compiler to accept your construct and
compile it, but, unless you really know what you're doing, it's
behaviour may not be what you had expected.

Read Eric Sosman's reply upthread. He explains the problems and
diagnostics required, when there's no casting.

Mar 5 '07 #7
"su**************@yahoo.com, India" <su**************@yahoo.comwrites:
#include <stdio.h>

int main(void)
{
char *p = (char *)NULL + 1;
printf("p = %p\n", (void *)p);

return 0;
}

In both VC++ 2005 Express Edition and gcc under Redhat Linux, no
warning or error is reported. Both print hexadecimal 1 (one).

So can we conclude that, by doing the casting of NULL, the size of +1
is known ?
I don't know what you mean by "the size of +1", but the answer to your
question is no. The expression (char *)NULL + 1 invokes undefined
behavior. The standard defines the semantics of pointer addition in
certain cases, but not in this one. The fact that the program happens
to print 1 on your system tells you nothing as far as the standard is
concerned.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 5 '07 #8
su**************@yahoo.com, India wrote:
#include <stdio.h>

int main(void)
{
char *p = (char *)NULL + 1;
printf("p = %p\n", (void *)p);

return 0;
}

In both VC++ 2005 Express Edition and gcc under Redhat Linux, no
warning or error is reported. Both print hexadecimal 1 (one).

So can we conclude that, by doing the casting of NULL, the size of +1
is known ?
No, it means that the sizeof `char' is known.

Pointer arithmetic operates in units of the pointed-to
type: If you know the type you can perform the arithmetic,
if you do not know the type you cannot do the arithmetic.
The cast provides the type information that was missing
without it.

Of course, the fact that the compiler knows how to do
the arithmetic doesn't mean the result will be valid. In
the example shown, the result is not known to be valid.
Similarly, the compiler knows how do divide integers u/v,
but if v==0 the result is not known to be valid.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 5 '07 #9

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

Similar topics

4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
27
by: Riaan Cillié | last post by:
Hi I'm trying to learn C, but I am struggling with using scanf and a struct. I want to define a structure and declare a variable of that type in int main. This has to be passed to a function and...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
23
by: Leon Brodskiy | last post by:
Hi, Could please anyone clarify about pointer and array in C? If I have: int arr; The following two commands will be the same: arr and &arr.
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
12
by: lithiumcat | last post by:
Hi, I bothered you a while back about storing integer values in void*. Now in a completely unrelated context, I'm trying to store pointer values in an integer type. So the basic question is,...
41
by: simonl | last post by:
Hi, I've been given the job of sorting out a crash in our product for which we have the crash information and an avi of the event (which can't possibly match but more of that later...) (btw this...
49
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.