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

Local Heap?

I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.

Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?

I have a program that does a lot of things. I don't have the code
handy to post, but I think a word description will do. I'd be
more than happy to post code if someone thinks it would be
helpful.

One function "dynamically copies a string" - so I pass a pointer
to a string, it checks the length of the string, allocates
sufficient memory, copies the string into the new memory, and
returns the value of the new pointer.

I have another function that does a few things, one of which is
to call the dynamic string copy function. It then does some other
things and returns the pointer to the new string to main(). That
works just fine (yes, including the null terminator). In main(),
I perform some manipulations on that string and then free the
pointer. That works fine.

I then made a copy of the function - literally - and changed the
name slightly and changed some of the miscellaneous details that
it does - but the dynamic copy works the same. When I run it, I
get a run time error when I call free() in main() and looking
through the debug headers it appears that this new version is
trying to free up memory that is not in the "Local Heap Space".
I've never heard of that? What gives?

I checked the value of the pointer within the function and the
value that gets returned and the value in the pointer at the time
that free() is called and they all agree. I copied the code from
the original function into the new function so that the only
remaining difference is the function names. The old function
works, the new function throws the error. The only way I can get
my program to run is to not perform the garbage collection?

Any thoughts?

Nov 14 '05 #1
9 2641
William L. Bahn wrote:
I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.

Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?

I have a program that does a lot of things. I don't have the code
handy to post, but I think a word description will do. I'd be
more than happy to post code if someone thinks it would be
helpful.
Anyday, few lines of C code would be better compared to plain
English !!

One function "dynamically copies a string" - so I pass a pointer
to a string, it checks the length of the string, allocates
sufficient memory, copies the string into the new memory, and
returns the value of the new pointer.

I have another function that does a few things, one of which is
to call the dynamic string copy function. It then does some other
things and returns the pointer to the new string to main(). That
works just fine (yes, including the null terminator). In main(),
I perform some manipulations on that string and then free the
pointer. That works fine.

I then made a copy of the function - literally - and changed the
name slightly and changed some of the miscellaneous details that
it does - but the dynamic copy works the same. When I run it, I
get a run time error when I call free() in main() and looking
through the debug headers it appears that this new version is
trying to free up memory that is not in the "Local Heap Space".
I've never heard of that? What gives?
What are those 'miscellaneous details' ?

What is the name of the function that you had chosen ? Are you sure
that there is no pollution of the function names here ?


I checked the value of the pointer within the function and the
value that gets returned and the value in the pointer at the time
that free() is called and they all agree. I copied the code from
the original function into the new function so that the only
remaining difference is the function names. The old function
works, the new function throws the error. The only way I can get
my program to run is to not perform the garbage collection?

Any thoughts?


Please post the source code ( a small, compilable one that describes
the problem precisely) for people to help.

--
Karthik.
Nov 14 '05 #2
In article <10*************@corp.supernews.com>,
William L. Bahn <wi*****@toomuchspam.net> wrote:
I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.
Strange problem, it sounds like undefined behavoiur somewhere in your
program, rather than a compiler problem.
Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?
Two possiblities:
1) You have already freed that section of memory.
2) You have invoked undefined behaviour somewhere in your program.

<snip>
I then made a copy of the function - literally - and changed the
name slightly and changed some of the miscellaneous details that
it does - but the dynamic copy works the same. When I run it, I
get a run time error when I call free() in main() and looking
through the debug headers it appears that this new version is
trying to free up memory that is not in the "Local Heap Space".
I've never heard of that? What gives?
It sounds like somewhere in your program you are writing outside of a
valid object. Which is doing strange things to malloc memory area. This
could be writing before the start or after the end of a malloced area of
memory, writing through a uninitialized pointer.
Any thoughts?


You could try Linting the code, or using a malloc debugging library if
there is one avalible for your platform.

These are only guesses, to get more help you need to post a version of
code that has had as much code as possible removed from it but still shows
the problem.

Kevin.

Nov 14 '05 #3
William L. Bahn wrote:

I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.

Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?


If that value has already been used in a free() call,
since the time when it was returned by malloc.

--
pete
Nov 14 '05 #4
"William L. Bahn" wrote:

I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.

Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?


If it already has been freed.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

CBFalconer wrote:
"William L. Bahn" wrote:
I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.

Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?

If it already has been freed.


Couldn't you add
"or, if it already has been passed to the realloc() function"
to this statement?

- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFBNxCiagVFX4UWr64RAhgcAKCZphoUPjYE84VzTTiflq X5ZfiFEQCg0e6g
Fhbq90fm0AgHNiWSRtPTS6o=
=1Bg4
-----END PGP SIGNATURE-----
Nov 14 '05 #6
"William L. Bahn" <wi*****@toomuchspam.net> wrote in message news:<10*************@corp.supernews.com>...

Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?


Normally this should not happen, but:
1.
-I once worked on a C Compiler for a Microcontroller, which had broken
a
Clib, as if it really ran out of memory (no physical memory left)
malloc() did not return 0, although the memory was not allocated, but
this is very rare. :-)

2.
You accidently freed the memory with free previously. (but as you
already said
you dont, well)

3.
Memory corruption
Where is the address stored? Global data?, stack?
If its stored on a stack variable it could possibly be a corrupted
stack
(Write an array variable over its real size, ...)

A similar thing could happen if its a global.

So what I would do is:
Look if the address value is the same when it is allocated and when it
is freed.
If not well, then you know its probably a memory (global, stack)
corruption :-).

Regards
Michael
Nov 14 '05 #7
Lew Pitcher wrote:
CBFalconer wrote:
"William L. Bahn" wrote:
I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.

Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?


If it already has been freed.


Couldn't you add
"or, if it already has been passed to the realloc() function"
to this statement?


Not necessarily. It may be so. However some system packages (my
nmalloc especially) go to pains to avoid unnecessary copying and
thus to return the same pointer if possible. Anytime realloc
returns NULL it is possible to free the original pointer, assuming
nothing else has fouled it.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #8
"William L. Bahn" <wi*****@toomuchspam.net> wrote in message news:<10*************@corp.supernews.com>...
I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.

Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?


William,

You have a clear case for heap corruption. (Mis)use of malloc/free
can lead to the heap being corrupted. One the heap is corrupted
results from malloc and free are both undefined. In you particular
case, you may a freed a global or stack variable. A subsquent malloc
would then return that value as a pointer. The final call to free
then is totally confused.

There are several freeware tools to help with this problems. Check
out Valgrind and mpatrol. My company, Dynamic Memory Solutions,
markets the commercial tool, Dynamic Leak Check (DLC). The DLC
catches heap corrupting error and leak by replacing the normal
malloc/free routine as runtime. See www.dynamic-memory.com

Good luck,
Matthew
Dynamic Memory Solutions
www.dynamic-memory.com
Nov 14 '05 #9
Matthew Fisher wrote:
"William L. Bahn" <wi*****@toomuchspam.net> wrote in message
I am having a strange problem - and I suspect it is something
specific to my compiler - but want to get a read on the Standard
C portion of it first.

Basic Question: Under the C Standard, under what conditions, if
any, can a value returned by malloc() not be used in a subsequent
free() call?


You have a clear case for heap corruption. (Mis)use of malloc/free
can lead to the heap being corrupted. One the heap is corrupted
results from malloc and free are both undefined. In you particular
case, you may a freed a global or stack variable. A subsquent malloc
would then return that value as a pointer. The final call to free
then is totally confused.

There are several freeware tools to help with this problems. Check
out Valgrind and mpatrol. My company, Dynamic Memory Solutions,
markets the commercial tool, Dynamic Leak Check (DLC). The DLC
catches heap corrupting error and leak by replacing the normal
malloc/free routine as runtime. See www.dynamic-memory.com

Good luck,
Matthew
Dynamic Memory Solutions
www.dynamic-memory.com


This continuous vending of an apparently obviously flawed
commercial system is getting on my nerves. There is a free
implementation of malloc/free/realloc, which also affords fairly
comprehensive debugging tools, designed for the DJGPP system and
which I believe will function quite adequately on any Linux system
that supplies sbrk(). It is NOT standard C, and uses GNU flavor
(not C99) variadic macros for debugging itself (thus they can be
removed with care).

There is no guarantee that any replacement for malloc etc. will
function in a C system, the very act of so doing is expressly
forbidden by the standard. It is probably especially obnoxious in
a C++ system, but that is OT here (F'ups set).

See <http://cbfalconer.home.att.net/download/nmalloc.zip>

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Nov 14 '05 #10

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

Similar topics

0
by: ANt | last post by:
Hi, we have some major GC issues at present with a system we're trying to put live. It's a live calculation engine that's distributed across about 30 Java server processes. A set of processes...
15
by: John Doe | last post by:
Hi all, I know the standard doesn't care about threads (wrongly :-) But in current compilers implementation, is the "list" which holds count of the occupied and free heap addresses SHARED among...
9
by: cppaddict | last post by:
I have a method that uses a fairly large object. The choice is between having a local object in the method or a static member object that the method uses. ------CHOICE 1--------- int...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
0
by: DanielQ | last post by:
I do something in c# language, but I also want to use the DLL which writed in c++.So I used the trusteeship as the c# help file teached us. But unfortunately I failed. I look for the reason....
6
by: RThaden | last post by:
Hi together, I am a bit clueless about the following problem: In the following code excerpt std::string getStr() { std::string bla="asdfsa";
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
10
by: Horacius ReX | last post by:
Hi, in some C program I need to port to some architecture, I send to a function the parameter char with predefined values. Inside the function, this data is read and something is calculated. But...
10
by: raylopez99 | last post by:
Beware this newbie trap: private bool myFunction (Myenum X) { bool local_bool; //this will not compile; you need to do this first: bool local_bool = true; switch (X) { case 1:
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.