473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok .

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[i].reserve(4096);
}
delete [] vt;
}

int main()
{
allocArrayOfStrings();
allocVectorOfStrings(); //This function crashes
return EXIT_SUCCESS;
}
Dec 20 '07 #1
4 2953
Rakesh Kumar wrote:
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);
'reserve' does not construct vector's elements. It only allocates
memory for constructing them later, when 'insert' is used. Here
'*vt' does not contain _any strings_. It's empty. It's _capable_
of containing at least 50 without reallocation of its storage. But
it doesn't have any elements.
for (size_t i = 0; i < vt->capacity(); ++i) {
This is a very bad idea. Never iterate to capacity. Always
iterate to size.
std::cout << "We are probably ok" << i << "\n";
No, you're not OK.
vt->operator[](i).reserve(40);
You're accessing a non-existent element at the index 'i' and
then calls a member functions for it. Undefined behaviour.
}
delete vt;
}

void allocArrayOfStrings() {
std::string * vt = new std::string[4096];
Here 'vt' contains all 4096 elements.
for (size_t i = 0; i < 1024; ++i) {
std::cout << "We are probably ok" << i << "\n";
Yes, you are.
vt[i].reserve(4096);
'vt[i]' represents a real string. You can call 'reserve' for
it, no problem.
}
delete [] vt;
}

int main()
{
allocArrayOfStrings();
allocVectorOfStrings(); //This function crashes
return EXIT_SUCCESS;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 20 '07 #2
On Dec 20, 11:10 am, Rakesh Kumar <rakesh.use...@gmail.comwrote:
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);
Try not to use new / delete unless you are required to do so.
You are attempting to access an element that doesn't yet exist.
reserve constructs nothing.
use vector's member function at(...) when in doubt. see below.
}
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[i].reserve(4096);
}
delete [] vt;

}

int main()
{
allocArrayOfStrings();
allocVectorOfStrings(); //This function crashes
return EXIT_SUCCESS;

}
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>

void allocVectorOfStrings()
{
std::vector<std::stringvt(10, "default string");
std::cout << "vt's size is " << vt.size() << std::endl;
for(size_t i = 0; i < vt.size(); ++i)
{
std::cout << "vt[ " << i << " ] ";
std::cout << vt.at(i) << std::endl;
}
// uncomment for a test
// vt.at(10) = "out or range";
}

int main()
{
try
{
allocVectorOfStrings();
}
catch( const std::exception& e)
{
std::cout << "Error:";
std::cout << e.what() << std::endl;
}
}

/*
vt's size is 10
vt[ 0 ] default string
....
vt[ 9 ] default string
*/
Dec 20 '07 #3
On Dec 20, 8:17 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rakesh Kumar wrote:
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);

'reserve' does not construct vector's elements. It only allocates
memory for constructing them later, when 'insert' is used. Here
'*vt' does not contain _any strings_. It's empty. It's _capable_
of containing at least 50 without reallocation of its storage. But
it doesn't have any elements.
for (size_t i = 0; i < vt->capacity(); ++i) {

This is a very bad idea. Never iterate to capacity. Always
iterate to size.
std::cout << "We are probably ok" << i << "\n";

No, you're not OK.
vt->operator[](i).reserve(40);

You're accessing a non-existent element at the index 'i' and
then calls a member functions for it. Undefined behaviour.
Thanks Victor.
The revised function seems to do what I intended in the first place.

void allocVectorOfStrings()
{
std::vector<std::stringvt(1024);

for (size_t i = 0; i < vt.size(); ++i)
{
std::cout << "Vector seems to be ok too" << i << "\n";
vt[i].reserve(4096);
}
}

Just a quick question - after a vector is allocated - is there a way I
can mass construct elements at one shot (instead of using insert /
push_back ) - similar to the construct shown above.

>
}
delete vt;
}
void allocArrayOfStrings() {
std::string * vt = new std::string[4096];

Here 'vt' contains all 4096 elements.
for (size_t i = 0; i < 1024; ++i) {
std::cout << "We are probably ok" << i << "\n";

Yes, you are.
vt[i].reserve(4096);

'vt[i]' represents a real string. You can call 'reserve' for
it, no problem.
}
delete [] vt;
}
int main()
{
allocArrayOfStrings();
allocVectorOfStrings(); //This function crashes
return EXIT_SUCCESS;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 20 '07 #4
On Dec 20, 6:56 pm, Rakesh Kumar <rakesh.use...@gmail.comwrote:
On Dec 20, 8:17 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rakesh Kumar wrote:
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);
'reserve' does not construct vector's elements. It only allocates
memory for constructing them later, when 'insert' is used. Here
'*vt' does not contain _any strings_. It's empty. It's _capable_
of containing at least 50 without reallocation of its storage. But
it doesn't have any elements.
for (size_t i = 0; i < vt->capacity(); ++i) {
This is a very bad idea. Never iterate to capacity. Always
iterate to size.
std::cout << "We are probably ok" << i << "\n";
No, you're not OK.
vt->operator[](i).reserve(40);
You're accessing a non-existent element at the index 'i' and
then calls a member functions for it. Undefined behaviour.

Thanks Victor.
The revised function seems to do what I intended in the first place.

void allocVectorOfStrings()
{
std::vector<std::stringvt(1024);

for (size_t i = 0; i < vt.size(); ++i)
{
std::cout << "Vector seems to be ok too" << i << "\n";
vt[i].reserve(4096);
}

}

Just a quick question - after a vector is allocated - is there a way I
can mass construct elements at one shot (instead of using insert /
push_back ) - similar to the construct shown above.
vt.resize( x );

After resize is used the vector will contain x elements. You could
also re-assign to it or use swap e.g:

vt = std::vector<std::string>( x ); //or

std::vector<std::stringv( x )
vt.swap( v );

I think resize would most probably be the best option though.

Regards,

Werner
Dec 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
10
by: dalbosco | last post by:
Hello, I am new to STL and I've written the following code which crash. Could anyone tell me why this code crash? Thanks by advance. -- J-F #include <iostream>
18
by: Active8 | last post by:
I put the bare essentials in a console app. http://home.earthlink.net/~mcolasono/tmp/degub.zip Opening output.fft and loading it into a vector<float> screws up, but input1.dat doesn't. It does...
2
by: laniik | last post by:
Hi. For some reason I am getting a crash on pop_back() and Im not sure why. sorry I cant post the whole code because the vector is used in a bunch of places. i have a vector<bool> complete;
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
20
by: Neclepsio | last post by:
Hi everyone. I've made a class Matrix, which contains a pointer to the data and some methods which all return a copy of the matrix modified in some way. The program works quite well for small...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
4
by: kungfuelmosan | last post by:
Hey guys, Im just getting into c++ at the moment so please bare with me Basically i need to declare a vector<stringstringArray(50) inside a class, however by doing so i am getting the following...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.