473,385 Members | 1,341 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,385 software developers and data experts.

Not free'd malloc memory issue

drhowarddrfine
7,435 Expert 4TB
Someone handed me a piece of code that works really well one time through but, if you call it a second time, it returns an offset from where it should point...I think.

My code calls his function which returns a pointer to a node structure. This node, and all its children, are allocated with malloc and I have a 'free function' to free everything when I'm done.

I've been calling his stuff just once and exiting with no problems but, today, I started calling it consecutively and it's as if the pointers to other nodes are correct at the beginning but making stuff up as it goes along.

What I found is his code allocates memory for the file we're reading, creates the nodes and pointers to that data, then returns the top node pointer to me.

As you can tell, the allocated nodes get free'd but the memory holding the file does not. This probably worked before because I exit the program at that point but, now, I have a new routine that calls it twice.

The file pointer is set up as you would expect, fp=malloc(size of file), so on the second call, the same fp gets re-allocated at the same point in his function on the second call.

The results give me three string, "cart", "order", "item". But the second call gives me "cart", "order", "cart". Which is why I think something's gotten offset somehow. The nodes themselves are still set up correctly, just the wrong content.

So my question is, would you guys agree that not freeing that file pointer is the source of the issue here? I know anything's possible since I'm not showing the code but does someone have some insight as to why that is? I'm throwing that out since I am just not in the mood to work on his code and it would be a pain to edit down a simple sample for you. I'd hate to start on it and have someone later tell me that's not the problem.
Jun 8 '10 #1
3 2001
donbock
2,426 Expert 2GB
I assume this is C, not C++, since you're using malloc instead of new.

Were you going to post any of the code?

"... The file pointer is set up as you would expect, fp=malloc(size of file), ..."
By file pointer, do you mean a FILE * variable?
What do you mean here by "size of file"?
Jun 8 '10 #2
Banfa
9,065 Expert Mod 8TB
Hard to tell without the code Doc but in relation to malloc and freeing memory then
  • Not freeing malloc'd memory is always an error and in the long term would lead to problems with memory resource running out.
  • If the program has not actually run out of memory then not freeing memory should not effect what happens the next time memory is allocated.
  • What you are describing, odd data values sounds more like not properly initialising pointers or writing over the endof allocated (or static) objects or using static objects where a malloced object should have been used. That is to say it is part of the logic of using the pointer rather than memory having been allocated and not freed.

How long is the code anyway without editing?
Jun 8 '10 #3
drhowarddrfine
7,435 Expert 4TB
@Banfa,
You're thinking the same thing I was thinking but it helps to hear someone else say the same thing.

I didn't supply the code because I'm just thinking out loud and it is pretty long.
Jun 9 '10 #4

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

Similar topics

13
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
7
by: David Jacques | last post by:
I have to reformat a string from the form "SRID=4269;POINT(-90.673 69.4310000006199)" to GeometryFromText('POINT (-141.095 68.5430000006417)',4269) ); I have a function to do this by...
3
by: Paminu | last post by:
I get this error after running my application for some time. What does it mean and what should I be looking for in my code?
3
by: bamapookie | last post by:
I am new to VB, but not new to programming. I am using VB.Net 2003 and I have written a small app to monitor several running processes and everything is fine except the memory usage. When the app...
5
by: prashantkhoje | last post by:
hi ppl, i am developing an C application in Microsoft visual C++ 6.0. i get following error while running it in debug mode: /* here's the error message */ program: my_application.exe...
3
by: sharadvasista | last post by:
Say i have to character pointers char *localbuf = NULL; char *malbuf = NULL; I now allocate memory for malbuf malbuf = (char*)malloc(30); I write a string into malbuf which is of...
4
by: =?Utf-8?B?SmVycnk=?= | last post by:
I have a program using VB express where I have about 50 forms through out the program on various forms I have multiple BMP pictures on the form, when I went to create a new form and put about 6 bmp...
7
by: prakashdehury | last post by:
Hi, I have a c# console exe Based on input it consumes arround 1-3 gb of memory I am trying to run it on win2003 box with 8GB of ram. Once the consumed memory goes beyound 1 GM the process...
8
by: ramsatishv | last post by:
Hi Group, I have one question. If I am allocating memory of 38 bytes to an integer pointer, what will be the memory actually allocated? Will there be any memory alignment concept in malloc?...
6
by: Dan | last post by:
What is the correct way to allocate memory that is defined as volatile so that all standard compilers will not optomise out writes to the memory that are never read again? I have some encryption...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.