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 7 1805
"Neil Zanella" <nz******@cs.mu n.ca> wrote in message
news:b6******** *************** ***@posting.goo gle.com... 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
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 documentatio n 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
"Neil Zanella" <nz******@cs.mu n.ca> wrote in message
news:b6******** *************** ***@posting.goo gle.com... 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
Victor Bazarov wrote: "Neil Zanella" <nz******@cs.mu n.ca> wrote in message news:b6******** *************** ***@posting.goo gle.com... 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)
Victor Bazarov wrote: "Neil Zanella" <nz******@cs.mu n.ca> wrote in message news:b6******** *************** ***@posting.goo gle.com... 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)
"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
$
"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
$ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 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...
|
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 single-pane form GUI.
I've got some stdout print statements in the code ... but I cannot find
where in the world the output text is appearing. For...
|
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 under the project directory.
When I go to the project's properties and try to change the path to the
output folder back to where it always was, I get...
|
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 end and gets
caught in an endless loop. I am checking for the end of the output
like this:
while ((hj =...
|
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?
| |
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 one error
message appears.
How to have output window back on VS main window?
|
by: rajpal_jatin |
last post by:
int main()
{
int k;
union jatin{
int i :5;
char j :2;
};
union jatin rajpal;
k= sizeof(rajpal);
|
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 show the number:
|
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 difference between two
source documents and output node-sets which are "different" (changed
or new) to new XML files using xsl:result-document
To...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| | |