[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

something weird in vector

look at this code:
---------------------
string s = "abc";
vector<string> v;
v.push_back(s);

v.clear();

cout<<v[0];
---------------------
I'm creating a vector and assigning to its first element the string s.
then I use clear() to empty the vector.
I expected v[0] to be empty but when i print it, it prints "abc", why is that? shouldn't it makes the program crash or something?
0
tamirvs
Asked:
tamirvs
  • 5
  • 4
  • 2
1 Solution
 
Jaime OlivaresCommented:
This is an unpredictable behaviour, you have to avoid to go out of vector bounds by yourself.
0
 
allmerCommented:
You are absolutely right that this should crash.
If you empty the vector and clear() does that
then accessing the content is of course illegal.
The above doesn't work in
MS Visual Studio .net Debug or Release compilation.
Not all compilers strictly abide the rules. I am not
saying that VS does (MS usually doesn't, does it?)
But in this case it does and it seems to be
a compiler issue to me.
0
 
Jaime OlivaresCommented:
BTW. Typically, collection implementations (specially arrays or 'vectors'), doesn't deallocate its memory inmediatly when you erase an element, for performance purposes.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tamirvsAuthor Commented:
I know, i'm just curious why it happens.
0
 
tamirvsAuthor Commented:
my previous reply was addressed to jaime, only now i see the other 2 replies =/

>>BTW. Typically, collection implementations (specially arrays or 'vectors'), doesn't deallocate its memory inmediatly when you erase an element, for performance purposes.

maybe this is the cause?
0
 
Jaime OlivaresCommented:
I think this is the most possible cause. Try with a more simple vector, let's say, vector<int>
0
 
tamirvsAuthor Commented:
I'm getting the same problem with int, char*, everything...
0
 
Jaime OlivaresCommented:
well, then it is the cause, but as I have said, this is an unpredictable result, you can get a different behaviour with another compiler, and finally you are responsible for not accessing vector out of bounds.
0
 
allmerCommented:
try

vector<string> v;
cout << v[0];

to find out if you will ever get an error message.
0
 
tamirvsAuthor Commented:
this one makes my program crash...
0
 
Jaime OlivaresCommented:
that's because tipically (but not mandatory) when a vector is new, it doesn't allocate space until it receive first element.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now