-6

What is the difference between C Style Strings

char str[10]="Hello";

char str[]="Hello";

char* str= "Hello";

1) I believe that char str[10]="Hello" is automatic variable and stored on the stack.True? i.e. Allocates 10 bytes on stack.

2) Does char str[]="Hello"; is also stored on stack? i.e. allocates 6 bytes - including null character on stack.

3) Does char* str= "Hello"; stores pointer str on stack and the object "Hello" is stored on heap? i.e. allocates 6 bytes - including null character on heap.

4) All strings (in question 1,2 and 3) are null terminated . True/False?

5) Whether it is C or C++ whenever we create an string like "Hello" , it is always null terminated. Suppose in C++ we declare string str = "Hello"; , is it also null Terminated?

EDIT

Consider All declared in main().

@Negative points and close requests. I am asking this question with respect to where they are stored heap or stack? And also null termination.

Gaurav K
  • 2,864
  • 9
  • 39
  • 68
  • The third declaration is plain wrong and 5 depends on which C++ standard is relevant. – chris May 18 '13 at 06:04
  • It all depends on where you're declaring them. – Ignacio Vazquez-Abrams May 18 '13 at 06:04
  • "The third declaration is plain wrong" -- Um, how so? "5 depends on which C++ standard is relevant" -- C++11 mandates NUL termination but implementations were allowed to NUL-terminate by previous standards. – Jim Balter May 18 '13 at 06:19
  • P.S. Correction: NUL-terminating a C++ string would be severely broken. -- that would imply that the NUL was part of the string value, which it is not. C++11 mandates that s.c_str() returns &s[0], which is a different matter entirely. It means that the implementation must allocate a NUL byte following the string data, but it doesn't imply that the NUL is accessible by the user. – Jim Balter May 18 '13 at 06:48
  • Additionally to the rant at the end of @JimBalters answer, a compiler may perform optimisations. Some optimisations might change the lifetime of objects or eliminate them entirely. This renders any definitive answer to any of these questions invalid. – autistic May 18 '13 at 07:06
  • Someone has an odd idea about what a "rant" is. – Jim Balter May 18 '13 at 08:03

2 Answers2

2

"Consider All declared in main()."

Then

1) Yes.

2) Yes.

3) Yes, and no (it's stored neither on the stack nor in the heap in common implementations). "i.e. allocates 6 bytes" -- you seem to have forgotten about the memory required for the pointer. Also, there's an erroneous claim in the comments and in another answer that char* str= "Hello"; is wrong, but in fact it is legal C and, for now, legal C++ ... see What is the type of string literals in C and C++?

4) True, but it would be false if you changed 10 to 5 -- that is, given char str[5]="Hello";, str is not NUL-terminated.

5) False and no (although the implementation might store a NUL following the string -- C++11 requires it -- but that isn't part of the string).


"I am asking this question with respect to where they are stored heap or stack?"

Where do people get the idea that these are the only sorts of memory? Local variables are stored on the stack and memory allocated via malloc or (non-placement) new is allocated from the heap. Program code, file-scope variables, and literals fall into neither of those categories.

Community
  • 1
  • 1
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
0

You are looking at this kind of sideways, which is probably why you are confused ;-)

1) If these variables are all declared inside a routine definition, without the static keyword, then they are all on the stack.

BUT char str[10] and char str[] are arrays - you get all characters of the array on the stack. char *str is a pointer to one or more characters. Only the pointer is sure to be on the stack.

2) "Hello" always represents a NULL terminated string in C - it's 6 char's long. If you wanted to initialize a character array to contain a set of characters which is not NULL terminated, you can't do it this way.

3) As people have pointed out in the comments, it's unclear what char *str = "Hello"; does, or even whether it's legal. If it were char const *str = "Hello"; and the compiler accepted it, I'd expect to find the 6 character string somewhere anonymous, global, and possibly protected.

4) I haven't a clue what the "string" class does in C++.

Arlie Stephens
  • 1,146
  • 6
  • 20
  • "it's unclear what `char *str = "Hello";` does, or even whether it's legal" -- no, it's not unclear at all, and it's certainly legal in both C and C++; see http://stackoverflow.com/questions/2245664/string-literals-in-c – Jim Balter May 18 '13 at 08:06