Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer to Structure Casts

Reply
Thread Tools

Pointer to Structure Casts

 
 
caleb.vandyke@gmail.com
Guest
Posts: n/a
 
      09-21-2005
I am working with some code that is doing some pointer to structure
casts and I can't figure out how the cast is being done. Here is
basically the code.

#include <stdio.h>
#include <stdlib.h>

typedef struct diffRecord
{
struct record *next[1];
} DiffRecord;

typedef struct record
{
struct record *next;
int value;
} Record;

int main(int argc, char* argv[])
{
Record *rec1 = malloc(sizeof(Record));
rec1->value = 5;

Record *rec2 = malloc(sizeof(Record));
rec2->value = 6;

rec1->next = rec2;
rec2->next = NULL;

DiffRecord *diffRec = ((DiffRecord*)rec1)->next[0];
Record *testRec = (Record*)diffRec;

printf("The value is: %i\n", testRec->value); //prints 6

free(rec1);
free(rec2);

return 0;
}

How is the pointer contained in record being coerced into the array in
the diffRecord structure?

Caleb Van Dyke

 
Reply With Quote
 
 
 
 
Alexei A. Frounze
Guest
Posts: n/a
 
      09-21-2005
<> wrote in message
news: ups.com...
> I am working with some code that is doing some pointer to structure
> casts and I can't figure out how the cast is being done. Here is
> basically the code.

....
> typedef struct diffRecord
> {
> struct record *next[1];
> } DiffRecord;
>
> typedef struct record
> {
> struct record *next;
> int value;
> } Record;
>
> int main(int argc, char* argv[])
> {
> Record *rec1 = malloc(sizeof(Record));
> rec1->value = 5;
>
> Record *rec2 = malloc(sizeof(Record));
> rec2->value = 6;
>
> rec1->next = rec2;
> rec2->next = NULL;
>
> DiffRecord *diffRec = ((DiffRecord*)rec1)->next[0];
> Record *testRec = (Record*)diffRec;
>
> printf("The value is: %i\n", testRec->value); //prints 6

....
> How is the pointer contained in record being coerced into the array in
> the diffRecord structure?


Here:
struct record *next[1];
struct record *next;
One struct contains array of 1 pointer.
The other contains 1 pointer, w/o being part of any array.
But, they both contain a pointer anyway, so there's no problem here. It's
not much different from the case when you have an array of 1 int and just 1
int, simply the type is different.
So, both structs contain in their beginning a pointer (or array of one
pointer -- doesn't matter how you think of it, in this particular case it's
essentially the same thing and from the memory's standpoint the two things
are equivalent)...
I can only say that this is a bit odd code... I'd expect this way of casting
Record *testRec = (Record*)diffRec;
rather than this
DiffRecord *diffRec = ((DiffRecord*)rec1)->next[0];
But you have both. What for?

Alex


 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      09-21-2005
On 20 Sep 2005 17:11:02 -0700, ""
<> wrote:

>I am working with some code that is doing some pointer to structure
>casts and I can't figure out how the cast is being done. Here is
>basically the code.


The cast is being done by the statements that contain the casts. What
can't you figure out?
>
>#include <stdio.h>
>#include <stdlib.h>
>
>typedef struct diffRecord
>{
> struct record *next[1];
>} DiffRecord;
>
>typedef struct record
>{
> struct record *next;
> int value;
>} Record;
>
>int main(int argc, char* argv[])
>{
> Record *rec1 = malloc(sizeof(Record));
> rec1->value = 5;
>
> Record *rec2 = malloc(sizeof(Record));


Many C compilers do not allow definitions to follow executable
statements within a block. (It did not become a standard feature
until C99 and there aren't many C99 compilers in use.) By not putting
all your definitions at the start of the block you reduce the number
of people in the group who can help you.

> rec2->value = 6;
>
> rec1->next = rec2;
> rec2->next = NULL;


You now have a linked list with two elements.

>
> DiffRecord *diffRec = ((DiffRecord*)rec1)->next[0];


Take the value in rec1 which has the type pointer to Record and
convert it by whatever means is appropriate for your system to point
to the same address but with type pointer to DiffRecord. Since next
and next[] are the first members of their respective structs, you are
guaranteed there is no padding before them so this code will pick up
the pointer that has offset 0 into the struct.

This is a bad idea. It is probable that Record and DiffRecord have
the same alignment but why run the risk. If the value in rec1 is not
suitably aligned for a DiffRecord, this would invoke undefined
behavior.

> Record *testRec = (Record*)diffRec;


Do the same thing in the opposite direction.

Since the standard requires all pointers to struct to have the same
size and representation, the value will always be the same address but
the type is changing.

>
> printf("The value is: %i\n", testRec->value); //prints 6
>
> free(rec1);
> free(rec2);
>
> return 0;
>}
>
>How is the pointer contained in record being coerced into the array in
>the diffRecord structure?


It isn't. rec1 is being treated as if it points to a DiffRecord.
next[0] is a pointer which occupies the beginning of the struct. rec1
actually points to a Record. "It just so happens" that the beginning
of this struct is also occupied by a pointer of the same type.

It's still a lousy idea. If you change value from an int to a double
you would invoke undefined behavior on my system.

>
>Caleb Van Dyke



<<Remove the del for email>>
 
Reply With Quote
 
caleb.vandyke@gmail.com
Guest
Posts: n/a
 
      09-22-2005
Alexei A. Frounze wrote:
> <> wrote in message
> news: ups.com...
> > I am working with some code that is doing some pointer to structure
> > casts and I can't figure out how the cast is being done. Here is
> > basically the code.

> ...
> > typedef struct diffRecord
> > {
> > struct record *next[1];
> > } DiffRecord;
> >
> > typedef struct record
> > {
> > struct record *next;
> > int value;
> > } Record;
> >
> > int main(int argc, char* argv[])
> > {
> > Record *rec1 = malloc(sizeof(Record));
> > rec1->value = 5;
> >
> > Record *rec2 = malloc(sizeof(Record));
> > rec2->value = 6;
> >
> > rec1->next = rec2;
> > rec2->next = NULL;
> >
> > DiffRecord *diffRec = ((DiffRecord*)rec1)->next[0];
> > Record *testRec = (Record*)diffRec;
> >
> > printf("The value is: %i\n", testRec->value); //prints 6

> ...
> > How is the pointer contained in record being coerced into the array in
> > the diffRecord structure?

>
> Here:
> struct record *next[1];
> struct record *next;
> One struct contains array of 1 pointer.
> The other contains 1 pointer, w/o being part of any array.
> But, they both contain a pointer anyway, so there's no problem here. It's
> not much different from the case when you have an array of 1 int and just 1
> int, simply the type is different.
> So, both structs contain in their beginning a pointer (or array of one
> pointer -- doesn't matter how you think of it, in this particular case it's
> essentially the same thing and from the memory's standpoint the two things
> are equivalent)...
> I can only say that this is a bit odd code... I'd expect this way of casting
> Record *testRec = (Record*)diffRec;
> rather than this
> DiffRecord *diffRec = ((DiffRecord*)rec1)->next[0];
> But you have both. What for?
>
> Alex


This code actually is from another author. Their code looks something
like this:

#include <stdio.h>
#include <stdlib.h>

typedef struct diffRecord
{
struct diffRecord *next[2];
} DiffRecord;

typedef struct record
{
struct record *next;
struct record *previous;
int value;
} Record;

static void printValue(int index, Record *rec)
{
DiffRecord *diffRec = (DiffRecord*)rec;
Record *testRec = (Record*)diffRec->next[index];
printf("Value: %i\n", testRec->value);
}

int main(int argc, char* argv[])
{
Record *rec1 = malloc(sizeof(Record));
Record *rec2 = malloc(sizeof(Record));
Record *rec3 = malloc(sizeof(Record));

rec1->value = 5;
rec2->value = 6;
rec3->value = 7;

rec1->next = rec2;
rec1->previous = NULL;

rec2->previous = rec1;
rec2->next = rec3;

rec3->previous = rec2;
rec3->next = NULL;

printValue(0, rec2);
printValue(1, rec2);

free(rec3);
free(rec2);
free(rec1);

return 0;
}

Output:
Value: 7
Value: 5

I think their intention was for the parameter index in printValue() to
be some sort of selector for the pointers in the rec parameter. The
haven't seen a cast like this before but I am assuming that this works
becuase the first two parameters of Record are pointers?

If say Record looks like this:

typedef struct record
{
struct record *next;
int value;
struct record *previous;
} Record;

and I cast struct record to a struct diffRecord would I get some
undefined behaviour trying to access next[1] field of diffRecord?

Thanks for the help,
Caleb Van Dyke

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-22-2005
On 21 Sep 2005 17:27:45 -0700, ""
<> wrote:

snip ~60 lines of code you now tell us is irrelevant. Please trim
your posts.

>
>This code actually is from another author. Their code looks something
>like this:
>
>#include <stdio.h>
>#include <stdlib.h>
>
>typedef struct diffRecord
>{
> struct diffRecord *next[2];


next[0] will be followed immediately by next[1].

>} DiffRecord;
>
>typedef struct record
>{
> struct record *next;
> struct record *previous;


But there is no guarantee that previous immediately follows next in
the memory occupied buy such a structure.

> int value;
>} Record;
>
>static void printValue(int index, Record *rec)
>{
> DiffRecord *diffRec = (DiffRecord*)rec;
> Record *testRec = (Record*)diffRec->next[index];
> printf("Value: %i\n", testRec->value);


This is still crappy code that could be solved without risk of
undefined behavior by code like
testRec = index ? rec->previous : rec->next;

>}
>
>int main(int argc, char* argv[])
>{
> Record *rec1 = malloc(sizeof(Record));
> Record *rec2 = malloc(sizeof(Record));
> Record *rec3 = malloc(sizeof(Record));
>
> rec1->value = 5;
> rec2->value = 6;
> rec3->value = 7;
>
> rec1->next = rec2;
> rec1->previous = NULL;
>
> rec2->previous = rec1;
> rec2->next = rec3;
>
> rec3->previous = rec2;
> rec3->next = NULL;
>
> printValue(0, rec2);
> printValue(1, rec2);
>
> free(rec3);
> free(rec2);
> free(rec1);
>
> return 0;
>}
>
>Output:
>Value: 7
>Value: 5
>
>I think their intention was for the parameter index in printValue() to
>be some sort of selector for the pointers in the rec parameter. The
>haven't seen a cast like this before but I am assuming that this works
>becuase the first two parameters of Record are pointers?
>
>If say Record looks like this:
>
>typedef struct record
>{
> struct record *next;
> int value;
> struct record *previous;
>} Record;
>
>and I cast struct record to a struct diffRecord would I get some
>undefined behaviour trying to access next[1] field of diffRecord?


Absolutely.


<<Remove the del for email>>
 
Reply With Quote
 
caleb.vandyke@gmail.com
Guest
Posts: n/a
 
      09-22-2005
Barry Schwarz wrote:
> On 21 Sep 2005 17:27:45 -0700, ""
> <> wrote:
>
> snip ~60 lines of code you now tell us is irrelevant. Please trim
> your posts.
>


Sorry it's so long. I was trying to be concise. Thanks for answering my
question, but why do you think I am saying this is irrelevant? I was
trying to figure out the behavior of structure members when they are
cast to another structure. I only posted the revised code to show why
this weird cast was being done.

Caleb Van Dyke

 
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
Pointer casts for OOP Sean Hamilton C Programming 2 08-18-2011 02:17 PM
Union and pointer casts? Jef Driesen C Programming 13 04-08-2011 12:35 AM
Is this portable? [static pointer casts, char* arithmetic] SG C++ 8 04-15-2009 09:45 AM
pointer casts(newbie) eminhanif@googlemail.com C++ 8 02-10-2009 10:05 AM
pointer casts and the heap Wolfgang Jeltsch C++ 7 08-31-2003 02:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57