+ 1
How to solve "std::bad_alloc" in the code?
30 Respuestas
+ 2
Mohammed
May you confusing between dynamic memory allocated array and dynamic array ( means which updates it's sizes automatically ).. ...
In c/c++/java, arrays are not dynamic data types. They are static only in nature.
The link you provided are about dynamic *memory allocation. , not about dynamic arrays.. The vector in c++ are called dynamic arrays in nature, which can extend there size as much as you needed automatically. But arrays are not dynamic, they are static, not extending memory which are allocated previously.. This is not possible by arrays.. But you can assign new memory by pointers dynamically and point to previous declared arrays, which gets new memory, not extended memory.
+ 2
Your string array length is 1 only.. Adding 2nd time will raise error..
Change to : size 1 to 2
like :
string* array1=new string[2];
string* array2=new string[2];
string* array3=new string[2];
+ 2
Mohammed
Assign enough size if you know size prior else go with vector..
+ 2
Mohammed
Pointers are always difficult to understand ( may be for me only)..
Nothing is impossible..
You are actually just declaring it as pointer but using it as array.. And better to use array notation to be clear understand it..
hope it helps..
+ 2
allocate and deallocate both, from heap memory at runtime.. rathar than compile time stack memory..
+ 1
Jayakrishna🇮🇳
How if I added another item, the same issue.
+ 1
Jayakrishna🇮🇳
Is it difficult to accomplish the task with pointers or impossible?
+ 1
Jayakrishna🇮🇳
That helps,
I am surprised about how it works if you comment lines(7, 8, 16, 18).
+ 1
Jayakrishna🇮🇳
This is the point, why it can add only one array of pointers if you comment the other two?, It allocated the first array successfully, why it can't allocate the other two?
+ 1
Of course the comments are not instructions, but I mean the other active array (not commented) can allocate memory whether the other arrays are commented or not, but if you uncomment the two arrays, the memory can't be allocated for them (bad_alloc).
+ 1
Mohammed
No. It allocates.
bad_alloc means you are accessing not existing, not allocated memory. .
It does not depends on other arrays... unless memory available.. Can you show the code, if this is not what you looking...!!
+ 1
Jayakrishna🇮🇳
So why I use dynamic array if I can't get the benefit in the next article ?
https://www.cplusplus.com/doc/tutorial/dynamic/
My main problem is when I allocate multiple dynamic arrays it doesn't work.
But it works in a single dynamic array like the following code.
https://code.sololearn.com/c3AYnBG13wen/?ref=app
+ 1
Jayakrishna🇮🇳
I think you meant arr2[...]=n2, arr[3]=n3.
Please have a look at my previous code, it works although I initialize the size to 1, and this is what should be because of using dynamic arrays.
But for multiple dynamic arrays it throws "bad_alloc".
+ 1
Can you tell what is the purpose of add_item() function in your code?
Your array size is 1 but you are trying to add 3 items.. So won't fit. Raise error. It don't have deal with other arrays.. Those are independent.
In my program, I have taken array size 5 for each so i can add upto 5 in each.
If I have sizes 3 , 1 , 1 for 3 arrays respectively I can add 3 items in 1st array and only 1 , 1 each on next arrays because it's size is 1 only.. array2, array3 will not stop array1 to add 3 items... 3 are independent.
If you understand, function purpose , what happening in, you can understand it will.
Hope it clears..
+ 1
Jayakrishna🇮🇳
Now I understand you, Thank you very much and thanks for your patience.
+ 1
Mohammed
Don't do C in C++.
If you just want to concatenate strings, just add the strings directly.
Take advantage of C++ features as much as possible.
Here is a discussion related to the topic:
https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new
https://code.sololearn.com/cE2w8u4qd7XE/?ref=app
0
FF9900
Will initializing them with large numbers make them upgradable?
0
FF9900
It's not a homework.
I haven't tried it using vectors, but I guess pushback solve it.
0
If you comments, means you just not creating , adding into or using arrays string1, string2 ..