A
AjayB
Hi All,
Sorry for this silly question. But all the google answers I got were
vague and could not make up my mind.
My question is can the padding in structure vary between different
"instances" of that same structure??
For example I have a structure say
struct ABC{
int i;
char c;
flaot f;
double d;
int *p_i ;
};
Now lets say in one part of the source file i have an object
v_instance1:
<<file1.c>>>
struct ABC v_instance1 ;
....
....
some code...
....
and another object v_instance2:
<<file2.c>>
struct ABC v_instance2;
....
....
some code...
....
well now sizeof(v_instance1) always equal to sizeof(v_instance2) with
or without padding.
My "silly" doubts:
1)But is it possible that a compiler does the internal padding for
v_instance1 differently from v_instance2 and still satisfy the
sizeof(v_instance1)==sizeof(v_instance2) ??
IOW is the padding between elements of v_instance1 is exactly same as
padding between elements of v_instance2 such that my custom funtions
similar to memcpy() or memset() don't messup anything later(as the
compiler is not aware of what i intend to do unlike stdlib
functions)..?
Which paragraph of the C standard mentions this explicitly?
2)Also after doing, v_instance1 = v_instance2 , memcmp() can still
return Non-Zero right?
anyway to make sure the copy is done in such a way that memcmp()
return 0 and never go into undefined behavior
3)what is the best portable way to copy v_instance1 into v_instance2
(assume pointer members exist) if i plan to use memcmp() and memset()
later on those two variables ,without leading to undefined behaviour
or traps??
4)if v_instance1 and v_instance2 are not initialized, does expr
v_instance1=v_instance2 give undefined behavior?? for example let int
be member of the struct ,then while copying uninitialized int between
two instances can it not generate trap?? so then
v_instance1=v_instance2 is not a good thing todo always?
Thanks in advance..
Regards
Ajay
Sorry for this silly question. But all the google answers I got were
vague and could not make up my mind.
My question is can the padding in structure vary between different
"instances" of that same structure??
For example I have a structure say
struct ABC{
int i;
char c;
flaot f;
double d;
int *p_i ;
};
Now lets say in one part of the source file i have an object
v_instance1:
<<file1.c>>>
struct ABC v_instance1 ;
....
....
some code...
....
and another object v_instance2:
<<file2.c>>
struct ABC v_instance2;
....
....
some code...
....
well now sizeof(v_instance1) always equal to sizeof(v_instance2) with
or without padding.
My "silly" doubts:
1)But is it possible that a compiler does the internal padding for
v_instance1 differently from v_instance2 and still satisfy the
sizeof(v_instance1)==sizeof(v_instance2) ??
IOW is the padding between elements of v_instance1 is exactly same as
padding between elements of v_instance2 such that my custom funtions
similar to memcpy() or memset() don't messup anything later(as the
compiler is not aware of what i intend to do unlike stdlib
functions)..?
Which paragraph of the C standard mentions this explicitly?
2)Also after doing, v_instance1 = v_instance2 , memcmp() can still
return Non-Zero right?
anyway to make sure the copy is done in such a way that memcmp()
return 0 and never go into undefined behavior
3)what is the best portable way to copy v_instance1 into v_instance2
(assume pointer members exist) if i plan to use memcmp() and memset()
later on those two variables ,without leading to undefined behaviour
or traps??
4)if v_instance1 and v_instance2 are not initialized, does expr
v_instance1=v_instance2 give undefined behavior?? for example let int
be member of the struct ,then while copying uninitialized int between
two instances can it not generate trap?? so then
v_instance1=v_instance2 is not a good thing todo always?
Thanks in advance..
Regards
Ajay