473,545 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Seg fault!: passing vector<vector<i nt> > to a function

Some time ago, I had a segment of code like

vector<vector<i nt example;
f(example);

and inside f, I defined vector<int>'s and used push_back to get them
inside example. I got a segmentation fault which I resolved by doing

vector<vector<i nt example;
example.push_ba ck(vector<int>( ));
f(example);

I also remember that when debugging, I got the segmentation fault
right at the function call. I wonder why is it that I needed to do
this when the original code works if I had had a

vector<intexamp le
f(example)

and just pushed back integers inside f

I'm sorry because I don't have the actual code, this is a question
I'va had for a long time now and I simply don't have the code, but the
question is, can I pass vectors of vectors to a function without
initialising them and why if the answer is no?
Thanks

Daniel

Feb 9 '07 #1
2 3297
da************* @gmail.com wrote:
Some time ago, I had a segment of code like

vector<vector<i nt example;
f(example);

and inside f, I defined vector<int>'s and used push_back to get them
inside example. I got a segmentation fault which I resolved by doing

vector<vector<i nt example;
example.push_ba ck(vector<int>( ));
f(example);
There is no reason for this code to have undefined behaviour. The
usual UB cause is the attempted use of non-existent vector contents.
If you pushed an empty vector, that empty vector does not acquire
any 'int' elements until you push them too.
I also remember that when debugging, I got the segmentation fault
right at the function call.
Well, my memory is also known to fail me now and then. No need to
worry.
I wonder why is it that I needed to do
this when the original code works if I had had a

vector<intexamp le
f(example)

and just pushed back integers inside f

I'm sorry because I don't have the actual code, this is a question
I'va had for a long time now and I simply don't have the code, but the
question is, can I pass vectors of vectors to a function without
initialising them and why if the answer is no?
You _can_ pass vectors. They are not "without initialising". They
are *default-initialised*. They don't contain any elements, that's
true. So any attempted use of operator[] will most likely fail.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 9 '07 #2
On 9 Feb 2007 04:59:24 -0800 in comp.lang.c++,
"da************ *@gmail.com" <da************ *@gmail.comwrot e,
>and inside f, I defined vector<int>'s and used push_back to get them
inside example. I got a segmentation fault which I resolved by doing

vector<vector< int example;
example.push_b ack(vector<int> ());
f(example);
Inside f() you probably do the equivalent of
example[0].push_back(42);

This fails if example[0] hasn't been created!

Feb 9 '07 #3

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

Similar topics

8
4102
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
39
7605
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down indicate: a) That I don't know enough b) Passing arguments by ref is bad
4
5004
by: Pushkar Pradhan | last post by:
I want a function to execute another function, which I pass to it, sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style. double exec_basecase(void (*func)(double *a, double *b, double *c), double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB) { ...../*other code*/ func(a, b, c); ..... }
7
5168
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving this. Ok... here goes.... I have a function that passes a pointer to a string to another function. For example: int FunctionA ()
2
2489
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
11
7690
by: jtagpgmr | last post by:
I am currently using the gcc compiler on a cygwin platform, I am a beginner when it comes to programming in C and want to know why anytime I run the .exe with the following code I get a "segmentation fault (core dumped)" error: #include <stdio.h> main() {
3
3008
by: Abubakar | last post by:
Hi, I compiled a simple code in c++/cli 2k5 public ref class Class1 { public : void doit (char * c ) { }
6
7867
by: Kiran | last post by:
Hi all, What I am trying to do is to pass a pointer to the first element of an array to a function, modify it in that function, and then print out the values of the array (which has been modified by the function) in main. However, I am getting a segmentation fault. Here is the code: (Please note, the size is fixed in this code, but in my...
3
1446
by: dorandoran | last post by:
function openMyWindow() { var myID = document.getElementById('ctl00_maincontent_txtEmpID').value; //var fname = document.getElementById('<%= lblFullName.ClientID %>').value; //var lname = document.getElementById('ctl00_maincontent_txtlname').value; var fname = document.getElementById('ctl00_maincontent_txtfname').value; ...
0
7425
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...
0
7935
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7449
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...
0
7780
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...
0
5069
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3479
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...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
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
0
734
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...

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.