I am completely lost on why memcpy or memmove is not working for me.
I am trying to copy a array of struct (joblist) to another array of same struct (Q) to use for data manipulation, while keeping my original array intact.
I have spent a lot of time on SO and google troubleshooting this and I cannot find out why it won't work.
Q = new AdjacencyList[numjobs + 1]; //create a queue
// Q = joblist;
memmove(&Q, &joblist, sizeof(AdjacencyList) );
Q[0].jobnum = 30;
I tried to just make Q = joblist, but then when I changed a value in one, it changed in both.
struct AdjacencyList {
NodeType state; // what is present state of job?
NodePath path; // is job part of critical path?
int jobnum;
int edges;
int tocount;
int fromcount;
int to[50];
int from[50];
};
AdjacencyList *joblist;
AdjacencyList *Q;
This is the struct I created.
*** Error in `p5.out': corrupted double-linked list: 0x0989e8c0 ***
======= Backtrace: =========
/lib/libc.so.6[0x47975143]
/lib/libc.so.6[0x4797b984]
/lib/libc.so.6[0x4797cd28]
/lib/libstdc++.so.6(_ZdlPv+0x20)[0x47d8d9e0]
/lib/libstdc++.so.6(_ZdaPv+0x1c)[0x47d8da3c]
Any help would be greatly appreciated. Thanks.
SUB
They way you call memmove(dst, src, sizes)
. Is not correct. It should be
memmove(Q, joblist, sizeof(AdjacencyList) * (numjobs + 1));
UPDATE:
Like its name, memmove
operates on memory. It simply copies sizes
bytes data from src
to dst
.
If you want to copy an array of n objects to Q
, you must tell the function the total size of the data you want to copy, that is size of each objects * number of objects
. I don't know how many objects you need to copy, numjobs + 1
is only an example. If you want to copy numjobs
it should be sizeof(AdjacencyList) * numjobs
.