473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

letting free() know how much to free...?

In one of my functions I create a char string s, of dynamic allocated
length.

I want to free the memory before my function returns. Everywhere I find
says free(s) is the way to do this, but I'm not so sure. How can the
language
know how much space to free? It looks to me as if this would just free the
first
char in the string, since an array is a pointer to it's first element.

What am I missing?

--
Jim H jh
@333
.org
Nov 14 '05 #1
9 1460
Jim H <jh************ ****@333.org> wrote:
I want to free the memory
"The" memory? What memory? You can't just call free() on any pointer,
you know. It must be a pointer you previously got from *alloc(), and
which hasn't been freed in the mean time.
before my function returns. Everywhere I find
says free(s) is the way to do this, but I'm not so sure.
Well, what other way would you use?
How can the language know how much space to free?
It knows. By magic, if that's how it wants to do it. Suffice it to know
that the Standard require that if *alloc() allocate memory and return a
pointer to it, free() knows how to free that memory when given that, and
no other, pointer.
It looks to me as if this would just free the first char in the string,
since an array is a pointer to it's first element.


I cannot fathom that logic.

Richard
Nov 14 '05 #2
Jim H <jh************ ****@333.org> spoke thus:
since an array is a pointer to it's first element.


This sounds like a job for the FAQ:

http://www.eskimo.com/~scs/C-faq/q6.3.html
http://www.eskimo.com/~scs/C-faq/q6.8.html

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
On Thu, 04 Mar 2004 14:13:25 -0000, Jim H <jh************ ****@333.org>
wrote:
In one of my functions I create a char string s, of dynamic allocated
length.

I want to free the memory before my function returns. Everywhere I find
says free(s) is the way to do this, but I'm not so sure. How can the
language
know how much space to free? It looks to me as if this would just free the
first
char in the string, since an array is a pointer to it's first element.

What am I missing?


In K&R, there is, I believe, an implementation of alloc() and free() that
you may want to take a look at (for demystification purposes). The
executive summary: the pointer you get from malloc() et. al. refers to
your requested amount of memory (except when it is NULL, of course), but
"parasitica lly clinging" to that memory, typically just before it (I'm not
sure whether other implementations are possible), is a data structure that
gives free() the ability to re-assimilate that memory into the free
store...and that data structure includes the size of the allocated block,
among other things.
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #4
In <op************ **@news.eclipse .co.uk> Jim H <jh************ ****@333.org> writes:
In one of my functions I create a char string s, of dynamic allocated
length.

I want to free the memory before my function returns. Everywhere I find
says free(s) is the way to do this, but I'm not so sure.
If everyone says this is the proper way, there must be a reason...
How can the language know how much space to free?
The language neither knows nor cares. It is the implementation of the
dynamical memory allocation function that does. And its obvious how:
because you specified the size when you allocated the memory block.
malloc gave you the address of the block, but memorised its size
somewhere. This somewhere can be in a block header that precedes the
block, so, when you give the block address to free(), it can easily
compute the address of the block header and retrieve its size from there.
That's why strange things can happen if you write beyond the limits of the
dynamically allocated memory blocks.
It looks to me as if this would just free the first
char in the string, since an array is a pointer to it's first element.
If you have a pointer to the first byte, you can also figure out the
size of the array, if you memorised it at the array allocation time,
which is what malloc and friends typically do. I have already showed
you a simple way of doing it, but it's not the only possible method (e.g.
a table associating a length to each allocated block starting address
could be maintained by malloc and friends and searched by free).
What am I missing?


Many things, apparently.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
>> since an array is a pointer to it's first element.

This sounds like a job for the FAQ:

http://www.eskimo.com/~scs/C-faq/q6.3.html
http://www.eskimo.com/~scs/C-faq/q6.8.html


You rightly saw it was the the equivalence between arrays and pointers
that I was
getting mixed up in.
I had been told that an array and a pointer to the start of the array were
exactly the
same in every way, so given an array:

char * str[];

sizeof( str ) would be the same as sizeof( &(str[0]) );

I can now see that the equivalence only goes so far. Thanks for the help.

--
Jim H jh
@333
.org
Nov 14 '05 #6

"Leor Zolman" <le**@bdsoft.co m> wrote in message
and that data structure includes the size of the allocated block,
among other things.

Actually if you implement malloc() and free() in the obvious way, then all
you need is the size of the block. You can tell where the block is by
pointer arithmetic, and that's all you need to know to consolidate freed
blocks and keep track of them.
Nov 14 '05 #7
"Malcolm" <ma*****@55bank .freeserve.co.u k> writes:
"Leor Zolman" <le**@bdsoft.co m> wrote in message
and that data structure includes the size of the allocated block,
among other things.

Actually if you implement malloc() and free() in the obvious way, then all
you need is the size of the block. You can tell where the block is by
pointer arithmetic, and that's all you need to know to consolidate freed
blocks and keep track of them.


How are you going to efficiently find the beginning of the
previous block if you only store the size of the current block?
If you can't find the beginning of the previous block, how can
you do consolidation?
Nov 14 '05 #8
On Thu, 4 Mar 2004 22:28:37 -0000, "Malcolm"
<ma*****@55bank .freeserve.co.u k> wrote:

"Leor Zolman" <le**@bdsoft.co m> wrote in message
and that data structure includes the size of the allocated block,
among other things.

Actually if you implement malloc() and free() in the obvious way, then all
you need is the size of the block. You can tell where the block is by
pointer arithmetic, and that's all you need to know to consolidate freed
blocks and keep track of them.


To be precise, I should have said "...include s the size of the allocated
block (or a reasonable way for it to be deduced), among other things."

As soon as I read another post in the thread discussing how the size can be
derived, I just knew someone was going to bring this up. If this is the
worst misspeak I commit over the next week...it probably means I'm about to
stop posting for a week ;-)
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #9

"Ben Pfaff" <bl*@cs.stanfor d.edu> wrote in message
How are you going to efficiently find the beginning of the
previous block if you only store the size of the current block?
If you can't find the beginning of the previous block, how can
you do consolidation?

As we allocate and free memory the arena fragments. We store the fragments
as a linked list which is always kept in ascending order.
When a block is freed you can find its place in the list by walking it until
two pointers straddle the block to be freed.
To consolidate, we need to know the size and the intial position of the
previous block, the size and intial position of the block to be freed, and
the size and intial position of the following block. Since the intial
position of the block to free is given by the pointer, it follows that all
we need to store is the size, in a block that is allocated. Blocks in the
free list, however, need both size information and pointers to the next
block.
If you attempt to store the location of the previous block you run into
problems when it is broken up or consolidated by further allocations.
Walking a linked list is fast, but it is still an O(N) operation. In
practise the easiest way of getting rid of this inefficiency is to treat
small blocks specially. You then have only a few big allocations to run from
the general-purpose allocator. If you are sufficiently determined you can
store all the free blocks in a balanced binary tree and find the position of
the free block that way.

Nov 14 '05 #10

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

Similar topics

0
1492
by: Bengt Richter | last post by:
We have where syntax in combination with suite expression syntax (bear with me, I think a good synergy will emerge ;-) http://groups.google.co.uk/groups?selm=mailman.403.1105274631.22381.python-list%40python.org http://groups.google.co.uk/groups?selm=3480qqF46jprlU1%40individual.net are the key referneces for background (I'm just repeating...
86
4080
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory that was freed by realloc(), but the only way explicitly mentioned in the C89 standard to free memory via realloc() is to realloc() it down to 0...
1
4634
by: VM | last post by:
Is it possible for the bound data in a web datagrid to be displayed in links? The grid will show the client's first name and last name and, when the user clicks on the first or last name, I want to get the cell the user clicked (thus getting the client he chose), and take the user to another separate page that will show that client's complete...
6
1899
by: John Dalberg | last post by:
I don't know how much Microsoft is planning to price Visual Web Developer Express Edition. I heard rumors from free to $100. In my opinion I think it should make it free. The reason is to attract as many people to .NET and ASP.NET. This includes people who are in the Linux world using free IDE's like Eclipse and the people who are thinking...
7
295
by: Jack | last post by:
how does free know how much of memory to release if I say free(ptr)? Thanks.
3
2271
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses these structs. A typical usage case will be as ff (note the code below is Pseudocode and WILL NOT compile) //example structs (I have left out the...
7
2670
by: siddhu | last post by:
Dear experts, If I do free(p); memory pointed by p is freed and is available for further allocations in the process. But how does it decide about how much memory (size) has to to be freed and make it available for further allocations? Regards, Siddharth
5
1351
SwimDude0614
by: SwimDude0614 | last post by:
Hi guys, A while ago I set up a free form mail program on a friend's server. It went quite well. I used Tectite's free wizard, filled out the stuff on their website and then downloaded the file and all was good. They charge for it now... ugh. Does anyone know of a good site like Tectite that is still completely free? I tried downloading the...
0
7659
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...
0
7580
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...
0
7882
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
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...
1
7634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
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...
1
5481
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...
0
5208
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...
1
2079
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

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.