|
Home > Archive > Unix Programming > May 2005 > Malloc
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Russell Shaw 2005-05-22, 2:47 am |
| Hi,
I wrote a malloc. When i do:
int *ptr = r_malloc(10);
*ptr = 27;
i get a segfault. ptr was set to 0x24. sbrk(0) shows enough
space. I'm using gcc-3.4 on a pc (debian-sid).
Is the stack and program data in this same memory space? How do
i know what space it occupies?
| |
| Paul Pluzhnikov 2005-05-22, 2:47 am |
| Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
> I wrote a malloc.
Do you mean you wrote your own implementation of malloc, or
something else?
> int *ptr = r_malloc(10);
> *ptr = 27;
Checking for NULL return is always a good idea ...
> i get a segfault. ptr was set to 0x24.
The return of 0x24 from malloc pretty much means that your malloc
implementation is broken.
> Is the stack and program data in this same memory space?
Definitely.
> How do i know what space it occupies?
Huh? Whatever 'it' is, it occupies whatever space it occupies.
The questions above make me think you need a couple more years
of experience before you are ready to write your own malloc
implementation :-(
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Russell Shaw 2005-05-22, 2:47 am |
| Paul Pluzhnikov wrote:
> Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
>
>
> Do you mean you wrote your own implementation of malloc,
yes
> or
> something else?
>
>
> Checking for NULL return is always a good idea ...
>
>
> The return of 0x24 from malloc pretty much means that your malloc
> implementation is broken.
>
>
> Definitely.
>
>
> Huh? Whatever 'it' is, it occupies whatever space it occupies.
Well, does that mean pc programs randomly crash when the heap size
is large enough to hit the stack or program variables?
> The questions above make me think you need a couple more years
> of experience before you are ready to write your own malloc
> implementation :-(
Do i just guess and use sbrk to get 1Mbyte?
| |
| Erik Max Francis 2005-05-22, 2:47 am |
| Russell Shaw wrote:
> I wrote a malloc. When i do:
>
> int *ptr = r_malloc(10);
>
> *ptr = 27;
>
>
> i get a segfault.
You probably wrote it incorrectly, then. You're going to have to give
more information than this if you want help.
--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Life imitates art far more than art imitates life.
-- Oscar Wilde
| |
| Barry Margolin 2005-05-22, 2:47 am |
| In article <m3vf5cexvv.fsf@amoeba.parasoft.com>,
Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> wrote:
> Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
>
>
> Do you mean you wrote your own implementation of malloc, or
> something else?
>
>
> Checking for NULL return is always a good idea ...
>
>
> The return of 0x24 from malloc pretty much means that your malloc
> implementation is broken.
More likely, some other part of his program corrupted the heap, causing
malloc to misbehave.
I suggest the OP invest in a heap debugging tool.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
| |
| Paul Pluzhnikov 2005-05-22, 2:47 am |
| Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
> Well, does that mean pc programs
What do you mean by "pc programs" ?
> randomly crash when the heap size
> is large enough to hit the stack or program variables?
I do not know of any OS that would allow sbrk() to grow heap such
that it "hits" the stack. And since heap usually begins *after*
the globals [1], there is little chance for heap to "hit" them.
> Do i just guess and use sbrk to get 1Mbyte?
You are not asking a smart question. Yes, you can "guess and use
sbrk()", but I suggest you read this first:
http://www.catb.org/~esr/faqs/smart-questions.html
and see if perhaps you want to provide some more details.
[1] Shared libraries do complicate matters a bit, but sbrk() will
not grow heap such that it reaches them either.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Russell Shaw 2005-05-22, 2:47 am |
| Paul Pluzhnikov wrote:
> Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
>
>
> What do you mean by "pc programs" ?
Any program that runs in linux on a pc. I've never worried about checking
how much space there is between the stack and heap on a pc app. I always
do when the same code is going to run in a microcontroller with 2kB ram.
>
> I do not know of any OS that would allow sbrk() to grow heap such
> that it "hits" the stack. And since heap usually begins *after*
> the globals [1], there is little chance for heap to "hit" them.
I was wondering what the default heap starting-address is for a linux app.
It can't be infinite, or some large programs will crash.
>
> You are not asking a smart question. Yes, you can "guess and use
> sbrk()", but I suggest you read this first:
> http://www.catb.org/~esr/faqs/smart-questions.html
> and see if perhaps you want to provide some more details.
In a (my) microcontroller with no OS and real (not virtual) addressing,
the variables and heap start around 0xff, and the stack base is at
0x7ff. On a pc, i have no idea.
> [1] Shared libraries do complicate matters a bit, but sbrk() will
> not grow heap such that it reaches them either.
>
> Cheers,
| |
| phil_gg04@treefic.com 2005-05-22, 7:47 am |
| > I wrote a malloc. When i do:
> int *ptr = r_malloc(10);
> ...
> ptr was set to 0x24
It looks like you have started allocating memory from address 0. This
is wrong.
I think, but am not certain, that you can get a special linker symbol
to know where to start allocating from. What that symbol is I now
can't recall. Or maybe the right thing to do is to call sbrk(0) and
use the value that it returns. I suggest that you look at the existing
malloc() implementation for some clues.
Phil.
| |
| Måns Rullgård 2005-05-22, 7:47 am |
| phil_gg04@treefic.com writes:
>
> It looks like you have started allocating memory from address 0. This
> is wrong.
>
> I think, but am not certain, that you can get a special linker symbol
> to know where to start allocating from. What that symbol is I now
> can't recall. Or maybe the right thing to do is to call sbrk(0) and
> use the value that it returns. I suggest that you look at the existing
> malloc() implementation for some clues.
sbrk(0) should work.
--
Måns Rullgård
mru@inprovide.com
| |
|
|
| Alex Colvin 2005-05-23, 5:59 pm |
|
>Well, does that mean pc programs randomly crash when the heap size
>is large enough to hit the stack or program variables?
no. it's quite reliable.
--
mac the naïf
|
|
|
|
|