Allocate a length of new C++ string dynamically

Dec 11, 2019 at 3:06am
How to create/allocate a length of new C++ string variable in in runtime ,ie. dynamically
Dec 11, 2019 at 3:08am
1
2
3
4
5
6
7
std::string *D;

std::cout << "Enter Size of String: ";
int in;
std::cin >> in;

D= new string[in];


^From there, you can now access "D" as an array with "in" amount of elements.
Dec 11, 2019 at 3:46am
Allocate a length of new C++ string dynamically

http://www.cplusplus.com/reference/string/string/resize/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
   std::string str("I like to code in C");
   std::cout << str << '\n';

   str.resize(str.size() + 2, '+');
   std::cout << str << '\n';

   str.resize(14);
   std::cout << str << '\n';
}

I like to code in C
I like to code in C++
I like to code

zapshe, DON'T! DO! THAT!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main()
{
   // create an empty string
   std::string str;
   std::cout << str.size() << '\n';

   str.resize(14);
   std::cout << str.size() << '\n';

   // there is a std::string fill constructor
   std::string str2(12, 'x');

   std::cout << str2 << ", " << str2.size() << '\n';
}

0
14
xxxxxxxxxxxx, 12

A std::string can be accessed as an array without the need to much around with a pointer and manually allocated heap memory.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main()
{
   std::string str("I like to code in C++");
   std::cout << str << '\n';

   std::cout << str[3] << '\n';
   std::cout << str.at(5) << '\n';
}

I like to code in C++
i
e
Dec 11, 2019 at 3:04pm
... c++ strings already are dynamic. if you want a dynamic array of strings, you can use vector, which is also dynamic. if you want to preallocate space rather than let it do it for you, that is a different question.
if you want to do a c-string, consider not doing that and using string instead, but its just basic pointer ops... char* cs = new char[somemaxsize]; ...use it... delete[] cs;

Dec 12, 2019 at 10:46am
if you want to do a c-string, consider not doing that and using string instead

If you need a C string (const char*) for some reason you can always call the c_str() member function.

1
2
std::string myString = "abc123";
legacyCFunction(myString.c_str());
Dec 12, 2019 at 2:09pm
you can also just take &var[0] but if you modify it you will be sorry. Sometimes you get a 'no can do on const' issue with the above, even if you didn't modify it. And that pointer can degrade if the string decides to grow its internal memory, like an iterator. Its best avoided, but sometimes you have to.
Last edited on Dec 12, 2019 at 2:11pm
Dec 13, 2019 at 11:54am
you can also just take &var[0]

If you need a non-const char* you can just use the data() member function (since C++17). The need for this should be pretty rare though. It's mostly for interaction with old C functions.
Last edited on Dec 13, 2019 at 11:54am
Topic archived. No new replies allowed.