Connecting Tech Pros Worldwide Help | Site Map

returned vector of function is zeroized

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 09:00 AM
Huibuh
Guest
 
Posts: n/a
Default 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!



  #2  
Old July 22nd, 2005, 09:00 AM
Pete
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized

Huibuh wrote:[color=blue]
> 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![/color]

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


  #3  
Old July 22nd, 2005, 09:00 AM
Pete
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized

Huibuh wrote:[color=blue]
> 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![/color]

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


  #4  
Old July 22nd, 2005, 09:00 AM
Leor Zolman
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized

On Mon, 5 Apr 2004 23:07:44 +0200, "Huibuh" <no.spam@t-online.de> wrote:
[color=blue]
>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???[/color]

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;
}
[color=blue]
>
>I have tried it before without using a pointer - same result![/color]

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
[color=blue]
>[/color]

--
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
  #5  
Old July 22nd, 2005, 09:00 AM
Leor Zolman
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized

On Mon, 5 Apr 2004 23:07:44 +0200, "Huibuh" <no.spam@t-online.de> wrote:
[color=blue]
>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???[/color]

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;
}
[color=blue]
>
>I have tried it before without using a pointer - same result![/color]

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
[color=blue]
>[/color]

--
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
  #6  
Old July 22nd, 2005, 09:00 AM
Bill Seurer
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized

Huibuh wrote:
[color=blue]
> vector<int> ve;
> vec=&ve;[/color]

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.
[color=blue]
> return vec;
> ve.clear();[/color]

Do you ever expect that clear function to be called? If so, how?
  #7  
Old July 22nd, 2005, 09:00 AM
Bill Seurer
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized

Huibuh wrote:
[color=blue]
> vector<int> ve;
> vec=&ve;[/color]

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.
[color=blue]
> return vec;
> ve.clear();[/color]

Do you ever expect that clear function to be called? If so, how?
  #8  
Old July 22nd, 2005, 09:01 AM
John Harrison
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized


"Huibuh" <no.spam@t-online.de> wrote in message
news:c4shr0$1mb$06$1@news.t-online.com...[color=blue]
> 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[/color]
(all[color=blue]
> 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!
>[/color]

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


  #9  
Old July 22nd, 2005, 09:01 AM
John Harrison
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized


"Huibuh" <no.spam@t-online.de> wrote in message
news:c4shr0$1mb$06$1@news.t-online.com...[color=blue]
> 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[/color]
(all[color=blue]
> 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!
>[/color]

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


  #10  
Old July 22nd, 2005, 09:10 AM
Huibuh
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized

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



  #11  
Old July 22nd, 2005, 09:10 AM
Huibuh
Guest
 
Posts: n/a
Default Re: returned vector of function is zeroized

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



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.