473,385 Members | 1,855 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,385 software developers and data experts.

What is wrong with the "for" loop?

Everything in this program produces the correct results except the part
dealing with the RealList. The "for" loop does not output the values that
were input to the ALL[3].RealList. What is the correct syntax to for this?
Thanks

#include <stdio.h>
#include <string>
#include <vector>

union AllType {
int Int;
double Real;
std::string* Text;
std::vector<double>* RealList;
};

void main()
{
std::vector<AllType> ALL;
ALL.reserve(10);
printf("sizeof(ALL)--->%d\n", sizeof(ALL));
printf("sizeof(AllType)--->%d\n", sizeof(AllType));
ALL[0].Int = 31415926;
ALL[1].Real = 3.1415926;
ALL[2].Text = new std::string("Hell Oh Whirled");
ALL[3].RealList = new std::vector<double>;
ALL[3].RealList->reserve(3);
ALL[3].RealList->push_back(123.0);
ALL[3].RealList->push_back(456.0);
ALL[3].RealList->push_back(789.0);
printf("%d\n", ALL[0].Int);
printf("%f\n", ALL[1].Real);
printf("%s\n", ALL[2].Text->c_str());
for (unsigned int N = 0; N < ALL[3].RealList->size(); N++)
printf("%d) %f\n", N, ALL[3].RealList[N]);
}

Jul 19 '05 #1
3 1862

"Peter Olcott" <ol****@worldnet.att.net> wrote in message
news:IR*********************@bgtnsc04-news.ops.worldnet.att.net...
Everything in this program produces the correct results except the part dealing with the RealList. The "for" loop does not output the values that were input to the ALL[3].RealList. What is the correct syntax to for this? Thanks

#include <stdio.h>
#include <string>
#include <vector>

union AllType {
int Int;
double Real;
std::string* Text;
std::vector<double>* RealList;
};

void main()
{
std::vector<AllType> ALL;
ALL.reserve(10);
printf("sizeof(ALL)--->%d\n", sizeof(ALL));
printf("sizeof(AllType)--->%d\n", sizeof(AllType));
ALL[0].Int = 31415926;
ALL[1].Real = 3.1415926;
ALL[2].Text = new std::string("Hell Oh Whirled");
ALL[3].RealList = new std::vector<double>;
ALL[3].RealList->reserve(3);
ALL[3].RealList->push_back(123.0);
ALL[3].RealList->push_back(456.0);
ALL[3].RealList->push_back(789.0);
printf("%d\n", ALL[0].Int);
printf("%f\n", ALL[1].Real);
printf("%s\n", ALL[2].Text->c_str());
for (unsigned int N = 0; N < ALL[3].RealList->size(); N++)
printf("%d) %f\n", N, ALL[3].RealList[N]);
}


The problem is that ALL[3].RealList is a pointer to a vector, rather
than the vector itself. Consequently the [] operator is performed on the
pointer rather than the vector. If you replace the for loop with this it
should work:

const std::vector<double>& v = *(ALL[3].RealList);
for (unsigned int N = 0; N < v.size(); N++)
{
printf("%d) %f\n", N, v[N]);
}

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #2
Peter Olcott <ol****@worldnet.att.net> wrote:
void main()
int main()
{
std::vector<AllType> ALL;
ALL.reserve(10);
You want 'resize' here, not 'reserve' if you want room for 10 items
printf("sizeof(ALL)--->%d\n", sizeof(ALL));
[This is the size of the vector's private data, completely unrelated to
the number of items stored there]
ALL[1].Real = 3.1415926;
ALL[2].Text = new std::string("Hell Oh Whirled");
ALL[3].RealList = new std::vector<double>;
ALL[3].RealList->reserve(3);


Same here.

Andre'

--
Those who desire to give up Freedom in order to gain Security, will not have,
nor do they deserve, either one. (T. Jefferson or B. Franklin or both...)
Jul 19 '05 #3
Peter> void main()
Peter> {
Peter> std::vector<AllType> ALL;
Peter> ALL.reserve(10);
Peter> printf("sizeof(ALL)--->%d\n", sizeof(ALL));
Peter> printf("sizeof(AllType)--->%d\n", sizeof(AllType));
Peter> ALL[0].Int = 31415926;
Peter> ALL[1].Real = 3.1415926;
Peter> ALL[2].Text = new std::string("Hell Oh Whirled");
Peter> ALL[3].RealList = new std::vector<double>;

These last four statements yield undefined behavior because ALL does
not have any elements, as you would see if you had executed

printf("ALL.size()--->%d\n", ALL.size()");

If you want to cause ALL to have 10 elements, you should execute

ALL.resize(10);

instead of calling reserve.
--
Andrew Koenig, ar*@acm.org
Jul 19 '05 #4

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

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
8
by: lancevictor | last post by:
I'm learning javascript, and toward that end, I am recreating a page "my way". The original page: http://stenbergcollege.com and the one that I'm creating:...
9
by: adam | last post by:
hello Does exist in SQL language "for" loop ? If yes, what syntax does it has ? best wishes Adam
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
12
by: Robbie Hatley | last post by:
I'm getting a bizarre error with this code: ... case WM_COMMAND: { switch (LOWORD(wParam)) // switch (control ID) { ... case IDC_RAIN_ENABLE_SIMPLE: {
5
by: Fabian Vilers | last post by:
Hi again... I'm wondering what could be better in terms of performance between: var my_array = new Array(); // populate array for (index in my_array) { // do something with my_array
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
5
by: anshuman | last post by:
Hi, I am trying to skip a record if some conditions match in a for loop, what is the exact keyword to use that in PL/SQL. e.g, DECLARE .. i NUMBER; BEGIN
9
by: Alexnb | last post by:
Okay, so lets say you have a list: funList = and you do: for x in funList: print x this will print 1-5
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.