Discussion:
malloc
(too old to reply)
cs412044
2005-02-21 09:50:26 UTC
Permalink
I'm up to Load_User_Program and have a couple of quick (I think) questions.

1) First, the initial goal is to:
"find the highest possible virtual address based on the segment base address
and memory size of that segment"
When it says 'the segment', which segment does it refer to? The last one?
That seems to make sense to me, that the last segments virtual address + its
size is the number we're looking for.

2) Then, in the hints senction we're told to:
"Determine where in memory each executable segment will be placed"
This sounds like a similar task to the one above, but I'm not sure if it's
the same (in which case I'm having trouble understanding what exactly to do)
or if perhaps they're 2 different things (in whch case it would seem we
first find the amound of space to allocate, and then later make the segment
placement determination for *each* segment when we're placing them into
memory).

3) Lastly, how exactly do we allocate space for something in memory? People
seem to be talking about something called "malloc" wh\hich may or may not
have anything to do with this. I'm not sure if this is a difference between
the C++ I learned and the C we're programming, but I'm used to using 'new'.

Thanks,
Jason
cs412044
2005-02-21 18:22:23 UTC
Permalink
From discussion today I now get my first question and a little bit of
the third.
Can anybody still help me with my second question and what the syntax of
Malloc is?

Thanks,
Jason
Paul Mazzucco
2005-02-21 14:18:09 UTC
Permalink
The syntax for Malloc is in the source code, in, coincidentally, malloc.h:

void* Malloc(ulong_t size);

where size is the number of bytes you want to allocate. If you're still
not sure (familiar with C++ and not C), just look up the standard c
libraries. "Malloc" is a repimplementatoin for geekos of the c-library
function "malloc" (like how "Print" is a reimplimentation of "printf").
A quick goole search brought up:

http://www.infosys.utas.edu.au/info/documentation/C/CStdLib.html
Post by cs412044
From discussion today I now get my first question and a little bit of
the third.
Can anybody still help me with my second question and what the syntax of
Malloc is?
Thanks,
Jason
c***@CSIC.UMD.EDU
2005-02-21 19:22:33 UTC
Permalink
Malloc(size in bytes)

it basically _Alloc_ates _M_emory.

what new does is

char* text = new char[100];

it takes the sizeof(char) [ex. 1 byte] and multiplies it by 100 to get the size in
memory to hold 100 chars [ex. 100 bytes]. then it calls Malloc(100) and allocates
that size and returns a pointer for that 100 bytes to you.

new is just a nice way to allocate memory and sorta does the calculation for how
much you need on its own and definitely is easier to understand. you just get to
use Malloc so you have to use sizeof() yourself on the structures and sizes that
you calculate from the information passed into Load_User_Program().
c***@CSIC.UMD.EDU
2005-02-21 19:29:03 UTC
Permalink
for yoru second question, the parse_elf_executable tells you where in file and
where in memory thigns should be placed. so just be sure to add the proper offsets
to your pointers.

finding the highest address means looking through all the segments (which could be
in or out of order, and based on the exe itself might want to re-order them) and
find where the highest segment says it wants to go. once there you want to copy
those segmetns into memory, the information is parsed in Parse_elf_executable. i'd
say look at project 1's picture where they had the executable file block and the
lines going down to the executable in memory block.

-drew

Continue reading on narkive:
Loading...