473,799 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 - allocVectorOfSt rings() and
allocArrayOfStr ings().
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 allocVectorOfSt rings();
void allocArrayOfStr ings();

void allocVectorOfSt rings() {
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 allocArrayOfStr ings() {
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()
{
allocArrayOfStr ings();
allocVectorOfSt rings(); //This function crashes
return EXIT_SUCCESS;
}
Dec 20 '07 #1
4 2981
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 - allocVectorOfSt rings() and
allocArrayOfStr ings().
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 allocVectorOfSt rings();
void allocArrayOfStr ings();

void allocVectorOfSt rings() {
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 allocArrayOfStr ings() {
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()
{
allocArrayOfStr ings();
allocVectorOfSt rings(); //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 - allocVectorOfSt rings() and
allocArrayOfStr ings().
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 allocVectorOfSt rings();
void allocArrayOfStr ings();

void allocVectorOfSt rings() {
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 allocArrayOfStr ings() {
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()
{
allocArrayOfStr ings();
allocVectorOfSt rings(); //This function crashes
return EXIT_SUCCESS;

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

void allocVectorOfSt rings()
{
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
{
allocVectorOfSt rings();
}
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...@com Acast.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 - allocVectorOfSt rings() and
allocArrayOfStr ings().
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 allocVectorOfSt rings();
void allocArrayOfStr ings();
void allocVectorOfSt rings() {
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 allocVectorOfSt rings()
{
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 allocArrayOfStr ings() {
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()
{
allocArrayOfStr ings();
allocVectorOfSt rings(); //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...@com Acast.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 - allocVectorOfSt rings() and
allocArrayOfStr ings().
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 allocVectorOfSt rings();
void allocArrayOfStr ings();
void allocVectorOfSt rings() {
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 allocVectorOfSt rings()
{
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
3209
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 "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
1
18017
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 application using C++ and the STL for handling my data. Unfortunately, I must interact with a (vanilla) C API. I use vectors of strings (for simplicity and less memory hassle), but the function calls for this API require arrays of character...
10
3104
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
2568
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 load the vector, too. It just craps out when you return to the console, or in a Windows app, the message loop. I think it has something to do with the vector<float> going out of scope and trying to free memory, but don't know why it would do...
2
3193
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
5641
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 strings of any lengths into an array of pointers to char type variable. my problem is: given the declaration
20
2095
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 matrices, but as matrix dimension grows up it crashes in deterministic locations depending on data, machine and OS. In particular, this happens when I test it with 3000x3000 matrices (which take up more than 30MB) under three different machines/OSes....
19
6082
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. allocate an array of the same size as the vector and copy elements from the vector into the array */
4
3442
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 error: error: expected identifier before numeric constant error: expected ';' or '...' before numeric constant But i can declare vector<stringtestArray(50); inside my main
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.