473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

free(...) fails in a *struct inside a *struct, please help...

(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.
Jan 15 '08 #1
3 2940
weaknessforcats
9,208 Expert Mod 8TB
/* here is where the problem occurs*/
free(data); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
free(datacarrier ->DCtype);
free(datacarrier ->DCindex);
free(datacarrier);
free(datablock ->previus);
free(datablock ->next);
free(datablock); < <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
data and datablock are local variables. You can't free() them since you didn't
malloc() them.

Also, why are these variables duplicated in your function????

Also, why are these functions static ??????

Use pointer arguments to pass malloc'd variables around. If you don't, the local variables are copies of the variables used on the call. Your function chnage the copies and not the originals. This will cause all sorts of problems.
Jan 15 '08 #2
Thank you weaknessforcats.

datacarrier = datablock ->value;

> I duplicated the pointers because the datablock ->value is type of void* and the datacarrier is type of DCT and i wand to free the memory i earlier state in the programm as malloc(sizeof(DCT)).

Can i just free(datablock ->value) and it will work as i free(datacarrier) even if they are differend types?

> I took this programm from a biggen application which it was part of it but i didn't change the static because i didn't know what was the actual problem of it.

You also sayed than : Use pointer arguments to pass malloc'd variables around. If you don't, the local variables are copies of the variables used on the call. Your function chnage the copies and not the originals. This will cause all sorts of problems.

Can you be more explanatory or give me an examble or correct one point of the code just there were it fails so i can see it...please.

I didn't understand the meaning of pointer arguments, you mean pointer variables to house the addresses of the call?
Jan 15 '08 #3
thank you solve it.i just free(function which returns the value i want to free) retrospectively...
Jan 15 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: skeeterbug | last post by:
i have something similar to this: <?php header("Location: http://mysite.com/destination.php"); ?> i would like to replace "mysite.com/destination.php" with a constant. for example, with a...
6
by: Agnes | last post by:
I got a ASP program in the server, it runs properly . However, As I run the web siste , click 'login' button. It return the following errors. It seems can't connect the database, I had stopped...
7
by: slashdotcommacolon | last post by:
Hello, I'm working on the exercises from k&r, exercise 5-13 is to implement a simple replacement for the unix tail command. The brief says it should be able to cope no matter how unreasonable the...
3
by: Rex | last post by:
Hey all... I have a personal web site that I added some content to for my fantasy football league rosters. I'd like to add current NFL headlines to it. Anybody have any idea how to do that??...
2
by: tuan_vandyk | last post by:
Hi I desperately need help with my project. Theoretically everything should work bu it just isn't. Please email me for a copy of the project's source code. It was made in Turbo C++ 5. Please if...
55
by: salad | last post by:
I have contained in a listbox the Window's caption, the class name for the window, and the hWND of the window. Is there a way, using the data from above, to activate/set focus to that window?
21
by: salad | last post by:
Thanks for reading this post and wasting your time. I was thinking about writing about the PCDatasheet vs The Pussyfarts war. The pussyfarts, as near as I can tell, have little to offer this...
76
by: dbansal | last post by:
Hi group, I have a question to ask you all. I have allocated some chunk of memory using ptr=(int*)malloc(). now I am trying to free that memory using free((void*)ptr). My question is does free()...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.