Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > modifying a vector (STL) in a function

Reply
Thread Tools

modifying a vector (STL) in a function

 
 
clinisbut
Guest
Posts: n/a
 
      01-11-2008
I'm trying to understand what's wrong with this:

void getAllData( std::vector<unsigned char> &char_array )
{
char_array.clear();
char_array.reserve( 11 );

char_array[0] = 'A'; //<----No error compilation, but no
effect!!
}

and then I call this function:

std::vector<unsigned char> char_array;
getAllData( char_array );

But if I do this:
void getAllData( std::vector<unsigned char> &char_array )
{
char_array.clear();
char_array.reserve( 11 );

char_array.push_back('A'); //Works
}

It works good, but when I try to read some element with [] operator
doesn't works...
What's the correct way to deal with a vector inside a function???

PD: What's the diference between using & and * in function header's
parameter list?
 
Reply With Quote
 
 
 
 
yaoyi
Guest
Posts: n/a
 
      01-11-2008
On Jan 11, 8:10 pm, clinisbut <(E-Mail Removed)> wrote:
> I'm trying to understand what's wrong with this:
>
> void getAllData( std::vector<unsigned char> &char_array )
> {
> char_array.clear();
> char_array.reserve( 11 );

the char_array is empty!
you can use char_array.resize(11); instead
>
> char_array[0] = 'A'; //<----No error compilation, but no
> effect!!
>
> }

 
Reply With Quote
 
 
 
 
jkherciueh@gmx.net
Guest
Posts: n/a
 
      01-11-2008
clinisbut wrote:

> I'm trying to understand what's wrong with this:
>
> void getAllData( std::vector<unsigned char> &char_array )
> {
> char_array.clear();
> char_array.reserve( 11 );
>
> char_array[0] = 'A'; //<----No error compilation, but no effect!!


Actually, you have an out-of-bounds error and the code has undefined
behavior.

> }
>
> and then I call this function:
>
> std::vector<unsigned char> char_array;
> getAllData( char_array );
>
> But if I do this:
> void getAllData( std::vector<unsigned char> &char_array )
> {
> char_array.clear();
> char_array.reserve( 11 );
>
> char_array.push_back('A'); //Works
> }


This is a correct way to fill a vector.


> It works good, but when I try to read some element with [] operator
> doesn't works...


operator[] and the at() function only allow you to access vector elements
that are already stored in the container; calling them will not magically
enlarge the vector (as opposed to std::map, where this kind of magic
happens). Thus, you have to populate the vector _before_ you can access its
elements. The push_back() function is the way to go.


> What's the correct way to deal with a vector inside a function???


That all of this happens inside a function is immaterial.


> PD: What's the diference between using & and * in function header's
> parameter list?


& denotes a reference parameter and * denotes a pointer.


Best

Kai-Uwe Bux
 
Reply With Quote
 
clinisbut
Guest
Posts: n/a
 
      01-11-2008
Oh... I think i confused reserve() with resize()...
thank you!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Modifying the value of each element stored in a std::vector. Jason Heyes C++ 11 02-09-2005 08:23 PM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments