modifying a vector (STL) in a function



 DEVELOP > c-Plus-Plus > modifying a vector (STL) in a function

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "clinisbut"
Date: 11 Jan 2008 06:10:52 AM
Object: modifying a vector (STL) in a function
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?
.

User: "yaoyi"

Title: Re: modifying a vector (STL) in a function 11 Jan 2008 06:50:36 AM
On Jan 11, 8:10 pm, clinisbut <clinis...@gmail.com> 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!!

}

.

User: ""

Title: Re: modifying a vector (STL) in a function 11 Jan 2008 06:51:01 AM
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
.
User: "clinisbut"

Title: Re: modifying a vector (STL) in a function 11 Jan 2008 07:19:23 AM
Oh... I think i confused reserve() with resize()...
thank you!
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER