473,513 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 charchar_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?
Jan 11 '08 #1
3 1721
On Jan 11, 8:10 pm, clinisbut <clinis...@gmail.comwrote:
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!!

}
Jan 11 '08 #2
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 charchar_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
Jan 11 '08 #3
Oh... I think i confused reserve() with resize()...
thank you!
Jan 11 '08 #4

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

Similar topics

9
2956
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
6
2818
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
3
4754
by: Duncan | last post by:
I need to populate a vector with the following struct details:\ struct group { string groupname; int gid; list<string> members; }; the text file which this is to read from is in the...
18
1999
by: John Black | last post by:
Hi, I am not familiar with for_each very well, suppoase I have a vector<pair<unsigned int, unsigned int> > vec1 and the contents are {<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>} what...
9
1672
by: Timothee Groleau | last post by:
Hi all, My name is Tim, I'm just getting started with C++ and this is my first post to the group. Is there a standard recommended approach to use a vector of pointers along with <algorithm>?...
23
6508
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
24
2917
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
6
1812
by: alan.patterson5 | last post by:
I have a vector of N values and a vector of N functors. I want to apply each functor to its corresponding value. What would be the 'correct' STL way to do this. for_each takes on functor and...
1
4139
by: themadme | last post by:
im running code thats using threads, everytime i get this debug assertion failed box poping up. It involves with my stl vector. its saying that vector iterators incompatible. the vector...
0
7161
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
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.