Solved

C stucture copy

Posted on 2007-12-03
10
222 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 100 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 84

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
 
LVL 43

Expert Comment

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

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 43

Expert Comment

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

Assisted Solution

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now