473,909 Members | 4,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with freeing data

I will past only two segments from the code it should be enough to see
what I did wrong, I think I know there I made a mistake, but how to
fix it I can not tell. This why I need help from you all.

Main code:
----------------
/* duomenu rasymas i faila */
dst = fopen(argv[2], "w");
if (dst != NULL) {
wordDBSize = sizeStack(wordD B);
for (tmp = 0; tmp < wordDBSize; tmp++) {
word = NULL;
word = popStack(wordDB );
if (fprintf(dst, "%d: %s\n", tmp + 1, word) < 0)
show_err("(antr a)[main] Can not write to file. Please
check <destination_fi lepermissions. Exiting.");
/* FIXME - PROBLEM HERE - (line below) */
free(word);
}
charAverage = wordCountAll / (float)wordCoun t;
fprintf(dst, "Average (Words: %d, Total chars: %d) : %.2f\n",
wordCount, wordCountAll, charAverage);
fclose(dst);
destroyStack(&w ordDB);
} else
show_err("(antr a)[main] Can not create <destination_fi le>
file.");

Stack code:
-----------------
char* popStack(stack *item) {
child *tmp;
char *value;

if (item->root == NULL)
return NULL;
value = item->root->value;
tmp = item->root;
item->root = item->root->next;
free(item->root);
item->size--;

return value;
}

I highlighted in the code there I am have problem. As you can see when
I call popStack it return pointer to the string, which is in my linked
list element and then I free memory of the element. free(word);
returns error:

Macbook:antra_n ew marius$ ./antra src.txt dst.txt sep.txt
antra(882) malloc: *** error for object 0xc0000003: Non-aligned
pointer being freed
*** set a breakpoint in malloc_error_br eak to debug
Segmentation fault

So, I think that freeing the element of my list frees my string, which
pointer is located in that element. Removing "free(item-
>root);" (stack code) fix this problem and now I can free my string in
the main code, but as you can see in this situation I am leaving
garbage in the memory and I don not want to do that.

Firstly I would like to know, does freeing my element from linked list
frees the string which pointer is located in that element. And finally
how could I fix that?

P.S. My stack holds the pointers in char arrays.
Mar 4 '08 #1
9 3331
P.S. My stack holds the pointers to(in) char arrays
Mar 4 '08 #2
david wrote:
[Snip]
char* popStack(stack *item) {
child *tmp;
char *value;

if (item->root == NULL)
return NULL;
value = item->root->value;
tmp = item->root;
item->root = item->root->next;
free(item->root);
Are you _sure_ you meant to do this, rather than free(tmp)?
Mar 4 '08 #3
david wrote:
I will past only two segments from the code it should be enough to see
what I did wrong,
No. It isn't enough.

As you don't know what's wrong, I don't see how you think you can decide
how much we need to know...

In future, please post something complete enough for us to see what's
happening - ideally produce the smallest possible self-contained
testcase.

[snip]
free(word);
Was "word" allocated with malloc/calloc/realloc? We don't know, because
you haven't shown us....

[Snip]
Firstly I would like to know, does freeing my element from linked list
frees the string which pointer is located in that element. And finally
how could I fix that?
free(pointer) frees what the pointer points to, no more and no less.
(Obviously "pointer" must point to space allocated by one of the
malloc family of functions).
P.S. My stack holds the pointers in char arrays.
What is that supposed to mean?
Mar 4 '08 #4
david <Da************ *****@gmail.com wrote:
word = popStack(wordDB );
/* FIXME - PROBLEM HERE - (line below) */
free(word);
char* popStack(stack *item) {
value = item->root->value;
tmp = item->root;
item->root = item->root->next;
free(item->root);
This is wrong; you want free(tmp); here. But that's probably not what's
causing your problem.
return value;
}
antra(882) malloc: *** error for object 0xc0000003: Non-aligned
pointer being freed
So, I think that freeing the element of my list frees my string,
That depends. If item->root->value is a char *, it doesn't. If it's a
char[], it does. But you have failed to show us two critical parts of
your code:
- the definition of a stack, and that of stack's member root;
- the code where you put your words _in_ your stack, in particular, how
you declare the memory for each root's value string.

If you malloc() memory for each word separately before you push them
onto your stack, and don't manipulate that address before you push it on
(or, say, free() it in between), you should be able to free() it. OTOH,
if you get the memory for each word from part of a single malloc(), or
from an automatic ("local") variable, or any number of other
possibilities, you not only do not need to free() it, but shouldn't.

What the solution to your problem is depends on the two bits of code I
mentioned above.

Richard
Mar 4 '08 #5
The is not that small and I will post in the one of pastebin websites.

stack.h - http://www.paste.lt/paste/6aa50aa1d9...56c8d24e3f1f16
stack.c - http://www.paste.lt/paste/684c6fa2ef...2d4be11c65e796
antra.c (main) - http://www.paste.lt/paste/0f2dd328b2...0383ad9fb41cdf

antra_lib.c - http://www.paste.lt/paste/83ec85cfac...55f7acc4d106f0
antra_lib.h - http://www.paste.lt/paste/cc88b47394...edd6edf9487b4c

I am sorry that I am not using buffered reading in this program. This
is the hole program and you should be able to compile it.
../antra <source_file<de stination_file< separators_list _file>
should put words in destination file whose length is even (sorry if
wrong word, my native language is not English) and word does not
contains any number.

I use realloc to allocate memory and grow my string until I get to
separator and when I push to stack the pointer of this string and then
I NULL the pointer and reseting the length of string back to zero,
this should protect the later string I created. And again I reallocate
a new one and etc. till the end.

Stack struct has pointer "root" which points to the top of my stack.
child struct contains of next pointer (next item in the linked list)
and char pointer there it holds the location of the string I pushed.

I again read how works realloc and I can see that I should be using it
correctly, if pointer is NULL when realloc works just like malloc.

I am very interested why I can not free that string later after poping
it from stack.
Thanks everyone for helping.
Mar 4 '08 #6
david <Da************ *****@gmail.com writes:
The is not that small and I will post in the one of pastebin websites.

stack.h - http://www.paste.lt/paste/6aa50aa1d9...56c8d24e3f1f16
stack.c - http://www.paste.lt/paste/684c6fa2ef...2d4be11c65e796
antra.c (main) - http://www.paste.lt/paste/0f2dd328b2...0383ad9fb41cdf

antra_lib.c - http://www.paste.lt/paste/83ec85cfac...55f7acc4d106f0
antra_lib.h -
http://www.paste.lt/paste/cc88b47394...edd6edf9487b4c
This looks like the code you posted earlier. The popStack function
still has the error that I saw being pointer out so I don't see any
point in taking time to look at the rest. You are freeing the wrong
thing and you need to fix that before anything else.

--
Ben.
Mar 4 '08 #7
In this code:
1 for (tmp = 0; tmp < wordDBSize; tmp++) {
2 word = NULL;
3 word = popStack(wordDB );
4 if (fprintf(dst, "%d: %s\n", tmp + 1, word) < 0)
5 show_err("(antr a)[main] Can not write to file. Please
check <destination_fi lepermissions. Exiting.");
6 /* FIXME - PROBLEM HERE - (line below) */
7 free(word);
8 }

In line 3, you get "word" from popStack(). What if word was NULL?
popStack() do return NULL on one case. You can't free NULL
david wrote:
I will past only two segments from the code it should be enough to see
what I did wrong, I think I know there I made a mistake, but how to
fix it I can not tell. This why I need help from you all.

Main code:
----------------
/* duomenu rasymas i faila */
dst = fopen(argv[2], "w");
if (dst != NULL) {
wordDBSize = sizeStack(wordD B);
for (tmp = 0; tmp < wordDBSize; tmp++) {
word = NULL;
word = popStack(wordDB );
if (fprintf(dst, "%d: %s\n", tmp + 1, word) < 0)
show_err("(antr a)[main] Can not write to file. Please
check <destination_fi lepermissions. Exiting.");
/* FIXME - PROBLEM HERE - (line below) */
free(word);
}
charAverage = wordCountAll / (float)wordCoun t;
fprintf(dst, "Average (Words: %d, Total chars: %d) : %.2f\n",
wordCount, wordCountAll, charAverage);
fclose(dst);
destroyStack(&w ordDB);
} else
show_err("(antr a)[main] Can not create <destination_fi le>
file.");

Stack code:
-----------------
char* popStack(stack *item) {
child *tmp;
char *value;

if (item->root == NULL)
return NULL;
value = item->root->value;
tmp = item->root;
item->root = item->root->next;
free(item->root);
item->size--;

return value;
}

I highlighted in the code there I am have problem. As you can see when
I call popStack it return pointer to the string, which is in my linked
list element and then I free memory of the element. free(word);
returns error:

Macbook:antra_n ew marius$ ./antra src.txt dst.txt sep.txt
antra(882) malloc: *** error for object 0xc0000003: Non-aligned
pointer being freed
*** set a breakpoint in malloc_error_br eak to debug
Segmentation fault

So, I think that freeing the element of my list frees my string, which
pointer is located in that element. Removing "free(item-
root);" (stack code) fix this problem and now I can free my string in
the main code, but as you can see in this situation I am leaving
garbage in the memory and I don not want to do that.

Firstly I would like to know, does freeing my element from linked list
frees the string which pointer is located in that element. And finally
how could I fix that?

P.S. My stack holds the pointers in char arrays.
Mar 5 '08 #8
Thought about that and checked some time ago, it does not return NULL;
It does return exact the same amount of strings (char pointers) I
pushed in the code above, the only problem it does not show the words
and some garbage.

Ben:
Sorry, only after your second post I noticed the mistake. I will try
to recompile code when I have a chance.
Mar 5 '08 #9
ru*********@gma il.com writes:
[...]
In line 3, you get "word" from popStack(). What if word was NULL?
popStack() do return NULL on one case. You can't free NULL
[...]

Yes, you can. free(NULL) does nothing. (Doing so may well be an
error; I haven't looked closely at the code.)

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 5 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7459
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
6
2022
by: da.colonel | last post by:
Hello, I'm having some trouble with implementing a hashtable (hashmap) for a class at school. The map's constructor is called with a hash function and the number of possible keys the function returns. The get and set methods do work well, only the -operator does not work. When trying to do something like map = "December", it seems like the = has no effect. At least, if the value for the same key is read after assigning it, it will be...
5
3178
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the client/user code be responsible for doing it? On what basis should a decision be made favouring either case ?
3
2429
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text field they are fairly large. the net result is that when 60 or so results are reitreved the page size is 400kb! which takes too long to load. is there a way of shorterning this? freeing up the memory say, bcos what is actually displayed is not...
7
1556
by: B. Williams | last post by:
I wrote a program that would simply output data to the screen, but now I am trying to have the data saved to a file. I don't have a problem creating the file or even outputting data to it when there is no header file to include, but I can't seem to figure out how to output the data to the file I create when the main is in a different. Will someone give me an example or show me what I am doing wrong? #include <iostream> #include...
3
2024
by: anand1603 | last post by:
hi, Following is problematic Code main() { char *ptr={NULL}; ftn(ptr); }
11
2018
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it takes me a lot of time (since i have to loop determining the data type and freeing it). Is there any idea to free all the contents of the structure in shortest possible time.
5
14830
by: jbenner | last post by:
I have opened a PMR for this with IBM, and am not asking for advice from the DB2 DBA community. I am posting this as an FYI that DB2 Health Monitor, even at the latest version of DB2, still can cause huge problems with slow connect times and heavy resource locking in high concurrency / high transaction volume environments. I have an OLTP with 30-90 transactions per second activity, and start of Health Monitor every 2 hours was crashing our...
10
1971
by: Igal | last post by:
hay, i'm doing this program. having problem wiht realloc in the function that reads data structures into array (pointer - bp2), this happens after reading the second record. when call to realloc. i can't figure out what's wrong, think it's soming got to do with freeing bp2. and something called "corruption of the heap". book* LoadBookData(unsigned *size) { FILE* fp;
0
10035
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9877
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10919
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10538
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8097
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7248
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.