Page 1 of 1
How do you find the memory address which is free in C?
Posted: Wed Jan 21, 2009 1:50 am
by Archil
Hi!
In assembly is used to do something like this, to find the last address of my code for later storage purposes, or creating a dynamic memory management:
Code: Select all
Start:
mov eax,End ; mov eax,offset End ; alternatively, ya know.
...
...
End:
Now EAX is pointing to the end of the code. But I've just started coding in C and I have no idea of finding this address after linking. I found that in C the first global variable of type void* points to what i need:
Code: Select all
void* lastAddress;
int main(){
...
...
}
// <-lastAddress points to this address after linking.
Of course if I add some more variables after that "lastAddress" it won't point to the end of the program. Is there any way of keeping that "lastAddress" the last variable after linking? I've been thinking about dividing bss section in my linker script but I don't seem to come to an idea that worked.. P.S. I'm linking several object files btw.
Re: How do you find the memory address which is free in C?
Posted: Wed Jan 21, 2009 2:59 am
by Hyperdrive
Archil wrote:In assembly is used to do something like this, to find the last address of my code for later storage purposes, or creating a dynamic memory management: [...] But I've just started coding in C and I have no idea of finding this address after linking. I found that in C the first global variable of type void* points to what i need:
Code: Select all
void* lastAddress;
int main(){
...
...
}
// <-lastAddress points to this address after linking.
You can't really rely on this. If you change your linker script and put the data sections somewhere else (e.g. in front of .text) the above assumption will fail.
Archil wrote:Of course if I add some more variables after that "lastAddress" it won't point to the end of the program. Is there any way of keeping that "lastAddress" the last variable after linking? I've been thinking about dividing bss section in my linker script but I don't seem to come to an idea that worked.. P.S. I'm linking several object files btw.
If your linker script defines "end" You could do something like:
Code: Select all
extern void *end;
int main() {
...
<use &end here>
...
}
Regards,
Thilo
Re: How do you find the memory address which is free in C?
Posted: Wed Jan 21, 2009 3:51 am
by Archil
Many thanks, Thilo. The code seems pretty nicer now
Hope this post will be helpful to others as well.
Re: How do you find the memory address which is free in C?
Posted: Thu Jan 22, 2009 2:59 am
by Hyperdrive
berkus wrote:Archil wrote:Hope this post will be helpful to others as well.
Not really. This is covered in bare bones and JamesM tutorials if I'm not mistaken.
Very likely, you
are mistaken.
The wiki's bare bones article(s) don't have that. The linker scripts there don't even define "end" or something alike. It's just not needed there.
I had a (very!) quick look at JamesM's tutorials. He has a linker script, where the symbols "end", "_end" and "__end" are defined. But he doesn't use them in the "C world", as far as I have seen. He only uses "end" in his multiboot info in the "assembly world".
Regards,
Thilo
Re: How do you find the memory address which is free in C?
Posted: Thu Jan 22, 2009 5:14 am
by JamesM
Hyperdrive wrote:berkus wrote:Archil wrote:Hope this post will be helpful to others as well.
Not really. This is covered in bare bones and JamesM tutorials if I'm not mistaken.
Very likely, you
are mistaken.
The wiki's bare bones article(s) don't have that. The linker scripts there don't even define "end" or something alike. It's just not needed there.
I had a (very!) quick look at JamesM's tutorials. He has a linker script, where the symbols "end", "_end" and "__end" are defined. But he doesn't use them in the "C world", as far as I have seen. He only uses "end" in his multiboot info in the "assembly world".
Regards,
Thilo
You are fail, it seems.
I allude to &end in the tutorial description but never describe it explicitly - I use a variable called "placement_address" which is never initialised in the tutorial description - It's left as an exercise to the user. However, in the sample code (downloadable at the end of each tutorial, in this case tutorial 6) uses &end to initialise placement_address.
James
Re: How do you find the memory address which is free in C?
Posted: Thu Jan 22, 2009 6:14 am
by Hyperdrive
JamesM wrote:
Hyperdrive wrote:[...] I had a (very!) quick look at JamesM's tutorials. He has a linker script, where the symbols "end", "_end" and "__end" are defined. But he doesn't use them in the "C world", as far as I have seen. He only uses "end" in his multiboot info in the "assembly world".
You are fail, it seems.
I allude to &end in the tutorial description but never describe it explicitly - I use a variable called "placement_address" which is never initialised in the tutorial description - It's left as an exercise to the user. However, in the sample code (downloadable at the end of each tutorial, in this case tutorial 6) uses &end to initialise placement_address.
Okay, you are right. Sorry. I definitely overlooked this. As I said, I had just more like a glance and never worked myself through your text.
Btw - From what I saw, your tutorials are really great. You take the reader through all important steps and your explanations are very well written.
That isn't the case for many (most?) tutorials around the net.
Re: How do you find the memory address which is free in C?
Posted: Thu Jan 22, 2009 8:35 am
by Archil
Anyway, the solution for me seems to be this:
Code: Select all
SECTIONS{
. =0x200000;
.text :{*(.text)}
.data :{*(.data)}
.bss :{*(.bss)}
.end :{*(.end)}
}
... and ...
Code: Select all
.globl GetProgramEnd
GetProgramEnd:
mov $.end,%eax
ret
.section .end
in an .s file, which will be compiled by "as".
Re: How do you find the memory address which is free in C?
Posted: Thu Jan 22, 2009 4:51 pm
by kasper
Hi
Archil wrote:Anyway, the solution for me seems to be this:
[...]
There's a more direct method, Archil
The linker script:
Code: Select all
SECTIONS{
. =0x200000;
.text :{*(.text)}
.data :{*(.data)}
.bss :{*(.bss)}
end = .;
}
In your c-code use:
The pointer end contains the address after all data in .bss. No extra section needed and no asm routing to calculate the end address.
Re: How do you find the memory address which is free in C?
Posted: Fri Jan 23, 2009 3:48 am
by Archil
Yes, that works but with the code:
..adding "C" I get a compilation error, but I'm not caring about it now. Thanks, again!
Re: How do you find the memory address which is free in C?
Posted: Fri Jan 23, 2009 4:03 am
by AJ
Hi,
For the record, you would only need the 'extern "C" ' part if you were using a C++ compiler.
Cheers,
Adam