Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C stucture copy

Posted on 2007-12-03
10
Medium Priority
?
265 Views
Last Modified: 2010-04-15
Struct {
             int a ;
            int b[100] ;
           char c[10 ];
       }   A , B ;

 A =B   ;

Question : A = B will copy all fields correctly ?
                 
0
Comment
Question by:karana
  • 5
  • 4
10 Comments
 
LVL 43

Accepted Solution

by:
ravenpl earned 400 total points
ID: 20401483
Why will not You check that? But yes - as long there's no pointers it will copy it fine.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 20401485
In other words it works like following
memcpy((void*)&A, (void*)&B, sizeof(B));
0
 
LVL 85

Expert Comment

by:ozo
ID: 20401507
probably.
although the memcpy would also copy anything in any padding between fields, whereas the assignment is not guaranteed to do that.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 43

Expert Comment

by:ravenpl
ID: 20401517
gnu gcc in optimized code does memcpy.
0
 
LVL 85

Expert Comment

by:ozo
ID: 20401538
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.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 20401544
True - anyway the answer "Yes" stands here.
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 20401563
Agreed.  A = B will copy all fields correctly.
0
 
LVL 85

Expert Comment

by:ozo
ID: 20401595
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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20401804
>>>> 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;

     ...
     free(a.fname);
     free(a.lname);
     ...
     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

     free(b.fname);
     free(b.lname);

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


   



0
 
LVL 43

Expert Comment

by:ravenpl
ID: 20401836
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question