Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How memory allocation works for char* without explicitly allocating

Tags:

c

pointers

In the following (legal) c code, there is no memory explicitly allotted to the pointer p.

AFAIK, i cannot get an int *p to point to a value 5 without explicitly allocating memory.

int main()
{
    char *p;
    p = "Hello";
    return 0;
}
  • How's char* pointers different
  • Where's the memory allocated for "Hello"
like image 577
Arjun Sreedharan Avatar asked Jan 12 '23 01:01

Arjun Sreedharan


2 Answers

When you put literal strings such as "Hello" in your program, the compiler creates an array of characters in the data area of the program (called the Data Segment).

When you assign:

p="Hello";

the compiler takes the address of the string literal in the data segment and puts it in the pointer variable p.

Notice that string literals are different to numeric literals. The type of a string literal is const char[] - which you can assign to a char* pointer. An integer literal is just type int.

Also note that a numeric literal does not need to be stored in the data segment - in most cases such literals are placed directly in the machine code instructions. Thus there is no address which you could point p towards.

If you tried to do this (as per your comment):

int *p;
*p = 5;

what you are actually saying is that you want to store the number 5 into the location pointed to by p (which would be undefined in this case, since we never set p to anything). You would probably get a segfault.

If you tried to do this:

int *p;
p = 5;

what you would be telling the compiler to do is to convert the value 5 into a pointer to an integer and store that in p, with the result that the pointer p now points at address 5. And you would probably get a warning like this:

t.c:7: warning: assignment makes pointer from integer without a cast

In other words, you are trying to convert an integer to a pointer - probably not what you thought you were trying to do.

like image 180
harmic Avatar answered Jan 21 '23 00:01

harmic


In C, string literal like "Hello" will be exaluated to a char * type value, i.e. its base address, you could use it to initialize a pointer to char.

In the runtime, string literals usually is located in some read-only memory segmentation.

By the way, it looks like you confused pointer variable and the address pointed to by a pointer variable.

  1. By statements like char *p;, you defined a pointer variable, compiler will allocate the necessary memory to store this variable. But in this time, this pointer has not been initialized, so it does not have a determined value, which means it could point to anywhere.

  2. By statements like p = "Hello";, you assigned pointer p a value, now it points to some determined memory address, this memory segmentation could be allocated by compiler, or be allocated by yourself, for example p = malloc(...);.

like image 44
Lee Duhem Avatar answered Jan 21 '23 01:01

Lee Duhem