(sorry about my english...)
I am a newbie in C (3 month expierience)
I have wrote a simple test programm in VS2005, what i do wrong?Please...
In this programm i create a double linked list.Then pass its first block pointer
inside the structure Array to Array ->first and the last block pointer inside the structure Array to Array ->last.So i can manipulate the double linked list as a dynamic array. The cells of this dynamic array are located in
Array ->first/last ->value and inside there i pass a point of structure and in this point of structure i pass onother one, which i fill then with values (type: double).
So far so good, everything is created as expected.The problem begins when i want to free all the memory i have took from the heap.The only memory i can free is from the 4 double values from the fourth stage struct and the 2 values from the third stage struct and the 2 values(previus, next) from the second stage struct, but not actually any of these structs.
When i try to delete any of these structs i get a message saying:
__________________________________________________ ________________
|
| Windows has triggered a breakpoint in Test.exe.
|
| This may be due to a corruption of the heap, and indicates a bug in
| LListTest.exe or any of the DLLs it has loaded.
|_________________________________________________ ________________
The followed programm has been run and tested only in debug mode.
programm: Test.c
#include <stdio.h>
#include <stdlib.h>
#include "LList2.h"
typedef struct {LBlock2T first; LBlock2T last;} *LL2T;
typedef struct {long *DCindex; void *DCvalue; short *DCtype;} *DCT;
typedef struct {double *a; double *b; double *c; double *d;} *DCVT;
static void Create(LBlock2T datablock);
static void Destroy(LBlock2T datablock);
main
(void)
{
LL2T Array;
Array = malloc(sizeof(LL2T));
Array ->first = NList2(NULL,0,f);
Array ->last = NList2(Array ->first,1,l);
Create(Array ->first);
Destroy(Array ->first);
free(Array);
}
static void Create(LBlock2T datablock)
{
DCT datacarrier;
DCVT data;
datablock ->value = malloc(sizeof(DCT));
datacarrier = datablock ->value;
datacarrier ->DCindex = malloc(sizeof(long));
*(datacarrier ->DCindex) = 1;
datacarrier ->DCtype = malloc(sizeof(short));
*(datacarrier ->DCtype) = 0;
datacarrier ->DCvalue = malloc(sizeof(DCVT));
data = datacarrier ->DCvalue;
data ->a = malloc(sizeof(double));
*(data ->a) = 123.456;
(data ->b) = malloc(sizeof(double));
*(data ->b) = 123.456;
data ->c = malloc(sizeof(double));
*(data ->c) = 123.456;
data ->d = malloc(sizeof(double));
*(data ->d) = 123.456;
}
static void Destroy(LBlock2T datablock)
{
DCT datacarrier;
DCVT data;
datacarrier = datablock ->value;
data = datacarrier ->DCvalue;
free(data ->a);
free(data ->b);
free(data ->c);
free(data ->d);
/* here is where the problem occurs*/
free(data);
free(datacarrier ->DCtype);
free(datacarrier ->DCindex);
free(datacarrier);
free(datablock ->previus);
free(datablock ->next);
free(datablock);
}
__________________________________________________ ________________
heap information at the time of problem:
the symbol > is a green arrow and the symbol -> is a yellow arrow.
-> ntdll.dll!77352ea8()
Frames below may be incorrect and/or missing, no symbols loaded
forntdll.dll]
ntdll.dll!773c0c9a()
ntdll.dll!773ac900()
ntdll.dll!77394b6e()
ntdll.dll!7732894a()
ntdll.dll!77372033()
ntdll.dll!77371c21()
kernel32.dll!75c0b019()
> msvcr80d.dll!_CrtIsValidHeapPointer(const void * pUserData=0x00a61208)
Line 2072 C++
msvcr80d.dll!_free_dbg_nolock(void * pUserData=0x00a61208, int
nBlockUse=1) Line 1279 + 0x9 bytes C++
msvcr80d.dll!_free_dbg(void * pUserData=0x00a61208, int nBlockUse=1)
Line 1220 + 0xd bytes C++
msvcr80d.dll!free(void * pUserData=0x00a61208) Line 1178 + 0xb bytes
C++
Test.exe!Destroy(<unnamed-tag> * datablock=0x00a63f80) Line 57 +
0xc bytes C
Test.exe!main() Line 20 + 0xb bytes C
Test.exe!__tmainCRTStartup() Line 597 + 0x19 bytes C
Test.exe!mainCRTStartup() Line 414 C
kernel32.dll!75c33833()
ntdll.dll!7734a9bd()
__________________________________________________ ________________local variables in the time of problem
_crtheap 0x00a60000 void *
pUserData 0x00a61208 const void *
__________________________________________________ ________________
Thank you for your help.