Original Post
Hi, I''m having some problem with my linked list. My data gets corrupted! I''m using void pointers to store any type of data, so when I''d like to add something to the list i call my funcion:
Add(void *pData,DWORD dwSize)
Inside this funcion i use malloc in the following way:
cpNewNode->pData = malloc(dwSize);
if (!cpNewNode->pData)
return false;
Then later on when i fetch data i typecast the data into a proper datatype like: int a = *((int*)my_list[5]);
When I output my values to a file however I''ve noticed something really strange, if I were to add 4 integers to my list, everything works correct but if I add more stuff to my list some data gets corrupted, here''s an example of what I mean:
// CODE
CList list;
int iArray[6] = {1,2,3,4,5,6};
for (int i = 0; i < 6; i++)
list.Add((void*)&iArray,sizeof(int));
for (i = 0; i < 6; i++)
file << *((int*)list[i]) << endl;
// END CODE
// FILE OUTPUT
1
2
3
4
4 // Corrupted!
6
// END OUTPUT
Could someone explain this and tell me how to fix this problem? It would be really nice, I guess it''s something to do with malloc. When I read about the function it said it might sometimes allocate some extra memory, but I don''t know.
/ Thanks!