• C

C stucture copy

Struct {
             int a ;
            int b[100] ;
           char c[10 ];
       }   A , B ;

 A =B   ;

Question : A = B will copy all fields correctly ?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

ravenplConnect With a Mentor Commented:
Why will not You check that? But yes - as long there's no pointers it will copy it fine.
In other words it works like following
memcpy((void*)&A, (void*)&B, sizeof(B));
although the memcpy would also copy anything in any padding between fields, whereas the assignment is not guaranteed to do that.
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

gnu gcc in optimized code does memcpy.
It's the simplest implementation, so I would expect most compilers to do it that way.
But there is no requirement that it be implemented that way.
True - anyway the answer "Yes" stands here.
ozoConnect With a Mentor Commented:
Agreed.  A = B will copy all fields correctly.
Sorry, ravenpl, I see you made the  "works like" comment, which I now think you meant to be in terms of answering the question.
I had thought initially that it was said by the asker of the question as an inference from your answer.  In which case I wanted to clarify that the inference may not precisely follow.
>>>> But yes - as long there's no pointers it will copy it fine.

It also copies pointers correctly.

The issue with pointers is differently. It normally is no good idea to have the same pointer 'value' stored in more than one struct cause only one of these structs can be responsible for freeing the memory. So, in case you have

typedef struct tagA
     char * fname;
     char * lname;
} A;

     A a, b;
     a.fname = (char*)malloc(50);
     strcpy(a.fname, "John");
     a.lname = (char*)malloc(50);
     strcpy(a.lname, "Smith");
     b = a;

     printf("%s %s", b.fname, b.lname);

would print data that were already freed in memory and may contain rubbish because of that. And of course


would crash. To get out of that you always would need to allocate new memory to pointers after assignment:

     b = a;
     b.fname = (char*)malloc(50);
     strcpy(b.fname, a.fname);
     b.lname = (char*)malloc(50);
     strcpy(b.lname, a.lname);

what of course makes the assignment worthless in that sample.

Regards, Alex


Well, yes pointers are copied, but data pointed by pointer is not. That what I  meant. If You have pointers in structure, it's somehow unsafe, and You may think You should free data pointed by those pointers twice. But it's untrue - and unclear which copy should free the pointed data.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.