Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Deep vs shallow coding in C

Status
Not open for further replies.

raj_rohit10

Advanced Member level 4
Full Member level 1
Joined
Jul 14, 2004
Messages
100
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Activity points
891
hi friends,
Is any term "Deep and shallow coding or copying" exist in c.If yes please explain the meaning of that.
Thanks in advance
 

Hi,
Deep and Shallow copying exists in C, let me explain,
imagine you have a structure which contains a member which is a pointer to a char,

struct mystruct
{
char * pointertodata;
};

now if u have 2 variables of this struct and you assign one to the other then the pointer in both the variables point to the same memory location, and this is known as shallow copying, now if u write a method which takes the two struct variables as parameter and you explictily allocate new memory for the destination variable and copy the required data then you say that you have performed deep copying, also deep copying is a must in the situations i decribed above i,e when you have pointers in structs etc...
hope that clears you doubts.
 

hi sahid,
tq for the reply,but the last pint is not much clear to me can you give me the piece of code which will explain deep and shallow???
 

Hi,
let me put some code,

// this is an example of shallow copying you should avoid if you have members
// which are pointers

struct Sample
{
char * ptrData;
};

main()
{
Sample s1,s2;

s1.ptrData = malloc(100); // allocate memory
strcpy(s1.ptrData,"hello world");

s1 = s2; // both s1.ptrData and s2.ptrData point to the same memory location
// due to shallow or member wise copying of data

free(s1.ptrData); // delete the memory pointed by s1.ptrData

strcpy(s2.ptrData,"god help this code"); // get ready for crash :)

}

// now a good practise to have a method which does deep copying when we have
// pointer as members

struct Sample
{
char * ptrData;
};

main()
{
Sample s1,s2;

s1.ptrData = malloc(100);
strcopy(s1.ptrData,"hello world");

CopyStructures(&s1,&s2)

free(s1.ptrData); // delete the memory pointed by s1.ptrData

strcopy(s2.ptrData,"smart code"); // will work no issues here since seperate
// memory
}

void CopyStructures(Sample * src,Sample * dest)
{
// allocate memory for destination
dest->ptrData = malloc(100);

// now copy data for src to destination

strcpy(dest->ptrData,src->ptrData)

}


so that deep copying, hope that help, i have explained you in context of structures and this holds good for c++ also when you use classes but i will not go into that now :)
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top