How do you find the memory address which is free in C?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Archil
Posts: 24
Joined: Tue Jul 15, 2008 7:54 am
Location: Tbilisi, Georgia.
Contact:

How do you find the memory address which is free in C?

Post 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.
Hyperdrive
Member
Member
Posts: 93
Joined: Mon Nov 24, 2008 9:13 am

Re: How do you find the memory address which is free in C?

Post 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
Archil
Posts: 24
Joined: Tue Jul 15, 2008 7:54 am
Location: Tbilisi, Georgia.
Contact:

Re: How do you find the memory address which is free in C?

Post by Archil »

Many thanks, Thilo. The code seems pretty nicer now :wink: Hope this post will be helpful to others as well.
Hyperdrive
Member
Member
Posts: 93
Joined: Mon Nov 24, 2008 9:13 am

Re: How do you find the memory address which is free in C?

Post 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
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: How do you find the memory address which is free in C?

Post 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
Hyperdrive
Member
Member
Posts: 93
Joined: Mon Nov 24, 2008 9:13 am

Re: How do you find the memory address which is free in C?

Post 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. =D> That isn't the case for many (most?) tutorials around the net.
Archil
Posts: 24
Joined: Tue Jul 15, 2008 7:54 am
Location: Tbilisi, Georgia.
Contact:

Re: How do you find the memory address which is free in C?

Post 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". :)
User avatar
kasper
Posts: 19
Joined: Sun Apr 27, 2008 7:59 am
Location: The Netherlands, Amersfoort
Contact:

Re: How do you find the memory address which is free in C?

Post 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:

Code: Select all

extern "C" void* end;
The pointer end contains the address after all data in .bss. No extra section needed and no asm routing to calculate the end address.
Archil
Posts: 24
Joined: Tue Jul 15, 2008 7:54 am
Location: Tbilisi, Georgia.
Contact:

Re: How do you find the memory address which is free in C?

Post by Archil »

kasper wrote:

Code: Select all

extern "C" void* end;
Yes, that works but with the code:

Code: Select all

extern void* end;
..adding "C" I get a compilation error, but I'm not caring about it now. Thanks, again!
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: How do you find the memory address which is free in C?

Post by AJ »

Hi,

For the record, you would only need the 'extern "C" ' part if you were using a C++ compiler.

Cheers,
Adam
Post Reply