Memory layout of C struct [duplicate] - c

This question already has answers here:
Is the memory allocated for struct members continguous? What if a struct member is an array?
(3 answers)
Closed 3 years ago.
Can I be sure that the following code will work on all platforms?
struct example{
int a;
int b;
} example;
*((int*)(((void*)&example) + sizeof(int))) = 33;
This should change the value of (b) inside (example) to 33.

It will not for sure.
&example + sizeof(int) this operation moves the pointer sizeof(int) * sizeof(example) bytes ahead.
And this line will not compile at all
*(&example + sizeof(int)) = 33;
To know the offset of the particular field in the struct or union use offsetof
http://man7.org/linux/man-pages/man3/offsetof.3.html

Related

Inter-nested struct type in C [duplicate]

This question already has answers here:
Structs that refer to each other
(4 answers)
Closed 2 years ago.
I want to create 2 structs that are nested inside each other, but C won't allow this as when I define the first struct, the second struct is still not defined. How to solve this?
code
what you need to do is add pointers to refer to the other structs :
struct y;
struct x {
struct y * yy;
};
struct y {
struct x * xx;
};

Why am I not getting a memory exception here [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 6 years ago.
Suppose I have this data structure
struct foo{
int a;
int b;
};
Now I would like to create an array of 2 items . So I do this
struct foo* farry = (struct foo*) malloc(2 * sizeof(struct foo));
Please correct me if I am wrong the above would create 2 slots having foo structure default initialized ? Is that correct ? If so then If i do this
struct foo* farry = (struct foo*) malloc(2 * sizeof(struct foo));
farry[0].a =1;
farry[1].a =2;
farry[2].a =3;
farry[3].a =4;
farry[4].a =5;
for(i=0 ; i<=4 ; i++)
{
printf("Value %d \n",farry[i].a );
}
Then why does at farry[2].a =3 it not tell me that a memory error occured. Instead it simply prints 1,2,3,4,5
You simply access uncontrolled locations in your heap which is in the program and user space. Nothing wrong from this point of view so no memory error.
Simply you're messing with other static variables in the heap. Should you mess too much like this your program would crash (for example if your messing for some reason reaches the program stack or a variable being messed will make a loop wreak havoc). Yes C is wild is this respect.

When to use pointers in C [duplicate]

This question already has answers here:
C best practices, stack vs heap allocation
(4 answers)
Closed 6 years ago.
I have a struct like this:
typedef struct {
int hi;
} my_struct;
Is there an advantage in using this:
my_struct *test = malloc(sizeof(my_struct));
test->hi = 1;
Instead of this:
my_struct test;
test.hi = 1;
No, usually it's quite the opposite. If you can use the format to satisfy your requrement
my_struct test;
test.hi = 1;
then it's always better, less overhead in runtime. There's no advantage in general of using memory allocator functions, when you can do without them.

Ambiguity calculating size of structure [duplicate]

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 9 years ago.
I am using code::blocks 12.11 (gcc) on a 32 bit OS. I have the following structure:
struct node
{
int a;
float b;
char d;
struct node* c;
}
s1;
now individually,
sizeof(int); sizeof(float); sizeof(char);
gives output 4 4 1 bytes respectively.
so i calculate size of structure as 13 bytes.
But the following
sizeof(s1); or sizeof(struct node);
gives output 16bytes.
I am unable to figure out why this is so. Please help me out here.
Thanks.
The struct rounded to 32-bit (4-byte) chunks. If you'd added 3 more chars, it would be the same size.

Offset of a member variable inside a struct [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
offsetof at compile time
How can I find the offset of a member within a struct in C? For example, how can I find the offset of t in this struct:
struct test
{
int a;
int b;
struct test* t;
int c;
};
Use the offsetof() macro from stddef.h: offsetof(struct test, t). (ideone example)

Resources