0

I need some help in understanding how the memory allocation is performed in this situation.

 char someStringPointer[100] = "hello world";
 char *argv[3];
 argv[0] = "echo";
 argv[1] = someStringPointer; 
 argv[2] = NULL;

Since I'm not allocating memory, everything will be on the stack, so no heap memory is allocated here.

But I'm not sure how the memory is allocated on the stack. Every pointer is 8 bytes, so let's say that argv is at (in octal)100.

so argv[0] is at 100, argv[1] is at 101 and argv[2] is at 102.

argv[0] will now point to a block of 5 bytes which holds the characters "echo\0" when I'm using the assignment operator '=' it's allocating memory directly on the stack based on the length of the string? (sequence of chars more precisely).

argv[1] will point to wherever someStringPointer was allocated.

argv[2] I'm not sure, I'm guessing it's not pointing anywhere and set to null.

I am keen to understand what the memory looks like here.

2
  • 2
    no strings are being copied, you are just setting pointers to literals Commented May 10, 2022 at 17:33
  • Only argv is on the stack. It contains 3 pointers. Each of them points to a string allocated somewhere else (string literal in one case, an array of chars in another), or set to NULL. Commented May 10, 2022 at 17:39

1 Answer 1

1

But I'm not sure how the memory is allocated on the stack. Every pointer is 8 bytes, so let's say that argv is at (in octal)100.

so argv[0] is at 100, argv[1] is at 101 and argv[2] is at 102.

If every pointer occupies 8 bytes then if atgv[0] is at the address 100 then argv[1] is at the address 108 and argv[2] is at the address 116.

In this assignment

 argv[0] = "echo";

there is used the string literal "echo" that compilers usually store in literal pools with static storage duration. That is the memory for the literal is reserved before the program startup.

argv[1] will point to wherever someStringPointer was allocated.

argv[1] points (stores the address of) to the first character of the character array someStringPointer.

argv[2] I'm not sure, I'm guessing it's not pointing anywhere and set to null.

argv[2] is a null pointer. It does not point to any valid object.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, a question regarding the literal pool, does it includes literal from functions as well? meaning, it scans all of the code and gathers every assignment such as this?
@AllForCode yes, not just assignments, but everywhere that you write "a string like this" - the compiler will put that exact string somewhere in the program file, and then when you write "a string like this" in the code it means a pointer to the string that the compiler puts somewhere. (Special exception: char myString[] = "a string like this"; does something different)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.