How to free memory...,

P

Pranav

I am trying to allocate memory using sbrk() and when I try to free the
allocated memory at the end of the code using free(), gives me a
segmentation fault when I try to execute the code., How to free memory
exactly?
 
O

onLINES

I am trying to allocate memory using sbrk() and when I try to free the
allocated memory at the end of the code using free(),  gives me a
segmentation fault when I try to execute the code., How to free memory
exactly?

In order to use free(), think of it as new and delete in C++. For
every new, a delete must follow. And free() is commonly used with
malloc, or realloc() (even though in a sense it has it's own free, but
only called when it "crashes")

So, use malloc() to allocate your memory space.
 
N

Nate Eldredge

Pranav said:
I am trying to allocate memory using sbrk() and when I try to free the
allocated memory at the end of the code using free(), gives me a
segmentation fault when I try to execute the code., How to free memory
exactly?

free() can only free memory obtained from malloc(). You almost
certainly want to be using malloc() to get memory instead of sbrk().
sbrk() is a lower-level, Unix-specific function, and one of its
disadvantages is that you usually can't reliably free memory allocated
with it, and certainly not via the free() function.

<off-topic>

sbrk() comes from a time when the usual memory model for Unix programs
was a single, contiguous chunk of memory. The address of the end of
this chunk was called the "break point". sbrk(n) would ask the
operating system to allocate enough memory to advance the break point by
n bytes. This is how malloc() would get additional memory. Some
systems allowed you to move the break point backwards by passing a
negative number to sbrk(), thus returning memory to the system, but this
required that *all* the memory beyond the new break point was no longer
needed. If your memory needs didn't occur in just the right order, this
usually didn't happen. This is why traditional implementations of
free() didn't actually return memory to the OS, but only reserved it for
future malloc() calls.

Nowadays the mmap()/munmap() calls allow a program to get memory from
the OS in a discontiguous fashion, so it is easier to return it later.
If for some reason you need something lower-level than malloc, mmap is
probably the way to go; you can map /dev/zero or use a flag to request
an anonymous mapping not associated with a file. Further discussion
about this would be better directed to comp.unix.programmer.

</off-topic>
 
M

Martin Ambuhl

Pranav said:
I am trying to allocate memory using sbrk() and when I try to free the
allocated memory at the end of the code using free(), gives me a
segmentation fault when I try to execute the code., How to free memory
exactly?

Your problem is not how to free memory, but how to allocate it. The
standard library provides malloc, realloc, and calloc for this. It does
not contain a function named sbrk.

sbrk is a UNIX system call, although a similarly named function may be
provided in other environments. It is designed for
implementation-specific (and highly knowledgeable) use, and should not
be used by novices. If you still want to use it instead of the standard
allocation functions, you need to ask technical support, or in a
newsgroup, or on a mailing list for your specific platform, operating
system, and library.
 
F

Flash Gordon

onLINES wrote, On 14/10/08 06:49:
In order to use free(), think of it as new and delete in C++. For
every new, a delete must follow.

Why use C++ to explain C? What makes you think the OP knows C++?
And free() is commonly used with
malloc, or realloc() (even though in a sense it has it's own free, but
only called when it "crashes")

By "crashes" I assume you mean fails. If so you are wrong because if
realloc fails the original memory is still allocated and accessible
using the original pointer.
So, use malloc() to allocate your memory space.

Agreed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top