8

I am working on Linux. In which section of memory are the command line arguments stored (stack or heap)?

I tried to execute free(argv) and I got a segmentation fault. Why is that?

Bruce
  • 33,927
  • 76
  • 174
  • 262
  • 2
    I would assume that they are stored on the stack (before main is called) so you shouldn't really be messing with their memory! – Nick Oct 11 '11 at 15:06
  • 3
    why does it matter? and why don't you check the address? :/ – Karoly Horvath Oct 11 '11 at 15:06
  • @yi_H: How can I infer the section by address? – Bruce Oct 11 '11 at 15:11
  • 2
    @yi_H - it matters to an inquisitive mind. Good beginners should ask these kind of questions all the time. Documentation may be too dry in this matter so one has to turn to a mentor. – Leonardo Herrera Oct 11 '11 at 15:40
  • @Leonardo Herrera: it was a rhetorical question. Maybe it's true for beginners, but professionals has to know where to draw the line.. modern computer systems are way too complicated to know every small detail. as long as it doesn't matter what's the point of knowing it? – Karoly Horvath Oct 11 '11 at 15:54
  • @yi_H - agree that systems may be too complex to know every little detail. This, however, is general knowledge and it is needed if you dwell in C enough time. The same can be said for, example, garbage collection methods when you are working with Java. – Leonardo Herrera Oct 11 '11 at 20:47
  • @Leonardo Herrera: yes, you might need to know that Java GC can take a lot of time and can hinder near-realtime systems, and that there is an alternative GC to solve such a problem. but can you give me just *one* example where knowing where argv is stored gives you *any* usable knowledge / benefit? does that matter at all from a C pointer's perspective? – Karoly Horvath Oct 11 '11 at 21:43
  • @yi_H: I concede you have a point knowing the specific place where argv is stored may be of little use. I still disagree with the "don't see, doesn't matter" sentiment so prevalent these days. Inquisitive minds need to know! – Leonardo Herrera Oct 12 '11 at 01:09
  • Possible duplicate of [Where are C/C++ main function's parameters?](http://stackoverflow.com/questions/4196201/where-are-c-c-main-functions-parameters) – Veedrac May 11 '17 at 17:33

2 Answers2

13

I tried to execute free(argv) and I got a segmentation fault. Why is that?

You can only free what you malloc/calloc (and possibly realloc later on). Trying to free something else invokes Undefined Behaviour. One (good) way UB manifests itself is by producing a segmentation fault; a (bad) way is to make the program appear to work as intended.

As to where they are ... read section 5.1.2.2.1 of the C99 Standard -- its unspecified.

the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    @Bruce: so `argv` points somewhere, you never know and must not rely upon. – Vlad Oct 11 '11 at 15:13
  • 1
    `argv` points to a groups of pointers. Each of the pointers `argv` points to reside where the compiler puts them (you can try to read the source of your compiler or ask the developers). Each of the things pointed to by the pointers in `argv` reside where the compiler put them. – pmg Oct 11 '11 at 15:14
  • @pmg: Please someone tell me where argv points to. Its killing me. – Bruce Oct 11 '11 at 15:15
  • @Bruce: we've *been* telling you, it's up to the individual compiler. It could be the stack, the heap, or a completely different memory segment. – John Bode Oct 11 '11 at 15:17
  • 6
    `#include ` / `int main(int argc, char **argv) { printf("%p\n", (void*)argv); return 0; }` results in `0x7fff0a8a9318`. That's where `argv` points to: **0x7fff0a8a9318** Remember this :) – pmg Oct 11 '11 at 15:18
  • @All: I am using gcc version 4.1.2 20080704 (Red Hat 4.1.2-51) – Bruce Oct 11 '11 at 15:18
  • 1
    Argv typically is located on (or near) the stack though. Anything stored in the stack area not either explicitly initialized by you (uninitialized vars, say) or used for control purposes (the location the stack keeps its ebp or esp, say) will be garbage: some number that could be anything, and, when used as a memory location as you use it in free(), could thus point anywhere. This is only in response to where it points. What happens when you free() stuff not priorly malloc()ed has been answered already. – gnometorule Oct 11 '11 at 15:30
  • This is a painfully unhelpful answer. You don't actually answer the question, just berate the asker for daring to ask. – Veedrac May 11 '17 at 17:31
1
I am working on Linux. In which section of memory are the command line arguments stored (stack or heap)?

That's up to the implementation; check your compiler documentation. All that's required is that that argc and argv, as well as the strings that the argv array points to, be modifiable by the program.

I tried to execute free(argv) and I got a segmentation fault. Why is that?

You didn't allocated argv with malloc. You don't need to free the command line argument vectory.

John Bode
  • 119,563
  • 19
  • 122
  • 198