-1

The following program will print on the screen "Hello\nWorld\n" ('\n' = line down) as it supposed to. But actually, as i learned, something here isn't done as it should be. The "hello" and "world" strings are defined inside a function (and therefore are local and their memory is released at the end of the function's scope - right?). Actually we don't do a malloc for them as we are supposed to (to save the memory after the scope). So when a() is done, isn't the memory stack move up it's cursor and "world" will be placed in the memory at the same place where "hello" was ? (it looks like it doesn't happen here and I don't understand why, and therefore, why do i usually need to do this malloc if actually the memory block is saved and not returned after the scope?)

Thanks.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *a()
{
    char *str1 = "Hello";
    return str1;
}

char *b()
{
    char *str2 = "World";
    return str2;
}

int main()
{
    char *main_str1 = a();
    char *main_str2 = b();
    puts(main_str1);
    puts(main_str2);
    return 0;

}

edit: So what you are saying actually is that my "hello" string takes a constant place in memory and even though it's inside a function , i can read it from anywhere i want if i have it's address (so its defined just like a malloc but you cant free it) - right ?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
elhamura
  • 3
  • 2

4 Answers4

0

Constant strings are not allocated on the stack. Only the pointer is allocated on the stack. The pointer returned from a() and b() points to some literal constant part of executable memory. Another question dealing with this topic

Community
  • 1
  • 1
LRA
  • 462
  • 3
  • 8
0

In this case all works because string literal are allocated in memory data available for all program lifetime.
Your code is equivalent to (produce same result,I mean):

char *a()
{
    return "Hello";
}

This code doesn't work

char* a()
{
  char array[6];
  strcpy(array,"Hello");
  return array;
}

because array[] is created on stack and destroyed when function returning

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
0

String literals (strings that are defined with "quotes") are created statically in the program's memory space at compile-time. When you go char *str1 = "Hello";, you aren't creating new memory at run-time like you would with a malloc call.

Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
0

C does not obligate the compiler to move memory on the stack as OP suggests and that is why the observed behavior is not failing as expected.

Compiler models and optimizations may allow a program, such as OP's with undefined behavior (UB), to apparently work without side effects like corrupt memory or seg faults. Another compiler may also compile the same code with very different results.

Version with allocated memory follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *a() {
  return strdup("Hello"); // combo strlen, malloc and memcpy
}

char *b() {
  return strdup("World");
}

int main() {
  char *main_str1 = a();
  char *main_str2 = b();
  puts(main_str1);
  puts(main_str2);
  free(main_str1);
  free(main_str2);
  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256