1

how can I get the first and last addresses of data, heap and stack and dimensions for C language in my machine? (Linux)

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Alquaary
  • 11
  • 1
  • 1
    Why do you want it? What is the actual problem you need to solve? Why do you think getting those pointers would solve that problem? Please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask] as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and [edit] your question to show us a [mcve] of your attempt. Lastly please read about [the XY problem](http://xyproblem.info/) as your question currently is one. – Some programmer dude Apr 22 '20 at 09:49
  • I have a project about memory. The actual problem I wanna solve is to know how much is the size of data, stack and heap in my machine. To know that I should know where every part starts and ends to calculate the actual size of them. Thank you – Alquaary Apr 22 '20 at 09:57
  • Usually you do that by (the human) reading some linker script, not by programming. Though I suppose there might be tools that let you peek into an .elf file. – Lundin Apr 22 '20 at 09:59
  • @Lundin Little problematic for the heap on a Linux system, as it doesn't really have a heap in the old-fashioned way. – Some programmer dude Apr 22 '20 at 10:02
  • @Someprogrammerdude *Why do you want it?* - I don't think that's relevant. *What is the actual problem you need to solve? Why do you think getting those pointers would solve that problem?* - Why should there be a problem in the first place? Seems pretty clear what OP wants. The question may be too broad, but those are not really the problems I see here. – Marco Bonelli Apr 22 '20 at 10:07
  • The *size* of the data segment can be fetched from the executable file itself. I don't know if the stack-size is in the executable file also, but should be possible to get through `/proc` filesystem somewhere (look at `/proc/self` to find information about the own process). The heap is another matter though, as Linux (and most other modern protected systems) doesn't have a heap in the traditional sense. – Some programmer dude Apr 22 '20 at 10:08
  • @MarcoBonelli Perhaps there are other solutions to the original problem, something that the OP overlooked? We could also come up with answers that might give the exact information asked about, but that doesn't really solve the problem itself. It's always better to ask directly about a problem, and giving information about attempts and what might be a possible solution, than asking about a possible pre-determined solution that might not really help or is possible to do. – Some programmer dude Apr 22 '20 at 10:10
  • @Someprogrammerdude you are still assuming OP has a problem to begin with. Maybe they just want to know these values and the method to obtain them. Granted, there surely are some misconceptions about how memory works here (from OP's comment above), but this could still be a valid (but too broad) question if they don't know much about the topic. – Marco Bonelli Apr 22 '20 at 10:14
  • Does this answer your question? [What and where are the stack and heap?](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – r3mainer Apr 22 '20 at 10:14
  • @MarcoBonelli Asking for curiosity is fine, but then it should be stated (otherwise we will start asking possibly irrelevant questions). – Some programmer dude Apr 22 '20 at 10:18
  • [Classmate of yours](https://stackoverflow.com/questions/61360487/find-memory-segments-size-and-initial-addresses-of-a-process)? – Shawn Apr 22 '20 at 10:35
  • Actually the professor asked us to make a program that print Data, stack and stack sections of a process in c language and he gave us two conditions 1) we should write 0x before the address which everyone can do that 2) the size of every section. and for that he gave us a guid. Guiding: You can take advantage of the signals so that the program does not end unintentionally. it's probably the same ex in this link https://stackoverflow.com/questions/61360487/find-memory-segments-size-and-initial-addresses-of-a-process – Alquaary Apr 22 '20 at 10:38
  • 'the professor asked' yes, I know, because the same question has been asked continually over the last week or so:( – Martin James Apr 22 '20 at 12:33
  • so can any one give me a solution for that ! – Alquaary Apr 22 '20 at 18:17

0 Answers0