Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which memory segment command line arguments get stored? [duplicate]

Tags:

c

linux

gcc

The command line arguments argc, arv[], used with the main function in C, where do they get stored in memory?

The storage area I am looking for this question should be heap, data segment, stack or other.

like image 625
Prashant Chikhalkar Avatar asked Sep 08 '13 06:09

Prashant Chikhalkar


2 Answers

It's not specified by the language.

Unix-like systems put them on the stack (or in the case of some of the more esoteric systems, "a" stack, since they have more than one).

like image 107
torek Avatar answered Nov 08 '22 14:11

torek


As torek said in his answer, the C language does not define where the command line arguments must be stored, and there isn't much practical use to knowing. However, the information is normally stored on the stack.

This code uses a common extension to the main() to get hold of the environment too, and then prints the addresses of both the arguments and the environment.

#include <stdio.h>
#include <inttypes.h>

int main(int argc, char **argv, char **envp)
{
    printf("&argc = 0x%.8" PRIXPTR "\n", (uintptr_t)&argc);
    printf("&argv = 0x%.8" PRIXPTR "\n", (uintptr_t)&argv);
    printf("&envp = 0x%.8" PRIXPTR "\n", (uintptr_t)&envp);
    while (*argv != 0)
        printf("*argv = 0x%.8" PRIXPTR "\n", (uintptr_t)*argv++);
    while (*envp != 0)
        printf("*envp = 0x%.8" PRIXPTR "\n", (uintptr_t)*envp++);
    return 0;
}

When run on my Mac, it showed:

&argc = 0x7FFF5494B52C
&argv = 0x7FFF5494B520
&envp = 0x7FFF5494B518
*argv = 0x7FFF5494B748
*envp = 0x7FFF5494B74E
*envp = 0x7FFF5494B839
*envp = 0x7FFF5494B862
*envp = 0x7FFF5494B86D
*envp = 0x7FFF5494B889
*envp = 0x7FFF5494B8C1
*envp = 0x7FFF5494B8F4
*envp = 0x7FFF5494B905
*envp = 0x7FFF5494B915
*envp = 0x7FFF5494B920
*envp = 0x7FFF5494B92E
*envp = 0x7FFF5494B93B
*envp = 0x7FFF5494B974
*envp = 0x7FFF5494B998
*envp = 0x7FFF5494B9CD
*envp = 0x7FFF5494B9F2
*envp = 0x7FFF5494BA0B
*envp = 0x7FFF5494BA2A
*envp = 0x7FFF5494BA46
*envp = 0x7FFF5494BA7B
*envp = 0x7FFF5494BB2E
*envp = 0x7FFF5494BB3C
*envp = 0x7FFF5494BB76
*envp = 0x7FFF5494BB87
*envp = 0x7FFF5494BB9D
*envp = 0x7FFF5494BBC8
*envp = 0x7FFF5494BBEA
*envp = 0x7FFF5494BC2B
*envp = 0x7FFF5494BC35
*envp = 0x7FFF5494BC51
*envp = 0x7FFF5494BC5F
*envp = 0x7FFF5494BC75
*envp = 0x7FFF5494BC82
*envp = 0x7FFF5494BD55
*envp = 0x7FFF5494BD7E
*envp = 0x7FFF5494BD96
*envp = 0x7FFF5494BDA3
*envp = 0x7FFF5494BDAE
*envp = 0x7FFF5494BDBF
*envp = 0x7FFF5494BDCA
*envp = 0x7FFF5494BDE8
*envp = 0x7FFF5494BE01
*envp = 0x7FFF5494BE09
*envp = 0x7FFF5494BE1E
*envp = 0x7FFF5494BE38
*envp = 0x7FFF5494BE52
*envp = 0x7FFF5494BE71
*envp = 0x7FFF5494BE7D
*envp = 0x7FFF5494BEF5
*envp = 0x7FFF5494BF16
*envp = 0x7FFF5494BF27
*envp = 0x7FFF5494BF32
*envp = 0x7FFF5494BF66
*envp = 0x7FFF5494BF8D

That's pretty unexciting, but shows that all the addresses are in the same general area, which is the C stack. (Tested on Mac OS X 10.8.4 — 64-bit program, invoked without any command line arguments and a biggish environment.)

like image 31
Jonathan Leffler Avatar answered Nov 08 '22 16:11

Jonathan Leffler