struct looses initialization when passing to a function

  • Thread starter ArifulHossain tuhin
  • Start date
A

ArifulHossain tuhin

i have a stuct like following:
typedef struct{
char static[10];
int a;
int b;
}my_type;

now i've initialized it like following:

my_type * s;

s = (my_type *)malloc(sizeof(my_type));
memmove(a->static,buf,len);
s->a = 1;
s->b = 2;
myfunc((void *)&s);


void myfun(void ** data);

inside myfun i retain initialization for the static[10] field. but i loose other initializations of field a and field b.

thanks in advance.
 
I

Ian Collins

i have a stuct like following:
typedef struct{
char static[10];
int a;
int b;
}my_type;

now i've initialized it like following:

my_type * s;

s = (my_type *)malloc(sizeof(my_type));
memmove(a->static,buf,len);
s->a = 1;
s->b = 2;
myfunc((void *)&s);


void myfun(void ** data);

inside myfun i retain initialization for the static[10] field. but i loose other initializations of field a and field b.


Your question isn't very clear and you appear to be writing C rather
than C++. You might be better of asking on comp.lang.c and providing
some more details, such as the definition of myfun.
 
K

Kaz Kylheku

i have a stuct like following:
typedef struct{
char static[10];

Since static is a reserved keyword, there is no way you compiled any
of this.

Post a complete example that compiles and reproduces the problem
that you perceive.
 
G

Guest

i have a stuct like following:
typedef struct{
char static[10];
int a;
int b;
}my_type;

now i've initialized it like following:

my_type * s;

s = (my_type *)malloc(sizeof(my_type));
memmove(a->static,buf,len);

like Kaz I don't believe you compiled this. What is 'a'? and buf and len?
s->a = 1;
s->b = 2;
myfunc((void *)&s);


void myfun(void ** data);

inside myfun i retain initialization for the static[10] field. but i loose other initializations of field a and field b.

post a short, complete, compliable program that illustrates your problem
 
B

Barry Schwarz

i have a stuct like following:
typedef struct{
char static[10];
int a;
int b;

}my_type;

now i've initialized it like following:

my_type * s;

s = (my_type *)malloc(sizeof(my_type));
memmove(a->static,buf,len);
s->a = 1;
s->b = 2;
myfunc((void *)&s);

void myfun(void ** data);

inside myfun i retain initialization for the static[10] field. but i loose other initializations of field a and field b.

Firstly, you need to cut and paste the code in question, not retype
it. Obviously the first argument to memmove should be s->static, not
a->static. We have no idea how many other transcription errors exist.

Secondly, you need to show the code that is experiencing the problem.
We have no idea what the code is for myfunc.

Thirdly, those of who wish to examine the problem in detail often
benefit from a compilable snippet that exhibitis the problem

And regarding what you did post, you seem to be working under the
micsonception that void** is a generic pointer to pointer. It most
definitely is not. It is explicitly a pointer to void and may have a
completely different representation and alignment requirements than
void* which is what you send it. But until we see how myfunc uses it,
we are just guessing.
 
S

Shao Miller

i have a stuct like following:
typedef struct{
char static[10];

Using 'static' as an identifier there has already been mentioned by others.
int a;
int b;
}my_type;

now i've initialized it like following:

my_type * s;

s = (my_type *)malloc(sizeof(my_type));

Please do not cast the return-value of a call to 'malloc'. It's not
needed because 'malloc' returns a 'void *' value, which can be
implicitly converted to any pointer-to-object type, which includes
'my_type *'. Casting the return-value also increases the chance that if
you don't find out about it during said:
memmove(a->static,buf,len);

Your 'a->static' typo already mentioned by others.
s->a = 1;
s->b = 2;
myfunc((void *)&s);


void myfun(void ** data);

'&s' has type 'my_type **'. Suppose that the 'my_type *' type (which is
the pointed-to type) is 4 bytes and has an alignment requirement of 4
bytes. Suppose 'void *' has 8 bytes and has an alignment requirement of
8 bytes.

Your call is similar to doing:

void ** data = (void **) &s;

Now if '&s' pointed to address '4' (satisfying the alignment for
'my_type *') but 'data' is expected to point to a multiple of '8'
(satisfying the alignment for 'void *'), then this behaviour is undefined.
inside myfun i retain initialization for the static[10] field. but i loose other initializations of field a and field b.

How are you attempting to access the original object that you allocated
storage for with 'malloc' from within the body of 'myfun'?
 
K

Keith Thompson

Barry Schwarz said:
And regarding what you did post, you seem to be working under the
micsonception that void** is a generic pointer to pointer. It most
definitely is not. It is explicitly a pointer to void and may have a

You mean "pointer to pointer to void".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top