473,507 Members | 13,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

segmentation fault...

What's wrong with the following code? It seems I can't assign to a
vector<stringelement...

#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
#include <string>

using namespace std;

struct c{
vector<stringa;
void set_size(int n) { a.reserve(n); }
void set_val(int i, string v) { a[i] = v; }
};

void foo(c & cc){
cc.set_size(10);
cc.set_val(3, "value 3");
}

int main(){

vector<inta;
a.reserve(10);
a[3] = 10;
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
cout << "a[3]: " << a[3] << endl;
{
vector<stringa;
a.reserve(10);
a[3] = "value 3";
copy(a.begin(), a.end(), ostream_iterator<string>(cout, " "));
cout << "a[3]: " << a[3] << endl;
}

c cc;
foo(cc);
}
Jul 23 '07 #1
4 1664
In article <f8**********@aioe.org>, fe****@aepnetworks.com says...

[ ... ]
int main(){

vector<inta;
a.reserve(10);
a[3] = 10;
This gives undefined behavior. a.reserve(10) only _reserves_ space for
10 elements, but does NOT actually add those 10 elements to the vector.
This ensures that you can add 10 elements to 'a' without a reallocation
taking place, so (for example) all iterators into 'a' remain valid
until/unless you add an eleventh element. It does NOT, however, make it
valid to index into those 10 elements until you add them (e.g. with
push_back).

If you change the 'reserve' to 'resize', this part of the code will
become valid -- resize actually adds elements to the vector, so you can
then index into the vector and use those elements.

[ ... ]
vector<stringa;
a.reserve(10);
a[3] = "value 3";
This has the same problem, which can be fixed in the same way (i.e.
change 'reserve' to 'resize').

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 23 '07 #2
Fei Liu wrote:
What's wrong with the following code? It seems I can't assign to a
vector<stringelement...
[..]
RTFM about the differences between 'reserve' and 'resize'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 23 '07 #3
Fei Liu wrote:
What's wrong with the following code? It seems I can't assign to a
vector<stringelement...
You have no elements to assign to. I believe you don't understand what
reserve() actually does, it does not create any elements, it only reserves
space for them. Try using a.at(i) instead of a[i].

What you really want is the resize() member function of vector.
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
#include <string>
#include <algorithm>
>
using namespace std;

struct c{
vector<stringa;
void set_size(int n) { a.reserve(n); }
void set_val(int i, string v) { a[i] = v; }
};

void foo(c & cc){
cc.set_size(10);
cc.set_val(3, "value 3");
}

int main(){

vector<inta;
a.reserve(10);
a.size() == 0
a[3] = 10;
Undefined behaviour. Use
a.at(3) = 10;
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
OK. a.begin() == a.end()
cout << "a[3]: " << a[3] << endl;
UB.
{
vector<stringa;
a.reserve(10);
a.size() == 0
a[3] = "value 3";
Undefined behaviour. While the previous errors are also undefined behaviour,
i guess this is what actually _caused_ the crash? A likely reason is that
a[3] returns some raw memory, on which no string has been constructed.
copy(a.begin(), a.end(), ostream_iterator<string>(cout, " "));
again, a.begin() == a.end(), so this is a no-op.
cout << "a[3]: " << a[3] << endl;
But this is UB again.
}

c cc;
foo(cc);
And UB.
}
--
rbh
Jul 23 '07 #4
Jerry Coffin wrote:
In article <f8**********@aioe.org>, fe****@aepnetworks.com says...

[ ... ]
>int main(){

vector<inta;
a.reserve(10);
a[3] = 10;

This gives undefined behavior. a.reserve(10) only _reserves_ space for
10 elements, but does NOT actually add those 10 elements to the vector.
This ensures that you can add 10 elements to 'a' without a reallocation
taking place, so (for example) all iterators into 'a' remain valid
until/unless you add an eleventh element. It does NOT, however, make it
valid to index into those 10 elements until you add them (e.g. with
push_back).

If you change the 'reserve' to 'resize', this part of the code will
become valid -- resize actually adds elements to the vector, so you can
then index into the vector and use those elements.

[ ... ]
> vector<stringa;
a.reserve(10);
a[3] = "value 3";

This has the same problem, which can be fixed in the same way (i.e.
change 'reserve' to 'resize').
Thank you!
Jul 23 '07 #5

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

Similar topics

2
6794
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
1939
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
16
8959
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
11381
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
2979
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
26062
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
3318
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
5857
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
5132
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
5022
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
7221
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,...
0
7109
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...
0
7372
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...
1
7029
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...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
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...
0
1537
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 ...
0
411
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...

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.