What is this?
( char * ) &( ( struct aStruct * ) 0 )
It looks like it's taking the address of something that contains a
pointer to null, and casting it to a pointer to char.
(Actually, technically, the address of a pointer to the beginning of a
struct aStruct, that starts at address 0).
Or, is it taking the pointer to null itself, and casting that to a
pointer to char? 19 2271
On Nov 19, 1:48*pm, Eric <answer.to.newsgr...@nospam.comwrote:
What is this?
( char * ) *&( ( struct aStruct * ) *0 )
It wouldn't work, because you can't take address (&) of the constant
(( struct aStruct * ) 0 ).
Eric wrote:
>
What is this?
( char * ) &( ( struct aStruct * ) 0 )
It looks like it's taking the address of something that contains a
pointer to null, and casting it to a pointer to char.
No, it looks like something that attempts to take the address of a null
pointer value. As such, it's a constraint violation (6.5.3.2p1).
In article <kh********************************@4ax.com>,
Eric <an*****************@nospam.comwrote:
>What is this?
( char * ) &( ( struct aStruct * ) 0 )
As it is, it's an error. If it were
( char * ) &( ( struct aStruct * ) 0 )->x
(assuming x is a member of the struct), then it would be an
almost-plausible (though theoretically non-portable) imitation of
offsetof().
-- Richard
--
Please remember to mention me / in tapes you leave behind.
Eric <an*****************@nospam.comwrites:
What is this?
( char * ) &( ( struct aStruct * ) 0 )
It is a constraint violation. That means that a conforming compiler
must issue a diagnostic. The result of a cast is not an lvalue and
the operand of & must be an lvalue.
It looks like it's taking the address of something that contains a
pointer to null, and casting it to a pointer to char.
(Actually, technically, the address of a pointer to the beginning of a
struct aStruct, that starts at address 0).
Not really. Once you have a constraint violation the program is
essentially meaningless, but if we put that to one side (i.e. we
pretend that the stated constraint is missing from the C
specification) then all we have is an attempt to take the address of a
null pointer constant. Null pointers (constant or otherwise) do not
point at anything -- specifically they don't point to an object at
"address zero".
The context of the code might explain the mystery, but it is hard to
guess any intent (at least I can't). Maybe there is a typo?
--
Ben.
Ben Bacarisse wrote:
Eric writes:
What is this?
( char * ) &( ( struct aStruct * ) 0 )
It is a constraint violation. That means that a conforming compiler
must issue a diagnostic. The result of a cast is not an lvalue and
the operand of & must be an lvalue.
It looks like it's taking the address of something that contains a
pointer to null, and casting it to a pointer to char.
(Actually, technically, the address of a pointer to the beginning of a
struct aStruct, that starts at address 0).
Not really. Once you have a constraint violation the program is
essentially meaningless, but if we put that to one side (i.e. we
pretend that the stated constraint is missing from the C
specification) then all we have is an attempt to take the address of a
null pointer constant. Null pointers (constant or otherwise) do not
point at anything -- specifically they don't point to an object at
"address zero".
The contents of the pointer are irrelevant since we're taking its ADDRESS.
Taking the address of a null pointer is perfectly fine (and casting this
to char* is too):
struct aStruct* p = NULL;
char* cp = (char*) &p; // perfectly valid
Maybe you were thinking of taking the address of the pointed-to object,
that is, dereferencing it first? In C89, it's undefined behavior, but in
C99 it's well-defined; the footnote for section 6.5.3.2 starts out: "83)
Thus, &*E is equivalent to E (even if E is a null pointer) [...]".
struct aStruct* p = NULL;
char* cp = (char*) &*p; // undefined behavior in C89 (valid in C99) bl********@gishpuppy.com (blargg) writes:
Ben Bacarisse wrote:
>Eric writes:
What is this?
( char * ) &( ( struct aStruct * ) 0 )
It is a constraint violation. That means that a conforming compiler must issue a diagnostic. The result of a cast is not an lvalue and the operand of & must be an lvalue.
It looks like it's taking the address of something that contains a
pointer to null, and casting it to a pointer to char.
(Actually, technically, the address of a pointer to the beginning of a
struct aStruct, that starts at address 0).
Not really. Once you have a constraint violation the program is essentially meaningless, but if we put that to one side (i.e. we pretend that the stated constraint is missing from the C specification) then all we have is an attempt to take the address of a null pointer constant. Null pointers (constant or otherwise) do not point at anything -- specifically they don't point to an object at "address zero".
The contents of the pointer are irrelevant since we're taking its ADDRESS.
Taking the address of a null pointer is perfectly fine (and casting this
to char* is too):
Yes, true, but I don't think I contradicted that, did I? I was keen
to correct the idea that (T *)0 is pointer to a T "at address zero".
<snip>
--
Ben.
Ben Bacarisse wrote:
Eric <an*****************@nospam.comwrites:
What is this?
( char * ) &( ( struct aStruct * ) 0 )
It is a constraint violation. That means that a conforming compiler
must issue a diagnostic. The result of a cast is not an lvalue and
the operand of & must be an lvalue.
It looks like it's taking the address of something that contains a
pointer to null, and casting it to a pointer to char.
(Actually, technically, the address of a pointer to the beginning of a
struct aStruct, that starts at address 0).
Not really. Once you have a constraint violation the program is
essentially meaningless, but if we put that to one side (i.e. we
pretend that the stated constraint is missing from the C
specification) then all we have is an attempt to take the address of a
null pointer constant. Null pointers (constant or otherwise) do not
point at anything -- specifically they don't point to an object at
"address zero".
The fact that this pointer is null is irrelevant. There would be no
problem if it were a null pointer object rather than a null pointer
constant. The problem is that it's a pointer value, not a pointer
object, and it is just as much of a problem whether or not the pointer
value is null.
The context of the code might explain the mystery, but it is hard to
guess any intent (at least I can't). Maybe there is a typo?
If it were followed by ->x, where "x" is one of the members of struct
aStruct, the only problem with this code would be the fact that it
attempts to de-reference a null pointer, rendering the behavior
undefined. For that reason, such a construct should never occur in
user code. However, on many implementations the actual behavior of
this construct is such that, when the result is converted to size_t,
it constitutes a conforming implementation of the offsetof() macro.
Since <stddef.his part of the implementation, it's perfectly
legitimate for the implementor to take advantage of that fact inside
of stddef.h, even though developers should not do so in user code.
Ben Bacarisse wrote:
blargg writes:
Ben Bacarisse wrote:
[...]
Null pointers (constant or otherwise) do not
point at anything -- specifically they don't point to an object at
"address zero".
The contents of the pointer are irrelevant since we're taking its ADDRESS.
Taking the address of a null pointer is perfectly fine (and casting this
to char* is too):
Yes, true, but I don't think I contradicted that, did I? I was keen
to correct the idea that (T *)0 is pointer to a T "at address zero".
Ahhh, I see now what you were getting at, that for example the
(non-portable) offsetof implementation based on it depends on a null
pointer's representation being all-zero,
#define offsetof(n,m) ((size_t) &((n*) 0)->m)
whereas something like
#define offsetof(n,m) ((size_t) &((n*) sizeof (n))->m - sizeof (n))
doesn't depend on a null pointer representation (but is still not fully
portable of course). Or more simply, to get a pointer to a T at zero, you
need something like (again, not fully portable, but then again, needing an
object at zero is inherently platform-specific)
T* t_at_zero = ((T*) sizeof (T)) - 1;
jameskuyper <ja*********@verizon.netwrites:
Ben Bacarisse wrote:
>Eric <an*****************@nospam.comwrites:
What is this?
( char * ) &( ( struct aStruct * ) 0 )
It is a constraint violation. That means that a conforming compiler must issue a diagnostic. The result of a cast is not an lvalue and the operand of & must be an lvalue.
It looks like it's taking the address of something that contains a
pointer to null, and casting it to a pointer to char.
(Actually, technically, the address of a pointer to the beginning of a
struct aStruct, that starts at address 0).
Not really. Once you have a constraint violation the program is essentially meaningless, but if we put that to one side (i.e. we pretend that the stated constraint is missing from the C specification) then all we have is an attempt to take the address of a null pointer constant. Null pointers (constant or otherwise) do not point at anything -- specifically they don't point to an object at "address zero".
The fact that this pointer is null is irrelevant. There would be no
problem if it were a null pointer object rather than a null pointer
constant. The problem is that it's a pointer value, not a pointer
object, and it is just as much of a problem whether or not the pointer
value is null.
Total failure to communicate on my part, I guess, since two people
have now made this point! I only wanted to address the phrase "a
struct aStruct at address 0". The cast expression (struct aStruct *)0
is (as you know) just a null pointer of struct aStruct * type and has
nothing to do with an object of type struct aStruct (and I did point
out what the problem really is with the code right up front!).
--
Ben. bl********@gishpuppy.com (blargg) writes:
Ben Bacarisse wrote:
>blargg writes:
Ben Bacarisse wrote:
[...]
>Null pointers (constant or otherwise) do not point at anything -- specifically they don't point to an object at "address zero".
The contents of the pointer are irrelevant since we're taking its ADDRESS.
Taking the address of a null pointer is perfectly fine (and casting this
to char* is too):
Yes, true, but I don't think I contradicted that, did I? I was keen to correct the idea that (T *)0 is pointer to a T "at address zero".
Ahhh, I see now what you were getting at, that for example the
(non-portable) offsetof implementation based on it depends on a null
pointer's representation being all-zero,
#define offsetof(n,m) ((size_t) &((n*) 0)->m)
No, that was not what I meant. For one thing, that code does not
depend on the representation of a null pointer (it is non-portable
because it involves de-referencing a null pointer). Honestly, all I
wanted to say was "null pointers do not point at anything --
specifically they don't point to an object at address zero".
I won't comment on the rest for fear of complicating matters more.
--
Ben.
On Wed, 19 Nov 2008 12:07:07 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
>The context of the code might explain the mystery, but it is hard to guess any intent (at least I can't). Maybe there is a typo?
Good afternoon, Ben.
I apologize for not providing enough information... I was just trying
to minimize my post but I guess I minimized it too much. :-)
A more detailed block of code is:
bool result;
bool GetByte( char *theAddr, int theCount, char *theDest);
char Dest[ SOME_SIZE ];
result =
GetByte( ( char * ) &( ( struct aStruct * ) 0 )->Addr,
1, ( char * ) ( &Dest ) );
My question is... does the struct aStruct live at address 0, or does
address 0 contain a pointer to wherever aStruct actually lives? Seems
to me like the "&" ahead of ( ( struct aStruct * ) 0 )->Addr would
indicate the latter...
On Wed, 19 Nov 2008 22:56:14 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
>that code does not depend on the representation of a null pointer (it is non-portable because it involves de-referencing a null pointer).
Technically, it's dereferencing a zero pointer, which isn't guaranteed
to be the same thing as a null pointer, right?
This code runs on a low-end microcontroller and I think the idea is
that they have something specific at address zero, that they want to
access.
Not really sure about that, though (yet).
See my other post today that shows a more complete block of code that
hopefully does a slightly better job of showing what they are trying
to do.
Eric wrote:
On Wed, 19 Nov 2008 22:56:14 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
that code does not
depend on the representation of a null pointer (it is non-portable
because it involves de-referencing a null pointer).
Technically, it's dereferencing a zero pointer, which isn't guaranteed
to be the same thing as a null pointer, right?
(struct aStruct*)0 is a null pointer.
Eric wrote:
On Wed, 19 Nov 2008 12:07:07 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
The context of the code might explain the mystery, but it is hard to
guess any intent (at least I can't). Maybe there is a typo?
Good afternoon, Ben.
I apologize for not providing enough information... I was just trying
to minimize my post but I guess I minimized it too much. :-)
That's easy to do; it's still too minimal.
A more detailed block of code is:
bool result;
bool GetByte( char *theAddr, int theCount, char *theDest);
char Dest[ SOME_SIZE ];
result =
GetByte( ( char * ) &( ( struct aStruct * ) 0 )->Addr,
1, ( char * ) ( &Dest ) );
My question is... does the struct aStruct live at address 0, or does
address 0 contain a pointer to wherever aStruct actually lives? Seems
to me like the "&" ahead of ( ( struct aStruct * ) 0 )->Addr would
indicate the latter...
struct aStruct is not a particular struct that resides at a particular
location, it is a struct type. This code creates a null pointer of
type "struct aStruct*", and then dereferences it. The behavior is
undefined, which ends the discussion as far as the C standard is
concerned.
Pragmatically, on many implementations a null pointer points at an
actual memory location, one that strictly conforming C code cannot
access. The result of the above conversion is to treat that memory
location as if it was the start of an object of type "struct aStruct".
The rest of the expression creates a char* pointer that points at the
location at which the "Addr" member of such a struct would reside. We
don't know what GetByte() will do with that pointer - you haven't told
us what GetByte() does. I would guess that this call to GetBytes()
copies 1 byte from that location into Dest.
This is all highly non-portable.
In article <13********************************@4ax.com>,
Eric <an*****************@nospam.comwrote:
>Technically, it's dereferencing a zero pointer,
No...
>which isn't guaranteed to be the same thing as a null pointer, right?
Right.
But (type *)0 is always a null-pointer constant: if null pointers have
some odd representation, the compiler must recognise the construct and
generate that odd representation.
On such a system you may be able to get a "zero pointer" by casting
a non-constant zero or by setting a pointer using memcpy().
-- Richard
--
Please remember to mention me / in tapes you leave behind.
Eric <an*****************@nospam.comwrites:
On Wed, 19 Nov 2008 12:07:07 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
>>The context of the code might explain the mystery, but it is hard to guess any intent (at least I can't). Maybe there is a typo?
Good afternoon, Ben.
I apologize for not providing enough information... I was just trying
to minimize my post but I guess I minimized it too much. :-)
A more detailed block of code is:
bool result;
bool GetByte( char *theAddr, int theCount, char *theDest);
char Dest[ SOME_SIZE ];
result =
GetByte( ( char * ) &( ( struct aStruct * ) 0 )->Addr,
1, ( char * ) ( &Dest ) );
My question is... does the struct aStruct live at address 0, or does
address 0 contain a pointer to wherever aStruct actually lives? Seems
to me like the "&" ahead of ( ( struct aStruct * ) 0 )->Addr would
indicate the latter...
&((struct aStruct *)0)->Addr is the address where s.Addr would be
located, if s were located at address 0. (Note that -binds more
tightly than &.) On many machines this has the same numerical value as
offsetof(struct aStruct, Addr), and on such machines the offsetof()
macro might be implemented in this way. According to standard C,
however, this behavior can't be relied upon.
The point of offsetof(), of course, is to tell you where a certain
member is located with respect to the starting address of a struct. So given
struct foo { T1 a; T2 b; T3 c; } s;
then
(T2 *)(((char *)s) + offsetof(struct foo, b)) == &s.b
This is sometimes useful to allow an external function to modify
something inside a structure that it doesn't otherwise know anything
about.
Now back in your case, why the program would take that address, cast it
to char *, and pass it to a function, I don't know. I expect there is
non-portable platform-specific magic going on, and you might find better
information in a group dedicated to that platform. (Is this some sort
of embedded system?) ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
[...]
But (type *)0 is always a null-pointer constant: if null pointers have
some odd representation, the compiler must recognise the construct and
generate that odd representation.
[...]
Quibble: (type*)0 isn't a null pointer constant, unless "type" is
void. But 0 is a null pointer constant, and a null pointer constant
converted to a pointer type is guaranteed to yield a null pointer of
that type.
The language could just as easily defined the term "null pointer
constant" to include (type*)0 for any type "type", so this isn't a
huge deal.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Thu, 20 Nov 2008 10:34:12 -0800 (PST), jameskuyper
<ja*********@verizon.netwrote:
>We don't know what GetByte() will do with that pointer - you haven't told us what GetByte() does. I would guess that this call to GetBytes() copies 1 byte from that location into Dest.
I don't know what it does, either. I'm assuming that it does as you
speculate... copies 1 byte into Dest.
To answer Nate's question... yes, it's an embedded system, uses an
ARM7-based controller. There is quite a bit of, as you say,
non-portable platform-specific magic going on. I could, as you
suggest, find an ARM7 group and ask there, but mostly I'm just asking
from a standards-based perspective.
Thanks to all for your help...
Eric wrote:
[...]
bool result;
bool GetByte( char *theAddr, int theCount, char *theDest);
char Dest[ SOME_SIZE ];
result =
GetByte( ( char * ) &( ( struct aStruct * ) 0 )->Addr,
1, ( char * ) ( &Dest ) );
My question is... does the struct aStruct live at address 0, or does
address 0 contain a pointer to wherever aStruct actually lives? Seems
to me like the "&" ahead of ( ( struct aStruct * ) 0 )->Addr would
indicate the latter...
struct aStruct lives at add8uH(UOPUREi0-()_*$%*()U$(T(UO$TH(UH#%
That is, you get undefined behavior, since you're dereferencing a null
pointer. If you truely want an aStruct at address zero, do one of the
following, which I believe is merely implementation-defined:
typedef struct aStruct type;
unsigned long zero = 0;
type* at_zero = (type*) zero;
type* at_zero = ((type*) sizeof (type)) - 1;
You can't just calculate the address of one directly at zero because the
compiler specially handles conversion from a constant integer of value
zero, yielding a null pointer whose representation may NOT be zero. The
first approach converts a NON-constant integer (unsigned long to quiet any
pointless compiler warnings about zero not having enough bits for a
pointer, even though we're converting FROM the int, not to it), the second
finds the address of an aStruct at a non-zero address and then decrements
it back to zero.
If your system has a POINTER to an aStruct at address zero, change the
typedef to "typedef struct aStruct* type".
An easy way to find out whether you're doing it correctly is to print the
address with printf:
printf( "%p\n", (void*) at_zero ); //value of at_zero pointer
printf( "%p\n", (void*) &at_zero->member ); // address of member This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Randell D. |
last post by:
I have just recompiled, upgraded to PHP 4.3.4. As an exercise (and
curiosity) I've decided to test out PDF functions and got the test in the
PHP online manual working. I had one problem whereby...
|
by: thecrow |
last post by:
Alright, what the hell is going on here?
In the following code, I expect the printed result to be:
DEBUG: frank's last name is burns.
Instead, what I get is:
DEBUG: frank's last name is...
|
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: mike420 |
last post by:
I think everyone who used Python will agree that its syntax is
the best thing going for it. It is very readable and easy
for everyone to learn. But, Python does not a have very good
macro...
|
by: Martin Maney |
last post by:
In my copious spare time I've
been dabbling at getting a computerized version of a board game
working. After deciding that tk just made me want to vomit, and wx was
like swimming through...
|
by: Reed L. O'Brien |
last post by:
I see rotor was removed for 2.4 and the docs say use an AES module
provided separately... Is there a standard module that works alike or
an AES module that works alike but with better encryption?...
|
by: Ron_Adam |
last post by:
Ok... it's works! :)
So what do you think?
Look at the last stacked example, it process the preprocess's first in
forward order, then does the postprocess's in reverse order. Which
might be...
|
by: Dario |
last post by:
The following simple program behaves differently
in Windows and Linux .
#include <stdexcept>
#include <iostream>
#include <string>
using namespace std;
class LogicError : public logic_error {...
|
by: James Conrad StJohn Foreman |
last post by:
After 3 years of using DB2 on Linux, I'm leaving my current employers
to go work for a SQL Server shop instead. In order to find my
replacement, they're trying to put together a set of questions...
|
by: yogesh |
last post by:
char TCGI::x2c(char *what)
{
register char digit;
digit = (char) ((what >= 'A' ? ((what & 0xdf) - 'A')+10 :
(what - '0')));
digit *= (char) 16;
digit += (char) ((what >= 'A' ? ((what & 0xdf) -...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |