Solved

C stucture copy

Posted on 2007-12-03
10
213 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

743 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