0

Firstly, I give two code fragments of a C/C++ function that reveal different strategies that C/C++ manage memory allocation. I was been asked these questions in a job interview.:(

#1

char *func()
{
    char *p = "hello world";
    return p;
}

#2

char *func()
{
    char p[] = "hello world";
    return p;
}

Can people still get the string "hello world" when the func returns?
This answer is YES and NO respectively.
Because in #1, "hello world" is a string constant. And in #2, the storage place of "hello world" is STACK. But when writing in this way, static char p[] = "hello world";, the answer will be YES.

So, my question is that How C/C++ govern memory allocation of a function?
Recalling what my interviewer told me. I can remember something like, STACK/HEAP/DATA SEGMENT/PROGRAM SEGMENT/*. I wish that anyone can describe this accurately.
Thanks.


CONTENTS ADDED

"Knowing how your codes are managed in memory will help programmers write their code." This is what the interviewer said to me.
The examples given above are used to describe this. What I want to ask is that How are memory segments organized from the point of view of program. Answers that I expect may like

|___________________|  
|        STACK      |
|___________________|  
|        HEAP       |
|___________________|  
|   DATA SEGMENT    |
|___________________|  
|  PROGRAM SEGMENT  |
|___________________|  
|        ...        |  
|                   |  

From the point view of a program, I am not sure whether memory partition like this is correct. (And also, other segments that store specific kind of data may exist.)

jam
  • 27
  • 4

2 Answers2

1

First one is OK, the returned pointer is pointing to a constant literal (However modifying it is undefined behavior and it should be a const).

Second one may causes an undefined behavior, it returns a pointer to a local variable which will be destructed after exiting the function.

Note: Constant literals will be stored in DATA-SEGMENT (in most implementations) not in function's stack.

masoud
  • 55,379
  • 16
  • 141
  • 208
  • I was dissapointed to discover that `gcc -Wall` did not throw a warning for `char *p = "hello world";` :-( –  Jul 12 '14 at 09:23
  • I think it's because, that piece of code is valid in C (historically!) – masoud Jul 12 '14 at 09:24
1

As written, your question is too broad to fully address, so I will aim to address to specific bits of it here.

In the first example, we are returning the address of a string literal, which is usually stored in the program segment in a read-only part of process memory. You can read more in this answer.

char *p = "hello world";
return p;

In the second example, we are creating a character array char[] on the stack, and populating it by making a copy of the string literal. Whenever we declare a non-static array in C, the memory is always allocated on the stack. Since stack memory effectively goes away when the function returns, the pointer is invalidated on the function return.

char p[] = "hello world";
return p;

In the third and final example, we declare a static array. Inside a function, the static keyword means that the variable has a lifetime equal to that of the entire program. That is why returning a pointer to such a array is perfectly valid, even though it is a copy just like the second example.

static char p[] = "hello world";

If this sort of knowledge is interesting to you, I would recommend this slide deck for further study. It is very practical and discusses these questions in an interview context.

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329