Solved

C stucture copy

Posted on 2007-12-03
10
240 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IIS Log files on Exchange 2013 server 6 184
What is atomic operation? 6 65
Test the speeds on my PC Drives 12 63
Windows Server 2012 R2 - connect to computer 13 40
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

808 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