Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
Why are the addresses of argc and argv 12 bytes apart?

I ran the following program on my computer (64-bit Intel running Linux).

#include <stdio.h>

void test(int argc, char **argv) {
    printf("[test] Argc Pointer: %p\n", &argc);
    printf("[test] Argv Pointer: %p\n", &argv);
}

int main(int argc, char **argv) {
    printf("Argc Pointer: %p\n", &argc);
    printf("Argv Pointer: %p\n", &argv);
    printf("Size of &argc: %lu\n", sizeof (&argc));
    printf("Size of &argv: %lu\n", sizeof (&argv));
    test(argc, argv);
    return 0;
}

The output of the program was

$ gcc size.c -o size
$ ./size
Argc Pointer: 0x7fffd7000e4c
Argv Pointer: 0x7fffd7000e40
Size of &argc: 8
Size of &argv: 8
[test] Argc Pointer: 0x7fffd7000e2c
[test] Argv Pointer: 0x7fffd7000e20

The size of the pointer &argv is 8 bytes. I expected the address of argc to be address of (argv) + sizeof (argv) = 0x7ffed1a4c9f0 + 0x8 = 0x7ffed1a4c9f8 but there is a 4 byte padding in between them. Why is this the case?

My guess is that it could be due to memory alignment, but I am not sure.

I notice the same behaviour with the functions I call as well.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

On your system, the first few integer or pointer arguments are passed in registers and have no addresses. When you take their addresses with &argc or &argv, the compiler has to fabricate addresses by writing the register contents to stack locations and giving you the addresses of those stack locations. In doing so, the compiler chooses, in a sense, whatever stack locations happen to be convenient for it.

over 4 years ago · Santiago Trujillo Denunciar

0

Why are the addresses of argc and argv 12 bytes apart?

From the perspective of the language standard, the answer is "no particular reason". C does not specify or imply any relationship between the addresses of function parameters. @EricPostpischil describes what is probably happening in your particular implementation, but those details would be different for an implementation in which all arguments are passed on the stack, and that is not the only alternative.

Moreover, I'm having trouble coming up with a way in which such information could be useful within a program. For example, even if you "know" that the address of argv is 12 bytes before the address of argc, there's still no defined way to compute one of those pointers from the other.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda