Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with a casted pointer

Reply
Thread Tools

problem with a casted pointer

 
 
praveen
Guest
Posts: n/a
 
      06-29-2007
-------------------------------My dud
program------------------------------------------
#include <stdioi.h>
#include <stddef.h>

typedef struct dummy{
int x, y, z;
}point;

int main() {
point a = {1, 2, 3};
int *p = (int *)(&a + offsetof(point, y);
int *q = (int *)(&a + offsetof(point, z);
printf("%d %d", *p, *q);
return 0;
}
-------------------------------------------------------------------------------------------------
my question is will *p and *q really point to (a.x) and (a.y) ?
i ran this program on DevC++ 4.9.9.2 and i'm getting some garbage
values as output!!!

 
Reply With Quote
 
 
 
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      06-29-2007
On 29 Jun, 12:10, praveen <(E-Mail Removed)> wrote:
> -------------------------------My dud
> program------------------------------------------
> #include <stdioi.h>
> #include <stddef.h>
>
> typedef struct dummy{
> int x, y, z;
>
> }point;
>
> int main() {
> point a = {1, 2, 3};
> int *p = (int *)(&a + offsetof(point, y);


&a is a pointer to point, so adding offsetof(point,y) will move it on
"n" points, not "n" bytes. You're now into Undefined Behaviour...

> int *q = (int *)(&a + offsetof(point, z);


As above.

> printf("%d %d", *p, *q);
> return 0;}
>
> -------------------------------------------------------------------------------------------------
> my question is will *p and *q really point to (a.x) and (a.y) ?


No.

> i ran this program on DevC++ 4.9.9.2 and i'm getting some garbage
> values as output!!!


I'm not surprised.

What are you actually trying to do? Why won't (e.g.) "&a.y" do what
you need?

By the way, it's _much_ better to cut and paste your real code, rather
than type up an approximation. "stdioi.h" doesn't exist and you have
missing parentheses in your code.

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-29-2007
praveen wrote:
> -------------------------------My dud
> program------------------------------------------
> #include <stdioi.h>


I wish, I *wish*, I *REALLY* *WISH* that when people
post "this is my code" they would actually post their code
and not some half-baked, typo-ridden approximation! You're
just introducing extra bugs that divert attention from the
problem that's actually bothering you; is that wise?

> #include <stddef.h>
>
> typedef struct dummy{
> int x, y, z;
> }point;
>
> int main() {
> point a = {1, 2, 3};
> int *p = (int *)(&a + offsetof(point, y);


Syntax error. If corrected, probably undefined behavior.

> int *q = (int *)(&a + offsetof(point, z);


Syntax error. If corrected, definitely undefined behavior.
(No, I won't explain why one is "probably" the other "definitely,"
because I'm angry with you. See above.)

> printf("%d %d", *p, *q);


Definitely undefined behavior (twice, or possibly thrice).
(No, I won't explain the "thrice." See above.)

> return 0;
> }
> -------------------------------------------------------------------------------------------------
> my question is will *p and *q really point to (a.x) and (a.y) ?


No, never.

> i ran this program on DevC++ 4.9.9.2 and i'm getting some garbage
> values as output!!!


You mean, after the required compiler diagnostics about
the syntax errors and the (probably) non-existent header?
If you ever expect to become a programmer, you *must* learn
how to ask questions without distorting the facts!

<Relenting> You have overlooked a basic fact about pointer
arithmetic: Adding an integer to a pointer "steps" it in whole
units of the pointed-to type. Now, ask yourself the question:
"What is the type of the pointer value `&a'?"

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid


 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      06-29-2007

"Eric Sosman" <(E-Mail Removed)> ha scritto nel messaggio news:(E-Mail Removed)...
> praveen wrote:
>> -------------------------------My dud
>> program------------------------------------------
>> #include <stdioi.h>

>
> I wish, I *wish*, I *REALLY* *WISH* that when people
> post "this is my code" they would actually post their code
> and not some half-baked, typo-ridden approximation! You're
> just introducing extra bugs that divert attention from the
> problem that's actually bothering you; is that wise?
>
>> #include <stddef.h>
>>
>> typedef struct dummy{
>> int x, y, z;
>> }point;
>>
>> int main() {
>> point a = {1, 2, 3};
>> int *p = (int *)(&a + offsetof(point, y);

>
> Syntax error. If corrected, probably undefined behavior.
>
>> int *q = (int *)(&a + offsetof(point, z);

>
> Syntax error. If corrected, definitely undefined behavior.
> (No, I won't explain why one is "probably" the other "definitely,"
> because I'm angry with you. See above.)


Because, if sizeof a.x == 1 and there's no padding, then
offsetof(point, y) is 1, so p points right past the end of the
fictitious one-element array of point a can be thought to belong
to. Right?

>> printf("%d %d", *p, *q);

>
> Definitely undefined behavior (twice, or possibly thrice).
> (No, I won't explain the "thrice." See above.)


Because it is implementation-defined wheter text files (including
stdout) need to end with a newline?

>> return 0;
>> }

>



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-29-2007
Army1987 wrote On 06/29/07 09:01,:
> "Eric Sosman" <(E-Mail Removed)> ha scritto nel messaggio news:(E-Mail Removed)...
>
>>praveen wrote:
>>
>>>-------------------------------My dud
>>>program------------------------------------------
>>>#include <stdioi.h>

>>
>> I wish, I *wish*, I *REALLY* *WISH* that when people
>>post "this is my code" they would actually post their code
>>and not some half-baked, typo-ridden approximation! You're
>>just introducing extra bugs that divert attention from the
>>problem that's actually bothering you; is that wise?
>>
>>
>>>#include <stddef.h>
>>>
>>>typedef struct dummy{
>>> int x, y, z;
>>>}point;
>>>
>>>int main() {
>>> point a = {1, 2, 3};
>>> int *p = (int *)(&a + offsetof(point, y);

>>
>> Syntax error. If corrected, probably undefined behavior.
>>
>>
>>> int *q = (int *)(&a + offsetof(point, z);

>>
>> Syntax error. If corrected, definitely undefined behavior.
>>(No, I won't explain why one is "probably" the other "definitely,"
>>because I'm angry with you. See above.)

>
>
> Because, if sizeof a.x == 1 and there's no padding, then
> offsetof(point, y) is 1, so p points right past the end of the
> fictitious one-element array of point a can be thought to belong
> to. Right?


Give that man a cigar!

>>> printf("%d %d", *p, *q);

>>
>> Definitely undefined behavior (twice, or possibly thrice).
>>(No, I won't explain the "thrice." See above.)

>
> Because it is implementation-defined wheter text files (including
> stdout) need to end with a newline?


Oops! I missed that one. Give *this* man a Bronx cheer!

--
(E-Mail Removed)
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      06-29-2007

"Eric Sosman" <(E-Mail Removed)> ha scritto nel messaggio news:1183128240.330121@news1nwk...
> Army1987 wrote On 06/29/07 09:01,:
>> "Eric Sosman" <(E-Mail Removed)> ha scritto nel messaggio news:(E-Mail Removed)...
>>
>>>praveen wrote:
>>>
>>>>-------------------------------My dud
>>>>program------------------------------------------
>>>>#include <stdioi.h>
>>>
>>> I wish, I *wish*, I *REALLY* *WISH* that when people
>>>post "this is my code" they would actually post their code
>>>and not some half-baked, typo-ridden approximation! You're
>>>just introducing extra bugs that divert attention from the
>>>problem that's actually bothering you; is that wise?
>>>
>>>
>>>>#include <stddef.h>
>>>>
>>>>typedef struct dummy{
>>>> int x, y, z;
>>>>}point;
>>>>
>>>>int main() {
>>>> point a = {1, 2, 3};
>>>> int *p = (int *)(&a + offsetof(point, y);
>>>
>>> Syntax error. If corrected, probably undefined behavior.
>>>
>>>
>>>> int *q = (int *)(&a + offsetof(point, z);
>>>
>>> Syntax error. If corrected, definitely undefined behavior.
>>>(No, I won't explain why one is "probably" the other "definitely,"
>>>because I'm angry with you. See above.)

>>
>>
>> Because, if sizeof a.x == 1 and there's no padding, then
>> offsetof(point, y) is 1, so p points right past the end of the
>> fictitious one-element array of point a can be thought to belong
>> to. Right?

>
> Give that man a cigar!
>
>>>> printf("%d %d", *p, *q);
>>>
>>> Definitely undefined behavior (twice, or possibly thrice).
>>>(No, I won't explain the "thrice." See above.)

>>
>> Because it is implementation-defined wheter text files (including
>> stdout) need to end with a newline?

>
> Oops! I missed that one. Give *this* man a Bronx cheer!


Ah... Then the "thrice" was... now I get it...

"Possibly" because we don't know that an implementation does not have a <stdioi.h> header which in turn includes <stdio.h>, so we
don't know for sure that we're calling a variadic function without
a prototype in scope?


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-29-2007
Army1987 wrote On 06/29/07 11:05,:
> "Eric Sosman" <(E-Mail Removed)> ha scritto nel messaggio news:1183128240.330121@news1nwk...
>
>>Army1987 wrote On 06/29/07 09:01,:
>>
>>>"Eric Sosman" <(E-Mail Removed)> ha scritto nel messaggio news:(E-Mail Removed)...
>>>
>>>
>>>>praveen wrote:
>>>>
>>>>
>>>>>-------------------------------My dud
>>>>>program------------------------------------------
>>>>>#include <stdioi.h>
>>>>
>>>> I wish, I *wish*, I *REALLY* *WISH* that when people
>>>>post "this is my code" they would actually post their code
>>>>and not some half-baked, typo-ridden approximation! You're
>>>>just introducing extra bugs that divert attention from the
>>>>problem that's actually bothering you; is that wise?
>>>>
>>>>
>>>>
>>>>>#include <stddef.h>
>>>>>
>>>>>typedef struct dummy{
>>>>> int x, y, z;
>>>>>}point;
>>>>>
>>>>>int main() {
>>>>> point a = {1, 2, 3};
>>>>> int *p = (int *)(&a + offsetof(point, y);
>>>>
>>>> Syntax error. If corrected, probably undefined behavior.
>>>>
>>>>
>>>>
>>>>> int *q = (int *)(&a + offsetof(point, z);
>>>>
>>>> Syntax error. If corrected, definitely undefined behavior.
>>>>(No, I won't explain why one is "probably" the other "definitely,"
>>>>because I'm angry with you. See above.)
>>>
>>>
>>>Because, if sizeof a.x == 1 and there's no padding, then
>>>offsetof(point, y) is 1, so p points right past the end of the
>>>fictitious one-element array of point a can be thought to belong
>>>to. Right?

>>
>> Give that man a cigar!
>>
>>
>>>>> printf("%d %d", *p, *q);
>>>>
>>>> Definitely undefined behavior (twice, or possibly thrice).
>>>>(No, I won't explain the "thrice." See above.)
>>>
>>>Because it is implementation-defined wheter text files (including
>>>stdout) need to end with a newline?

>>
>> Oops! I missed that one. Give *this* man a Bronx cheer!

>
>
> Ah... Then the "thrice" was... now I get it...
>
> "Possibly" because we don't know that an implementation does not have a <stdioi.h> header which in turn includes <stdio.h>, so we
> don't know for sure that we're calling a variadic function without
> a prototype in scope?


Give that man another cigar! (Y'know, you really
should cut down: Those things are bad for you.)

--
(E-Mail Removed)
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      06-29-2007
Eric Sosman wrote:
> Army1987 wrote On 06/29/07 09:01,:
>> "Eric Sosman" <(E-Mail Removed)> ha scritto nel messaggio news:(E-Mail Removed)...
>>
>>> praveen wrote:
>>>
>>>> -------------------------------My dud
>>>> program------------------------------------------
>>>> #include <stdioi.h>
>>> I wish, I *wish*, I *REALLY* *WISH* that when people
>>> post "this is my code" they would actually post their code
>>> and not some half-baked, typo-ridden approximation! You're
>>> just introducing extra bugs that divert attention from the
>>> problem that's actually bothering you; is that wise?
>>>
>>>
>>>> #include <stddef.h>
>>>>
>>>> typedef struct dummy{
>>>> int x, y, z;
>>>> }point;
>>>>
>>>> int main() {
>>>> point a = {1, 2, 3};
>>>> int *p = (int *)(&a + offsetof(point, y);
>>> Syntax error. If corrected, probably undefined behavior.
>>>
>>>
>>>> int *q = (int *)(&a + offsetof(point, z);
>>> Syntax error. If corrected, definitely undefined behavior.
>>> (No, I won't explain why one is "probably" the other "definitely,"
>>> because I'm angry with you. See above.)

>>
>> Because, if sizeof a.x == 1 and there's no padding, then
>> offsetof(point, y) is 1, so p points right past the end of the
>> fictitious one-element array of point a can be thought to belong
>> to. Right?

>
> Give that man a cigar!
>
>>>> printf("%d %d", *p, *q);
>>> Definitely undefined behavior (twice, or possibly thrice).
>>> (No, I won't explain the "thrice." See above.)

>> Because it is implementation-defined wheter text files (including
>> stdout) need to end with a newline?

>
> Oops! I missed that one. Give *this* man a Bronx cheer!
>

How about..

int *p = (int*)((char*)&a + offsetof(point,x));

...it seems to work but is it correct?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-29-2007
praveen wrote:
>
> #include <stdioi.h>
> #include <stddef.h>
>
> typedef struct dummy{
> int x, y, z;
> }point;
>
> int main() {
> point a = {1, 2, 3};
> int *p = (int *)(&a + offsetof(point, y);
> int *q = (int *)(&a + offsetof(point, z);
> printf("%d %d", *p, *q);
> return 0;
> }
>
> my question is will *p and *q really point to (a.x) and (a.y) ?
> i ran this program on DevC++ 4.9.9.2 and i'm getting some garbage
> values as output!!!


No. *p points to a.y, and *q points to a.z. Remove the casts,
they are unnecessary and only serve to suppress compiler warnings.
Use "int main(void)", a better expression. There is no such thing
as stdioi.h. You want stdio.h.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net



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

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      06-29-2007

"Joe Wright" <(E-Mail Removed)> ha scritto nel messaggio news:(E-Mail Removed)...
> Eric Sosman wrote:
>> Army1987 wrote On 06/29/07 09:01,:
>>> "Eric Sosman" <(E-Mail Removed)> ha scritto nel messaggio news:(E-Mail Removed)...
>>>
>>>> praveen wrote:
>>>>
>>>>> -------------------------------My dud
>>>>> program------------------------------------------
>>>>> #include <stdioi.h>
>>>> I wish, I *wish*, I *REALLY* *WISH* that when people
>>>> post "this is my code" they would actually post their code
>>>> and not some half-baked, typo-ridden approximation! You're
>>>> just introducing extra bugs that divert attention from the
>>>> problem that's actually bothering you; is that wise?
>>>>
>>>>
>>>>> #include <stddef.h>
>>>>>
>>>>> typedef struct dummy{
>>>>> int x, y, z;
>>>>> }point;
>>>>>
>>>>> int main() {
>>>>> point a = {1, 2, 3};
>>>>> int *p = (int *)(&a + offsetof(point, y);
>>>> Syntax error. If corrected, probably undefined behavior.
>>>>
>>>>
>>>>> int *q = (int *)(&a + offsetof(point, z);
>>>> Syntax error. If corrected, definitely undefined behavior.
>>>> (No, I won't explain why one is "probably" the other "definitely,"
>>>> because I'm angry with you. See above.)
>>>
>>> Because, if sizeof a.x == 1 and there's no padding, then
>>> offsetof(point, y) is 1, so p points right past the end of the
>>> fictitious one-element array of point a can be thought to belong
>>> to. Right?

>>
>> Give that man a cigar!
>>
>>>>> printf("%d %d", *p, *q);
>>>> Definitely undefined behavior (twice, or possibly thrice).
>>>> (No, I won't explain the "thrice." See above.)
>>> Because it is implementation-defined wheter text files (including
>>> stdout) need to end with a newline?

>>
>> Oops! I missed that one. Give *this* man a Bronx cheer!
>>

> How about..
>
> int *p = (int*)((char*)&a + offsetof(point,x));
>
> ..it seems to work but is it correct?


But what's so wrong with good ol' &a.x?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with a casted pointer Raj Pashwar C Programming 4 10-10-2011 06:29 PM
Object pointer casted to int* , wont increment with ++ Ravi C++ 11 12-02-2008 11:19 AM
Problem on using a casted void pointer kkirtac C Programming 10 09-24-2007 09:41 AM
Safely deleting a type-casted pointer Pat C++ 1 02-22-2007 07:33 PM
How to get the class of a casted null reference. Cram TeXeD Java 4 02-07-2005 01:25 AM



Advertisments