0

My understanding is that in C and C++, creating a character array by calling:

char *s = "hello";

actually creates two objects: a read-only character array that is created in static space, meaning that it lives for the entire duration of the program, and a pointer to that memory. The pointer is a local variable to its scope then dies.

My question is what happens to the array when the pointer dies? If I execute the code above inside a function, does this mean I have a memory leak after I exit the function?

4
  • 1
    what you call "static" space is, from the point of view of the executable file, the "data" (or "rodata") segment. It's effectively a global variable, just you're not making it (globally) visible / you're not giving it a (global) name.
    – FrankH.
    Aug 9, 2013 at 14:35
  • The string is not necessarily read-only. It can be, and in environments where this is possible it probably is. See stackoverflow.com/questions/1704407/…
    – jarmod
    Aug 9, 2013 at 14:54
  • Just to amplify here, the "hello" is constructed when the program is linked. The section of code containing the assignment need never be called, but the string will be available.
    – user888379
    Aug 9, 2013 at 15:31
  • Note in C++ this uses a deprecated conversion see: C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] Jun 17, 2015 at 11:52

6 Answers 6

6

it lives for the entire duration of the program

Exactly, formally it has static storage duration.

what happens to the array when the pointer dies?

Nothing.

If I execute the code above inside a function, does this mean I have a memory leak after I exit the function?

No, because of (1). (The array is only "freed" when the program exits.)

1
  • More importantly, the array is only created once.
    – Luaan
    Jun 17, 2015 at 11:57
1

No, there is no leak.

The literal string is stored in the program's data section, which is typically loaded into a read-only memory page. All equivalent string literals will typically point to the same memory location -- it's a singleton, of sorts.

char const *a = "hello";
char const *b = "hello";

printf("%p %p\n", a, b);

This should display identical values for the two pointers, and successive calls to the same function should print the same values too.

(Note that you should declare such variables as char const * -- pointer to constant character -- since the data is shared. Modifying a string literal via a pointer is undefined behavior. At best you will crash your program if the memory page is read-only, and at worst you will change the value of every occurrence of that string literal in the entire program.)

2
  • 1
    "The literal string is stored in the program's data section" - say, on x86 using a compiler that emits an ELF executable. This is not universal :) The terminology is that it has static storage duration.
    – user529758
    Aug 9, 2013 at 14:36
  • It's far from universal that identical string literals share the same memory. At least one compiler I've used has an option to make string literals writable (as they were originally), and guarantees that each literal will occupy different memory (so that a modification to one doesn't modify the other). Aug 9, 2013 at 15:46
1

const char* s = "Hello"; is part of the code (program) - hence a constant never altered (unless you have some nasty mechanism altering code at runtime)

0

My question is what happens to the array when the pointer dies? If I execute the code above inside a function, does this mean I have a memory leak after I exit the function?

No there will be no memory leak and nothing happens to the array when the pointer dies.

A memory leak could be possible only with dynamic allocation, via malloc(). When you're malloc() something, you have to free() it later. If you don't, there will be a memory leak. In your case, it's a "static allocation": the allocation and free of this memory space will be freed automatically and you don't have to handle that.

0

does this mean I have a memory leak after I exit the function?

No, there is no memory leak, string literals have static duration and will be freed when the program is done. Quote from the C++ draft standard section 2.14.5 String literals subsection 8:

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration

Section 3.7.1 Static storage duration says:

[...] The storage for these entities shall last for the duration of the program

Note in C++, this line:

char *s = "hello";

uses a deprecated conversion see C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] for more details.

The correct way would be as follows:

const char *s = "hello";
-1

you only have to free if you use malloc or new

EDIT:

char* string = "a string"; memory allocation is static, and not good practice (if it will be constant the declaration should be a const char*) because this is in the stack when the function ends it should be destroyed along with the rest of the local variables and arguments. you need to use specific malloc/free and new/delete when you allocate the memory for your variable like: char *string = new char[64]; --> delete string; char *string = malloc(sizeof(char) * 64); --> free(string); //this is not best practice unless you have to use C

1
  • 1
    This is wrong. One has to use delete for every newed object. Also with no explanation on why this is the case, this is a very poor answer.
    – stefan
    Aug 9, 2013 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.