Reputation: 23
I was wondering if it is possible to dynamically allocate a string literal?
I know you are able to do
char * letter = new char('b');
but is it possible to allocate a string literal like that
For example:
char * letter = new char("Hello");
Upvotes: 0
Views: 14697
Reputation: 1
string* p=new string {"Dynamically allocated string"};
strings can be dynamically allocated using the above syntax
Upvotes: 0
Reputation: 1
What you can and should do is use std::string and initialize it from a literal like
std::string mystring{"Hello"};
When you do that, mystring
is allocated on the stack and its content is initialized from "Hello"
(perhaps by copying it, but read about small string optimization, or short string optimization).
When that string is destroyed (e.g. at the end of the containing block), its storage (and perhaps the internal copy, if one was made) will be released.
If you insist for having a pointer to a heap-allocated C string, you could use strdup
(then release it with free
, not delete
); if you want a raw array of char
, you could use
char* p = new char[sizeof("Hello")]; // or new char[6]
strcpy(p,"Hello");
but this works only for literal strings like "Hello"
and you should use the same literal string "Hello"
twice (or use strlen("Hello")+1
instead of sizeof("Hello")
, which is 6, because of the terminating zero byte).
Notice that in all cases you never dynamically allocate a string literal. A compiler is permitted to (and often does) put the literal string in the read-only code segment of the produced binary executable. What you can do is allocate in heap a memory zone filled with a copy of that literal string (or use a pointer const char* pc = "hello";
)
Upvotes: 3
Reputation: 385108
You can never allocate a literal, dynamically or otherwise. Literals are literals; they are baked-in by definition.
You can certainly dynamically allocate a char
and initialise it from a literal, as you've done in your example.
As for your question, though, initialising from string literals is a bit more tricky, because you can only get your own char[N]
from string literal initialisation using the normal initialisation syntax:
char[] str = "hello"; // special rules apply here
and it is not possible to initialise a dynamically-allocated array. There's just no way to do it in the new[]
syntax.
You could dynamically allocate a pointer to a string literal:
const char** s = new const char*("hello");
but that's totally useless.
The closest I think you can get is:
char* str = new char[5]{'h','e','l','l','o'};
But, honestly, just use a std::string
instead:
std::string str("hello");
It was invented specifically to solve these kinds of problems.
Upvotes: 2
Reputation: 3
Try like this
int size = 6;
char * letter = new char[size];
strcpy(letter, "Hello");
std::cout << letter;
delete[] letter;
But remember to delete your variable because in this example your variable 'letter' assigned in 'heap'
Upvotes: 0
Reputation: 3904
No it is not because when you are writing
char * letter = new char("Hello");
you are declaring a pointer to a character string but assigning it a value that is a constant char which is not possible.
Upvotes: -2