Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: struct looses initialization when passing to a function |
| Date | 2012-03-03 14:29 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jitre3$o9d$1@dont-email.me> (permalink) |
| References | <29955299.255.1330701473733.JavaMail.geo-discussion-forums@pbij6> |
On 3/2/2012 10:17, ArifulHossain tuhin wrote:
> 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 forget to #include <stdlib.h>, you don't find out about it during
translation.
> 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'?
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
struct looses initialization when passing to a function ArifulHossain tuhin <etothepowerpi@gmail.com> - 2012-03-02 07:17 -0800
Re: struct looses initialization when passing to a function Ian Collins <ian-news@hotmail.com> - 2012-03-03 11:59 +1300
Re: struct looses initialization when passing to a function Ian Collins <ian-news@hotmail.com> - 2012-03-03 12:01 +1300
Re: struct looses initialization when passing to a function Kaz Kylheku <kaz@kylheku.com> - 2012-03-02 23:01 +0000
Re: struct looses initialization when passing to a function nick_keighley_nospam@hotmail.com - 2012-03-03 06:29 -0800
Re: struct looses initialization when passing to a function Barry Schwarz <schwarz45@yahoo.com> - 2012-03-03 07:29 -0800
Re: struct looses initialization when passing to a function Keith Thompson <kst-u@mib.org> - 2012-03-03 12:55 -0800
Re: struct looses initialization when passing to a function Shao Miller <sha0.miller@gmail.com> - 2012-03-03 14:29 -0500
csiph-web