Dynamic allocation of 'string' arrays [duplicate] - c++

This question already has answers here:
In what cases do I use malloc and/or new?
(20 answers)
Dynamically allocated C array of strings
(6 answers)
Closed 5 years ago.
I tried this
string *codes = (string*)malloc(256*sizeof(string));
codes[0] = "";
in C++. But it didn't work, but when I tried
string *codes = new string[256];
codes[0] = "";
This worked.
I did not understand the basic idea behind why this is happening. Could someone please tell me.
Thanks

This is because new uses constructor of the given class (in your case: std::string) and malloc() doesn't do this.

Related

Dynamic memory allocaion in c++ [duplicate]

This question already has answers here:
What does "new int(100)" do?
(4 answers)
Closed 1 year ago.
I came across two similar statements but could not exactly find the diference between them. The statements are:
int *p = new int(75);
int *p = new int[75];
Can anyone help me in knowing the difference between the above two statements.
The first returns a pointer to the integer with value of 75, but the second returns a pointer to the array of 75 integers, which don't have a value.

allocate new Zero-sized array can have effective value? [duplicate]

This question already has answers here:
C++ new int[0] -- will it allocate memory?
(6 answers)
Closed 2 years ago.
// new T[0] allocate a zero sized array can have values?
auto pv=new int[0];
cout<<pv<<endl; //0x... ?
*pv=123;
cout<<*pv<<endl; //123 ?
delete[] pv;
Why?
if so, what's difference between new T[0] and new T[1]
Why can I set the value of 0 sized array ...?
It is legal to create a new int[0] (though at first glance it may not appear to be useful!).
However, your use of it is just like any other buffer overrun: your program has undefined behaviour.
C++ does not check array bounds for you. That's your job.
Going past them can appear to work; it can cause a crash; it can instantaneously transport the sun to another part of the galaxy.
Just don't do it.

get the length of a string and assign it to an array [duplicate]

This question already has answers here:
Array size at run time without dynamic allocation is allowed? [duplicate]
(8 answers)
Closed 4 years ago.
Total c++ newbie here.
I have a problem, where I can not assign the number of character in a string to an array size, like so..😓
string outStr;
ifstream input("read.txt");
getline(input, outStr);
int const n = outStr.length();
int arr[n];
error msg --> expression must have a constant value. although i have declared the "const"
Thanks in advance✌.
C++ does not support Variable Length Arrays. Use a std::vector instead.

How to set variable name as a string? c++ [duplicate]

This question already has answers here:
How to adress variable by name stored in another variable (C++)
(2 answers)
Closed 5 years ago.
std::string VariableName = "name";
int (VariableNameHere) = 5;
From my understanding of c++ what I am asking is most likely impossible. If it is please post possible alternative solutions. Thanks!
As you have it is not possible, you would need to have some kind of look-up system, such as:
std::map<std::string, int> variables;
...
variables["name"] = 5;

Use `delete []` when `new [0]` [duplicate]

This question already has answers here:
C++ new int[0] -- will it allocate memory?
(6 answers)
Closed 5 years ago.
I dynamically allocated an array (unsigned int n might have been passed as a parameter in a function):
int * ar = new int [n];
When I'm done using it:
delete [] ar;
But, what happens when n = 0?
Is allocate 0 ints the same as not allocating at all?
In which case, do bad things happen when I call delete?
It's ok to new a zero-sized array, and to delete it. This is more of a convenience than anything, as it means you don't have to write separate conditions for the 0 case.