?
Solved

C stucture copy

Posted on 2007-12-03
10
Medium Priority
?
266 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
Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

 
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

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

589 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