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 .

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

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.

1 Like

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

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

Thanksfor your suggestion . What about allocating memory for the linked list .

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.

You might find info in there that will help.