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

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 1449
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)cyberspace.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
"parasitically 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.com> 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.uk> writes:
"Leor Zolman" <le**@bdsoft.com> 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.uk> wrote:

"Leor Zolman" <le**@bdsoft.com> 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 "...includes 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.stanford.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
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 ;-) ...
86
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...
1
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...
6
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...
7
by: Jack | last post by:
how does free know how much of memory to release if I say free(ptr)? Thanks.
3
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...
7
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
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
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...

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.