Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #18109 > unrolled thread

struct looses initialization when passing to a function

Started byArifulHossain tuhin <etothepowerpi@gmail.com>
First post2012-03-02 07:17 -0800
Last post2012-03-03 14:29 -0500
Articles 8 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  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

#18109 — struct looses initialization when passing to a function

FromArifulHossain tuhin <etothepowerpi@gmail.com>
Date2012-03-02 07:17 -0800
Subjectstruct looses initialization when passing to a function
Message-ID<29955299.255.1330701473733.JavaMail.geo-discussion-forums@pbij6>
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.

[toc] | [next] | [standalone]


#18123

FromIan Collins <ian-news@hotmail.com>
Date2012-03-03 11:59 +1300
Message-ID<9rd1n8Ftd8U1@mid.individual.net>
In reply to#18109
On 03/ 3/12 04:17 AM, ArifulHossain tuhin wrote:
> 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.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#18125

FromIan Collins <ian-news@hotmail.com>
Date2012-03-03 12:01 +1300
Message-ID<9rd1puFtd8U2@mid.individual.net>
In reply to#18123
On 03/ 3/12 11:59 AM, Ian Collins wrote:

Nonsense, please ignore!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#18124

FromKaz Kylheku <kaz@kylheku.com>
Date2012-03-02 23:01 +0000
Message-ID<20120302145833.711@kylheku.com>
In reply to#18109
On 2012-03-02, ArifulHossain tuhin <etothepowerpi@gmail.com> wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#18144

Fromnick_keighley_nospam@hotmail.com
Date2012-03-03 06:29 -0800
Message-ID<23209526.614.1330784965988.JavaMail.geo-discussion-forums@vblb5>
In reply to#18109
On Friday, March 2, 2012 3:17:53 PM UTC, ArifulHossain tuhin wrote:

> 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

[toc] | [prev] | [next] | [standalone]


#18147

FromBarry Schwarz <schwarz45@yahoo.com>
Date2012-03-03 07:29 -0800
Message-ID<b8e08012-2d01-4c63-a099-e385d2bb5ed8@q12g2000yqg.googlegroups.com>
In reply to#18109
On Mar 2, 9:17 am, ArifulHossain tuhin <etothepowe...@gmail.com>
wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#18160

FromKeith Thompson <kst-u@mib.org>
Date2012-03-03 12:55 -0800
Message-ID<lnaa3x76bz.fsf@nuthaus.mib.org>
In reply to#18147
Barry Schwarz <schwarz45@yahoo.com> writes:
[...]
> 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".

> 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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#18155

FromShao Miller <sha0.miller@gmail.com>
Date2012-03-03 14:29 -0500
Message-ID<jitre3$o9d$1@dont-email.me>
In reply to#18109
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'?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web