R
Rakesh Kumar
Hi All -
In a project of mine - I was trying to scale down the actual issue
to the following piece of code. I need to allocate an array of strings
and reserve the individual string to a particular size (4K) .
I wrote 2 functions - allocVectorOfStrings() and
allocArrayOfStrings().
Each of them seem to allocate similar amounts of memory - but the
version of vectorOfStrings seem to crash with the following error -
"double free or corruption (out): 0x08055ff8 ***" .
I was just curious if I am doing anything fundamentally wrong here
to cause the issue.
#include <iostream>
#include <cstdlib>
#include <vector>
void allocVectorOfStrings();
void allocArrayOfStrings();
void allocVectorOfStrings() {
std::vector<std::string> * vt = new std::vector<std::string>();
vt->reserve(50);
for (size_t i = 0; i < vt->capacity(); ++i) {
std::cout << "We are probably ok" << i << "\n";
vt->operator[](i).reserve(40);
}
delete vt;
}
void allocArrayOfStrings() {
std::string * vt = new std::string[4096];
for (size_t i = 0; i < 1024; ++i) {
std::cout << "We are probably ok" << i << "\n";
vt.reserve(4096);
}
delete [] vt;
}
int main()
{
allocArrayOfStrings();
allocVectorOfStrings(); //This function crashes
return EXIT_SUCCESS;
}
In a project of mine - I was trying to scale down the actual issue
to the following piece of code. I need to allocate an array of strings
and reserve the individual string to a particular size (4K) .
I wrote 2 functions - allocVectorOfStrings() and
allocArrayOfStrings().
Each of them seem to allocate similar amounts of memory - but the
version of vectorOfStrings seem to crash with the following error -
"double free or corruption (out): 0x08055ff8 ***" .
I was just curious if I am doing anything fundamentally wrong here
to cause the issue.
#include <iostream>
#include <cstdlib>
#include <vector>
void allocVectorOfStrings();
void allocArrayOfStrings();
void allocVectorOfStrings() {
std::vector<std::string> * vt = new std::vector<std::string>();
vt->reserve(50);
for (size_t i = 0; i < vt->capacity(); ++i) {
std::cout << "We are probably ok" << i << "\n";
vt->operator[](i).reserve(40);
}
delete vt;
}
void allocArrayOfStrings() {
std::string * vt = new std::string[4096];
for (size_t i = 0; i < 1024; ++i) {
std::cout << "We are probably ok" << i << "\n";
vt.reserve(4096);
}
delete [] vt;
}
int main()
{
allocArrayOfStrings();
allocVectorOfStrings(); //This function crashes
return EXIT_SUCCESS;
}