473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
7 1787
"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
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

"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
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Neil Zanella | last post by:
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...
1
by: noleander | last post by:
Hi. I've got a C++ program written in Visual C++ 2003. The program is trivial, created with the Program-creation wizard: used the .NET "Form" template. The program has a trivial...
6
by: Peter Afonin | last post by:
Hello, When I build an ASP.NET application, for some weird reason Visual Studio.Net started to put my DLLs into the VSWebCache folder under Documents and Settings instead of usual bin folder...
16
by: Zenon | last post by:
I have an application where I create a Plink process to communicate with an HP Unix box. The problem I am having is that while reading the redirected output, it seems to try to continue past the...
11
by: Vaibhav87 | last post by:
i write void main() { int j; j - = 0; printf("%d",y); } & i got the answer as 842. i tried it on various computers but the answer is same. pls help me how is this answer is?
1
by: =?Utf-8?B?U2luY2xhaXI=?= | last post by:
Hi everyone, it happened to my VS2002 that I can't see Output window. I selected view-other windows-output, but nothing happened. It is really nightmare to find errors after build process. No...
18
by: rajpal_jatin | last post by:
int main() { int k; union jatin{ int i :5; char j :2; }; union jatin rajpal; k= sizeof(rajpal);
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can...
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.