Deep copy of structure in C


 
Thread Tools Search this Thread
Top Forums Programming Deep copy of structure in C
# 1  
Old 06-19-2014
Deep copy of structure in C

Hi ,

I have a scenario where i need to copy the iter to another local variable , where iter is of type MCC_T_SYS_ADDRINFO *iter .

Code:
struct addrinfo {
    int              ai_flags;
    int              ai_family;
    int              ai_socktype;
    int              ai_protocol;
    socklen_t        ai_addrlen;
    struct sockaddr *ai_addr;
    char            *ai_canonname;
    struct addrinfo *ai_next;
};


typedef struct addrinfo MCC_T_SYS_ADDRINFO;

MCC_T_SYS_ADDRINFO *iter = ipc->addr_ptr;

Now i need to copy the *iter to a local variable which of type MCC_T_SYS_ADDRINFO *localiter

how do i achieve it ?

Please help me in this


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 06-19-2014 at 05:22 AM.. Reason: code tags
# 2  
Old 06-19-2014
There's a couple things that make it hard to just give you a suggested solution...

First, the structure you're talking about it generally not just one structure. It's a linked list of structures, each one describing one address info "chunk" of information. So do you just want to copy one link in the chain, or the whole thing?

Second, a "struct sockaddr" might seem like a simple, friendly little thing. But it's not. It's really a placeholder structure for another structure, usually either a "struct sockaddr_in" or a "struct sockaddr_in6". So copying it requires you to do more work than you would have to do for a static structure with a consistent format.

And third, which is really minor compared to the other two, is that even if you just one this one particular chain in the link, some of the fields are pointers. So the logic just has to allocate separate chunks of memory for the various pieces.

Maybe if you explain a bit more about what you're trying to do it would help? Obviously this has something to do with some sort of host/service lookup and scanning through the results. But that doesn't narrow things down very much.
This User Gave Thanks to cnamejj For This Post:
# 3  
Old 06-19-2014
For me whole linked list has to be copied .

Yes for third point ,
We need to allocate different chunks of memory.

Additional information :

Actually my problem is the variable iter in the code gets corrupted , Using the vaue of iter we try to connect to some host using a
fuction
mcc_ipc_connect_a(iter-ai_family,SOCK_STREAM, 0, &conn->socket,
(MCC_T_SYS_SOCKADDR*)(iter->ai_addr), iter->ai_addrlen);

Here due to whatever reasons , the connection doesnt establish and later result in retry for connection and timesout. This corrupts iter structure , which will be used iterate . As this is corrupted and is invalid structure , the application results in core dump.
so we are thinking of having a local variable and pass to the to function which actually corrupts data , So the iter is not corrupted rather the local variable which we used is corrupted. So after the timeout based on the value of iter , the iteration is done and will be done properly . So this is my requirement , probably we may not require whole linked list to be copied to the local variable in case , please share your ideas on the same and how can we achieve it
# 4  
Old 06-19-2014
Code:
MCC_T_SYS_ADDRINFO *iter = ipc->addr_ptr;
MCC_T_SYS_ADDRINFO *newloc;

copy( &iter, &newloc);

copy(MCC_T_SYS_ADDRINFO **src, MCC_T_SYS_ADDRINFO **dst)
{
  while( *src != NULL )
  {
    //1. Allocate Memeory to dst
	(*src)->ai_flags = (*dst)->ai_flag;
	.
	.
	.
	//2. Allocate Memory to ai_canonname of dst
	strcpy( (*src)->ai_canonname, (*dst)->ai_canonname);
	
	if( (*src)->ai_next != NULL ){
		(*src) = (*src)->ai_next;
		(*dst) = (*dst)->ai_next;
	}
  }
}

# 5  
Old 06-19-2014
Thanksfor your suggestion . What about allocating memory for the linked list .
# 6  
Old 06-19-2014
If it helps, there's a program called "quipi" in this GitHub repo (mine) that does address lookups and runs through the resulting records to pull out data.

https://github.com/cnamejj/cli-tools

You might find info in there that will help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy all structure folder create last day?

HI All, please help , i got same case . i want copy folder and directory create yesterday. for sample below : drwxr-xr-x 4 apps apps 33 Nov 23 04:00 xxxxxx@gmai.com drwxr-xr-x 4 apps apps 33 Nov 23 04:00 yyyyyyy@gmail.com drwxr-xr-x 4 apps apps 33 Nov 24 04:00... (2 Replies)
Discussion started by: fajar_3t3
2 Replies

2. Shell Programming and Scripting

Need to copy a directory structure from one server to another

Hello All, I have got a dev server and a production server will call it as D server and P server. I have a dir structure in my D server and i need to create the same dir structure in P server also using a shell script. If i do a find . in my D server, i am getting an o/p like :- . ./vio... (9 Replies)
Discussion started by: Renjesh
9 Replies

3. Shell Programming and Scripting

Copy files from input file with dir structure

hi, I want to copy files from source directory based on input file (or output of previous command) and i want to have the SAME DIRECTORY STRUCTURE. Note that i will have other files and directories which i dont want to copy to destination. For example, dir source has following content:... (22 Replies)
Discussion started by: dragon.1431
22 Replies

4. UNIX for Advanced & Expert Users

Copy Structure excluding some folders

Hi I want to copy the structure from one place to another. -> cd /hol/; -> find . -type d | cpio -pvdm /abc/cat; while copying the structure I want to exclude some directories like test1 and Test. I have read somewhere that this can be done with -prune option. Could anyone... (2 Replies)
Discussion started by: soumodeep123
2 Replies

5. Shell Programming and Scripting

Copy a file by creating folder structure in destination as in Souce

Hello, i am having a source directory which consist of multiple sub directories and my destination folder is a empty directory. if try to copy a file source->test->1.txt from source to destination test2 using the commaind. cp source/test/1.txt desti/ It will copy the 1.txt under desti... (1 Reply)
Discussion started by: tsaravanan
1 Replies

6. Shell Programming and Scripting

Copy only folder structure ?

Hello, I am not sure how feasible is it, but trying to copy/create the folders from one box to another. I dont want to copy the content of folder otherwise it'd be too much of data.. The folder structure is quite complex (in deep hierarchy) and its a big effort manually .. Please... (5 Replies)
Discussion started by: navsha
5 Replies

7. Shell Programming and Scripting

copy directory structure to a system on the network

I am trying to write a script which has to copy the directory structure from my system to another system on the network. But I dont want the files to be copied. I think I have to start with copying all subdirectories names in a directory to a system on the network. Here's the case: Source... (1 Reply)
Discussion started by: firefox211
1 Replies

8. Shell Programming and Scripting

Recursion to Copy a Directory structure

I have the below code to recurse through a given file path. I am able to go to the leaf level once recursively but traversing out is not possible as the required varaibles don't have the values on return. Both typeset and local don't work for my variable definitions. I have the o/p of the sample... (2 Replies)
Discussion started by: avrkiran
2 Replies

9. UNIX for Dummies Questions & Answers

copy files with directory structure

i have a text file as. /database/sp/NTR_Update_Imsi_List.sql /database/sp/NTR_Update_Imsi_Range_List.sql /database/sp/NTR_Vlr_Upload.sql /database/tables/StatsTables.sql /mib/ntr.mib /mib/ntr.v2.mib /scripts/operations/ntr/IMSITracer.ph /scripts/operations/ntr/IMSITracer.pl ... (3 Replies)
Discussion started by: adddy
3 Replies

10. UNIX for Dummies Questions & Answers

any idea to copy directory structure

I want to copy the durectory structure having subdirectories in it ... would be greatful if anyone could let me know .. thanks in advance (6 Replies)
Discussion started by: myelvis
6 Replies
Login or Register to Ask a Question