What happens with memory when we reassign value to char pointer?

I wonder what happens inside the memory when we do something like this

  char *s;
  s = "Text";
  s = "Another Text";

If I'm getting it right, by assigning string to char pointer memory is dynamically allocated. So according to my understanding assignment expression

  s = "Text";

equals to

  s = (char *) malloc(5); // "Text" + '\0'
  strcpy(s, "Text");

Well, this way we can easily free memory by using

  free(s);

But... After reassigning same pointer to another value, it allocates new memory segment to store that value.

  s = "Text";
  printf("\n(%p) s = \"%s\"", s, s);

  s = "Another Text";
  printf("\n(%p) s = \"%s\"", s, s);

Output:

  (0x400614) s = "Text"
  (0x400628) s = "Another Text"

That means that address of old value is not accessible to us any longer and we can't free that any more. Another call to free(s); will probably deallocate only last memory segment used by that pointer.

My question is: If we reassign same char pointer over and over again, does it consume more and more program memory during run-time or that garbage somehow gets automatically freed?

I hope that was enough to demonstrate my problem, couldn't think better example. If something's not clear enough please ask for additional clarification.


Your understanding is wrong. It is just the assignment and it does not allocate any memory. In your example you assign the pointer with the addresses of the string literals. String literals are created compile time and placed in the read only memory

You do now allocate any memory by assigning the pointer

Character Array and Character Pointer in C, The type of both the variables is a pointer to char or (char*) , so you can pass It allocates 12 consecutive bytes of memory and associates the address bytes for string literal "Hello World" and 4 extra bytes for pointer variable ptr . We can assign a new string to arr by using gets() , scanf() , strcpy() or by� If we send a pointer to memory to a function, any changes to the pointer itself will be ignored, but the function can dereference the pointer and make changes to memory that the pointer references. That memory is not part of the parameter list of the function and those changes will be reflected back to the caller.


It's not equal to doing a malloc. What's happening is that the string literal is stored in a read only part of memory. And it's not the assignment of a pointer that does the allocation. All string literals in a program are already allocated from start.

It might be worth mentioning that they are not strictly speaking stored in read only memory, but they might be and writing to a string literal is undefined behavior.

You cannot and should not call free on a string literal. Well, you can, but the program will likely crash.

Module 3: Pointers, strings, arrays, malloc, A pointer usually has an associated type, e.g., an int pointer vs. a char pointer. Add a second pointer variable that points to i and modifies i using only the of variable i into the pointer: intPtr = & i; // Print. printf ("The int at memory In this case, we examine the next address beyond i, which happens to be where j is stored. C++ does not make any guarantees about what will happen to the contents of deallocated memory, or to the value of the pointer being deleted. In most cases, the memory returned to the operating system will contain the same values it had before it was returned, and the pointer will be left pointing to the now deallocated memory.


Strings as arrays, as pointers, and string.h, where the beginning of the array is at some location in computer memory, for This means that when we use a char * to keep track of a string, the character array Note: Since we assigned the pointer the address of an array of characters , the That's also why you don't use an ampersand when you pass a string variable� We also can say its type is: int* The type is important. While pointers are all the same size, as they just store a memory address, we have to know what kind of thing they are pointing TO. double * dptr; // a pointer to a double char * c1; // a pointer to a character float * fptr; // a pointer to a float


C strings and C++ strings, It is also possible to declare a C string as a pointer to a char : If used improperly , it can easily result in corrupted program memory or runtime errors. You can obtain the length of a C string using the C library function strlen() . A character array (including a C string) can not have a new value assigned to it after it is� char ch = 'a'; float *ptr_int = &ch; ptr_int++; printf("%c",*ptr_int); In the above example, we are assigning address of a character variable to integer pointer and later on incrementing the pointer value by 1 integer, this will give the address 2004 to the integer pointer as the character was stored at 2000 location.


Pointer Basics and Pass-By-Address, A pointer is a variable that stores a memory address. Pointers are You may NOT assign arbitrary numbers to pointer variables: int * ip; char * cp; double * dp;. The following sets aside memory for a string and also a character pointer, and then initializes the pointer to point to the first character in the string. char *p = "hello"; While the following sets aside memory just for the string. So it can actually use less memory. char p[10] = "hello";


C/Pointers, Memory in a typical modern computer is divided into two classes: a small number of registers, 1 int *pointerToInt; 2 double *pointerToDouble; 3 char * pointerToChar; 4 char To initialize a pointer variable, you have to assign to it the address of To do so, you should convert the pointer to type void * first using a cast (see� The type of both the variables is a pointer to char or (char*), so you can pass either of them to a function whose formal argument accepts an array of characters or a character pointer. Here are the differences: arr is an array of 12 characters. When compiler sees the statement: