473,387 Members | 1,440 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.

returned vector of function is zeroized

The following code is a function called from the main. Sence is to
initialize a vector of the dimension of "a". "b" is the according value (all
the same in a vector).

The problem is that the initialization is done the right way (controled by
the output) but the returned vector is empty - meaning the pointer is
showing zero.

vector<int>* initialize (int a, vector<int>* vec, int b)

{

vector<int> ve;

vec=&ve;

ve.reserve(a);

for (int i=0; i<=a-1; i++)

ve[i]=b;

for (i=0; i<=a-1; i++)

cout << i << ". value : "

<< ve[i] << endl;

return vec;

ve.clear();

}

What is wrong???

I have tried it before without using a pointer - same result!
Jul 22 '05 #1
10 1712
Huibuh wrote:
The following code is a function called from the main. Sence is to
initialize a vector of the dimension of "a". "b" is the according
value (all the same in a vector).

The problem is that the initialization is done the right way
(controled by the output) but the returned vector is empty - meaning
the pointer is showing zero.

vector<int>* initialize (int a, vector<int>* vec, int b)

{

vector<int> ve;

vec=&ve;

ve.reserve(a);

for (int i=0; i<=a-1; i++)

ve[i]=b;

for (i=0; i<=a-1; i++)

cout << i << ". value : "

<< ve[i] << endl;

return vec;

ve.clear();

}

What is wrong???

I have tried it before without using a pointer - same result!


First, you're returning a address of a local. Also, I'm not sure what you're
trying to do with "vec", but I'm almost sure it isn't what you want.

What are you trying to do?

- Pete
Jul 22 '05 #2
Huibuh wrote:
The following code is a function called from the main. Sence is to
initialize a vector of the dimension of "a". "b" is the according
value (all the same in a vector).

The problem is that the initialization is done the right way
(controled by the output) but the returned vector is empty - meaning
the pointer is showing zero.

vector<int>* initialize (int a, vector<int>* vec, int b)

{

vector<int> ve;

vec=&ve;

ve.reserve(a);

for (int i=0; i<=a-1; i++)

ve[i]=b;

for (i=0; i<=a-1; i++)

cout << i << ". value : "

<< ve[i] << endl;

return vec;

ve.clear();

}

What is wrong???

I have tried it before without using a pointer - same result!


First, you're returning a address of a local. Also, I'm not sure what you're
trying to do with "vec", but I'm almost sure it isn't what you want.

What are you trying to do?

- Pete
Jul 22 '05 #3
On Mon, 5 Apr 2004 23:07:44 +0200, "Huibuh" <no*****@t-online.de> wrote:
The following code is a function called from the main. Sence is to
initialize a vector of the dimension of "a". "b" is the according value (all
the same in a vector).

The problem is that the initialization is done the right way (controled by
the output) but the returned vector is empty - meaning the pointer is
showing zero.

vector<int>* initialize (int a, vector<int>* vec, int b)

{

vector<int> ve;

vec=&ve;

ve.reserve(a);

for (int i=0; i<=a-1; i++)

ve[i]=b;

for (i=0; i<=a-1; i++)

cout << i << ". value : "

<< ve[i] << endl;

return vec;

ve.clear();

}

What is wrong???
You're creating an entire local vector in your function, setting it up, and
then it gets destroyed upon exit from the function. How about just
dispensing with the local vector and using the pointer your function is
given:

#include <iostream>
#include <vector>
using namespace std;
vector<int>* initialize (int a, vector<int>* vec, int b)

{

//vector<int> ve;

// vec=&ve;

vec->reserve(a);

for (int i=0; i<=a-1; i++)

(*vec)[i]=b;

for (i=0; i<=a-1; i++)

cout << i << ". value : "

<< (*vec)[i] << endl;

return vec;
}


int main()
{
vector<int> x;

vector<int> *vecp = initialize(10, &x, 1000);
cout << x[0] << " " << x[1] << " " << x[2] << endl;
return 0;
}

I have tried it before without using a pointer - same result!
Not sure what you mean, but not sure it matters. You /could/ have the
vector be returned by value, but that would be rather inefficient.
-leor


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4
On Mon, 5 Apr 2004 23:07:44 +0200, "Huibuh" <no*****@t-online.de> wrote:
The following code is a function called from the main. Sence is to
initialize a vector of the dimension of "a". "b" is the according value (all
the same in a vector).

The problem is that the initialization is done the right way (controled by
the output) but the returned vector is empty - meaning the pointer is
showing zero.

vector<int>* initialize (int a, vector<int>* vec, int b)

{

vector<int> ve;

vec=&ve;

ve.reserve(a);

for (int i=0; i<=a-1; i++)

ve[i]=b;

for (i=0; i<=a-1; i++)

cout << i << ". value : "

<< ve[i] << endl;

return vec;

ve.clear();

}

What is wrong???
You're creating an entire local vector in your function, setting it up, and
then it gets destroyed upon exit from the function. How about just
dispensing with the local vector and using the pointer your function is
given:

#include <iostream>
#include <vector>
using namespace std;
vector<int>* initialize (int a, vector<int>* vec, int b)

{

//vector<int> ve;

// vec=&ve;

vec->reserve(a);

for (int i=0; i<=a-1; i++)

(*vec)[i]=b;

for (i=0; i<=a-1; i++)

cout << i << ". value : "

<< (*vec)[i] << endl;

return vec;
}


int main()
{
vector<int> x;

vector<int> *vecp = initialize(10, &x, 1000);
cout << x[0] << " " << x[1] << " " << x[2] << endl;
return 0;
}

I have tried it before without using a pointer - same result!
Not sure what you mean, but not sure it matters. You /could/ have the
vector be returned by value, but that would be rather inefficient.
-leor


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #5
Huibuh wrote:
vector<int> ve;
vec=&ve;
Ugh! Never return the address of a local variable from a function. You
should be passing the address of where you want the results (or a
reference to them) into the function for something like this or
returning the actual vector if you want a copy of it.
return vec;
ve.clear();


Do you ever expect that clear function to be called? If so, how?
Jul 22 '05 #6
Huibuh wrote:
vector<int> ve;
vec=&ve;
Ugh! Never return the address of a local variable from a function. You
should be passing the address of where you want the results (or a
reference to them) into the function for something like this or
returning the actual vector if you want a copy of it.
return vec;
ve.clear();


Do you ever expect that clear function to be called? If so, how?
Jul 22 '05 #7

"Huibuh" <no*****@t-online.de> wrote in message
news:c4*************@news.t-online.com...
The following code is a function called from the main. Sence is to
initialize a vector of the dimension of "a". "b" is the according value (all the same in a vector).

The problem is that the initialization is done the right way (controled by
the output) but the returned vector is empty - meaning the pointer is
showing zero.

vector<int>* initialize (int a, vector<int>* vec, int b)

{

vector<int> ve;

vec=&ve;

ve.reserve(a);

for (int i=0; i<=a-1; i++)

ve[i]=b;

for (i=0; i<=a-1; i++)

cout << i << ". value : "

<< ve[i] << endl;

return vec;

ve.clear();

}

What is wrong???

I have tried it before without using a pointer - same result!


Don't use a pointer, why did you think a pointer would help? They make
things more complicated not less.

Also you use reserve wrongly, reserve does NOT change the size of a vector,
so your code is putting values in a vector with zero size.

Do it like this

vector<int> initialize (int a, int b)
{
vector<int> ve(a);
for (int i=0; i<=a-1; i++)
ve[i]=b;
return ve;
}

Simple really.

john
Jul 22 '05 #8

"Huibuh" <no*****@t-online.de> wrote in message
news:c4*************@news.t-online.com...
The following code is a function called from the main. Sence is to
initialize a vector of the dimension of "a". "b" is the according value (all the same in a vector).

The problem is that the initialization is done the right way (controled by
the output) but the returned vector is empty - meaning the pointer is
showing zero.

vector<int>* initialize (int a, vector<int>* vec, int b)

{

vector<int> ve;

vec=&ve;

ve.reserve(a);

for (int i=0; i<=a-1; i++)

ve[i]=b;

for (i=0; i<=a-1; i++)

cout << i << ". value : "

<< ve[i] << endl;

return vec;

ve.clear();

}

What is wrong???

I have tried it before without using a pointer - same result!


Don't use a pointer, why did you think a pointer would help? They make
things more complicated not less.

Also you use reserve wrongly, reserve does NOT change the size of a vector,
so your code is putting values in a vector with zero size.

Do it like this

vector<int> initialize (int a, int b)
{
vector<int> ve(a);
for (int i=0; i<=a-1; i++)
ve[i]=b;
return ve;
}

Simple really.

john
Jul 22 '05 #9
Thanks to all of you, your help was very appreciated and I finally was
successful. But now I have a similar problem with RETURN correlated with a
pointer. If you are interested please see my threat

pointer return: address correct / value nonesense

Happy eastern

Jul 22 '05 #10
Thanks to all of you, your help was very appreciated and I finally was
successful. But now I have a similar problem with RETURN correlated with a
pointer. If you are interested please see my threat

pointer return: address correct / value nonesense

Happy eastern

Jul 22 '05 #11

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

Similar topics

1
by: Alan Benn | last post by:
(VC6) When I use the STL <vector> template as follows: #include <vector> .... vector<CString> m_nameList; // Names of the chips I get these compiler warnings : C:\Program...
4
by: Vish | last post by:
I cannot compile a simple code as below using cygwin. I believe I am using latest version of g++(3.3.1) I get following error is_equal.cpp:9:19: ccc.cpp: No such file or directory...
2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
10
by: Huibuh | last post by:
The following code is a function called from the main. Sence is to initialize a vector of the dimension of "a". "b" is the according value (all the same in a vector). The problem is that the...
12
by: ypjofficial | last post by:
Hello All, I need to return a vector from a function.The vector is of int and it can contain as many as 1000 elements.Earlier I was doing //function definition vector<intretIntVector() {...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
1
by: Hasan007 | last post by:
The Program This assignment will use separate compilation. You MUST use the following guidelines for the design of your program: You are given the header file TelephoneList.h which contain the...
8
by: Rob | last post by:
I have a vector of a class type and when I create an object inside a function and return that and add it to the vector, it doesn't properly keep the data inside it. So I have a few questions: 1....
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.