Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers and memory scope

I have been programming C for a while (but still pretty new to C) and I am sometimes getting confused of the way the C is handling the memory.

Consider following valid C snippet:

const char *string(void)
{
       /* where is this pointer variable located in the memory? */
       const char *s;

       /* where is this text data located in the memory? */
       /* and when the program allocates memory for it?  */
       s = "Hello, World";

       return s;
}

int main(void)
{
    printf( "%s", string() );

    return 0;
}

I am asking what exactly is going on in the memory? Isn't the pointer variable 's' a local variable or where is the pointer variable stored in the memory. Also where is the text constant "Hello, World" stored in memory (isn't this considered to be local variable which isnt accesible after function returns)?

Basically what kind of variables / data are consider to be in "local" scope of the functions (ceases to be accessible after function returns)?

I hope you understand what I am trying to say :D.. I think I have lot to learn about compilers and executables so feel free enlighten me!

like image 708
XrM Avatar asked Mar 25 '11 03:03

XrM


People also ask

What is the scope of pointer?

A pointer is a variable that contains a memory location. Like all variables, it can be on the heap or the stack, depending on how it's declared. It's value -- the memory location -- can also exist on the heap or the stack.

What are pointers in memory?

Stated simply, a pointer is nothing more than a variable that holds an address in the computer's memory. This is where a pointer gets its name. A pointer variable holds the address of a certain piece of memory in the computer; in other words, a pointer points at a specific location in memory.

Do pointers take up more memory?

No memory allocation needed, and the pointer will usually occupy exactly as much space in memory as an integer index would. Also, as Joshua Taylor reminds us in a comment, pointers are used to pass something by reference.

How are pointer variables related to memory?

A pointer variable stores a memory address which can be modified, whereas an array name stores a fixed address, set to the first element in the array.


2 Answers

I am asking what exactly is going on in the memory?

Local variables are being allocated on the stack. Constants, including the literal strings, are being allocated in the text or data sections of the executable.

Isn't the pointer variable 's' a local variable?

Yes

or where is the pointer variable stored in the memory?

The local s is in a register or on the stack.

Also where is the text constant "Hello, World" stored in memory?

In either the .text or the .data section. It's constant, but legacy code would sometimes modify them, so it depends on compiler options. You need to distinguish between the reference and the object to understand it all.

(isn't this considered to be local variable which isn't accesible after function returns)?

Well, s is local but the string itself will be needed every time the function is called, and the local frame won't even exist until that happens, so the constant itself is most likely stored in the .text section. It may be stored in .data, depending on compiler options and how much the current compiler release cares about compiling legacy code. The literal inside the expression is something completely different from the variable it is being assigned to.

Basically what kind of variables / data are consider to be in "local" scope of the functions (ceases to be accessible after function returns)?

The ones that are lexically scoped as auto variables, i.e., declared inside functions without the static storage class. The word accessible is unfortunately a bit imprecise. With static storage class the object could be referenced if its address was leaked out of the function.

like image 165
DigitalRoss Avatar answered Nov 03 '22 10:11

DigitalRoss


The compiler has already prepared storage of the string "Hello, World" in such a way that it has its own memory address when your application is loaded.

Then when your code s = "Hello, World"; runs, you're copying the memory address of the compiled string into pointer s. No additional memory has been allocated for the string, and s itself is a local variable (a pointer) temporarily taking up space on the stack.

like image 24
DuckMaestro Avatar answered Nov 03 '22 09:11

DuckMaestro