473,228 Members | 1,771 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,228 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 1701
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.