By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,445 Members | 3,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,445 IT Pros & Developers. It's quick & easy.

std Output: 1 0 Frankly speaking, I was expecting: 1 1 So what happened. Ag

P: n/a
OK, this time the compiler's got me a little bit puzzled, simply because it is
doing something I am not expecting. My understanding, according to the
documentation of std::vector<>::resize(), is that when you specify a
second argument the number of elements specified in the first
argument is each in turn set to the second argument.

void Foo::bar(int x) {
static std::vector<int> foo;
std::cout << "source: " << x << std::endl;
foo.resize(1, x);
std::cout << "foo element zero: " << foo[0] << std::endl;

// ...

}

Output:

1
0

Frankly speaking, I was expecting:

1
1

So what happened. Again, when I run this on a smaller code snipped, I do
indeed get 1 followed by 1. So, what gives? Am I invoking undefined
behavior, or am I simply cursed by my own code???

Again, g++ 3.3.2 on FC1.

Thanks,

Neil
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
OK, this time the compiler's got me a little bit puzzled, simply because it is doing something I am not expecting. My understanding, according to the
documentation of std::vector<>::resize(), is that when you specify a
second argument the number of elements specified in the first
argument is each in turn set to the second argument.

void Foo::bar(int x) {
static std::vector<int> foo;
std::cout << "source: " << x << std::endl;
foo.resize(1, x);
std::cout << "foo element zero: " << foo[0] << std::endl;

// ...

}

Output:

1
0
Really? Where did the words 'source' and 'foo element zero' go?

Frankly speaking, I was expecting:

1
1

So what happened. Again, when I run this on a smaller code snipped, I do
indeed get 1 followed by 1. So, what gives? Am I invoking undefined
behavior, or am I simply cursed by my own code???

Again, g++ 3.3.2 on FC1.


What happened? Faulty compiler, most likely. Or the library
implementation.

----------------------------------- This
#include <iostream>
#include <vector>
using namespace std;

int main()
{
int x = 1;
std::vector<int> foo;
std::cout << "source: " << x << std::endl;
foo.resize(1, x);
std::cout << "foo element zero: " << foo[0] << std::endl;

return 0;
}
----------------------------------- gives
source: 1
foo element zero: 1
------------------------------------------

Test with a different compiler.

Victor
Jul 22 '05 #2

P: n/a
On 2 Apr 2004 20:17:40 -0800, nz******@cs.mun.ca (Neil Zanella) wrote:
OK, this time the compiler's got me a little bit puzzled, simply because it is
doing something I am not expecting. My understanding, according to the
documentation of std::vector<>::resize(), is that when you specify a
second argument the number of elements specified in the first
argument is each in turn set to the second argument.

void Foo::bar(int x) {
static std::vector<int> foo;
std::cout << "source: " << x << std::endl;
foo.resize(1, x);
std::cout << "foo element zero: " << foo[0] << std::endl;

// ...

}

Output:

1
0

Frankly speaking, I was expecting:

1
1

So what happened. Again, when I run this on a smaller code snipped, I do
indeed get 1 followed by 1. So, what gives? Am I invoking undefined
behavior, or am I simply cursed by my own code???

Again, g++ 3.3.2 on FC1.

Thanks,

Neil


I couldn't reproduce this using gcc, nor am I able to imagine how using
resize the way you've illustrated, alone, could have that result (missing
output text notwithstanding...I didn't even pick up on that, Victor)

Are you saying it only occurs within a "large" program, but if you try to
whittle it down to a small snippet, such as the one Victor and I tested
with, it does not happen? If that's the case, I'd suspect memory
corruption from some "unrelated" bug that just happens to manifest itself
in the observed behavior...
-leor
--
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
Jul 22 '05 #3

P: n/a

"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
OK, this time the compiler's got me a little bit puzzled, simply because it is doing something I am not expecting. My understanding, according to the
documentation of std::vector<>::resize(), is that when you specify a
second argument the number of elements specified in the first
argument is each in turn set to the second argument.

Not correct, any elements *added* are set to the second argument. Any
elements already present are left alone.

If you want the former behaviour then the code is

foo.assign(1, x);
void Foo::bar(int x) {
static std::vector<int> foo;
std::cout << "source: " << x << std::endl;
foo.resize(1, x);
std::cout << "foo element zero: " << foo[0] << std::endl;

// ...

}

Output:

1
0

Frankly speaking, I was expecting:

1
1

So would I.
So what happened. Again, when I run this on a smaller code snipped, I do
indeed get 1 followed by 1. So, what gives? Am I invoking undefined
behavior, or am I simply cursed by my own code???


Probably your vector size isn't zero to begin with and you didn't appreciate
the distinction I made above.

john
Jul 22 '05 #4

P: n/a
Victor Bazarov wrote:

"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
OK, this time the compiler's got me a little bit puzzled, simply because

it is
doing something I am not expecting. My understanding, according to the
documentation of std::vector<>::resize(), is that when you specify a
second argument the number of elements specified in the first
argument is each in turn set to the second argument.

void Foo::bar(int x) {
static std::vector<int> foo;
std::cout << "source: " << x << std::endl;
foo.resize(1, x);
std::cout << "foo element zero: " << foo[0] << std::endl;

// ...

}

Output:

1
0


Really? Where did the words 'source' and 'foo element zero' go?

Frankly speaking, I was expecting:

1
1

So what happened. Again, when I run this on a smaller code snipped, I do
indeed get 1 followed by 1. So, what gives? Am I invoking undefined
behavior, or am I simply cursed by my own code???

Again, g++ 3.3.2 on FC1.


What happened? Faulty compiler, most likely. Or the library
implementation.


Since the output has nothing to do with the posted code, it's premature
to blame the implementation.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #5

P: n/a
Victor Bazarov wrote:

"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
OK, this time the compiler's got me a little bit puzzled, simply because

it is
doing something I am not expecting. My understanding, according to the
documentation of std::vector<>::resize(), is that when you specify a
second argument the number of elements specified in the first
argument is each in turn set to the second argument.

void Foo::bar(int x) {
static std::vector<int> foo;
std::cout << "source: " << x << std::endl;
foo.resize(1, x);
std::cout << "foo element zero: " << foo[0] << std::endl;

// ...

}

Output:

1
0


Really? Where did the words 'source' and 'foo element zero' go?

Frankly speaking, I was expecting:

1
1

So what happened. Again, when I run this on a smaller code snipped, I do
indeed get 1 followed by 1. So, what gives? Am I invoking undefined
behavior, or am I simply cursed by my own code???

Again, g++ 3.3.2 on FC1.


What happened? Faulty compiler, most likely. Or the library
implementation.


Since the output has nothing to do with the posted code, it's premature
to blame the implementation.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #6

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
std::vector<>::resize()
Not correct, any elements *added* are set to the second argument. Any
elements already present are left alone.
Ah! _That_'s the key point!
If you want the former behaviour then the code is

foo.assign(1, x);

Probably your vector size isn't zero to begin with and you didn't appreciate
the distinction I made above.


Precisely. The output was not from the first call to Foo::bar(int) and as a
result what you describe was taking place.

Here is a small code snippet just to summarize what we have just discussed...

$ cat foo.cpp
#include <iostream>
#include <vector>

void output(const std::vector<int> &foo) {
for (int i = 0; i < foo.size(); i++)
std::cout << foo[i] << ' ';
std::cout << std::endl;
}

int main() {
std::vector<int> foo(10, -11);
output(foo);
foo.resize(5, -22);
output(foo);
foo.resize(10, -33);
output(foo);
foo.assign(4, -44);
output(foo);
}
$ g++ -o foo foo.cpp; ./foo
-11 -11 -11 -11 -11 -11 -11 -11 -11 -11
-11 -11 -11 -11 -11
-11 -11 -11 -11 -11 -33 -33 -33 -33 -33
-44 -44 -44 -44
$
Jul 22 '05 #7

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
std::vector<>::resize()
Not correct, any elements *added* are set to the second argument. Any
elements already present are left alone.
Ah! _That_'s the key point!
If you want the former behaviour then the code is

foo.assign(1, x);

Probably your vector size isn't zero to begin with and you didn't appreciate
the distinction I made above.


Precisely. The output was not from the first call to Foo::bar(int) and as a
result what you describe was taking place.

Here is a small code snippet just to summarize what we have just discussed...

$ cat foo.cpp
#include <iostream>
#include <vector>

void output(const std::vector<int> &foo) {
for (int i = 0; i < foo.size(); i++)
std::cout << foo[i] << ' ';
std::cout << std::endl;
}

int main() {
std::vector<int> foo(10, -11);
output(foo);
foo.resize(5, -22);
output(foo);
foo.resize(10, -33);
output(foo);
foo.assign(4, -44);
output(foo);
}
$ g++ -o foo foo.cpp; ./foo
-11 -11 -11 -11 -11 -11 -11 -11 -11 -11
-11 -11 -11 -11 -11
-11 -11 -11 -11 -11 -33 -33 -33 -33 -33
-44 -44 -44 -44
$
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.