What is wrong with the "for" loop?

Discussion in 'C++' started by Peter Olcott, Jul 16, 2003.

  1. Peter Olcott

    Peter Olcott Guest

    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]);
    }
    Peter Olcott, Jul 16, 2003
    #1
    1. Advertising

  2. "Peter Olcott" <> wrote in message
    news:IRaRa.57177$...
    > 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
    Peter van Merkerk, Jul 16, 2003
    #2
    1. Advertising

  3. Peter Olcott <> 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...)
    =?iso-8859-1?Q?Andr=E9_P=F6nitz?=, Jul 16, 2003
    #3
  4. 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,
    Andrew Koenig, Jul 16, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew
    Replies:
    7
    Views:
    667
    Priscilla Walmsley
    Jan 7, 2005
  2. Jeff
    Replies:
    3
    Views:
    564
    Karsten Baumgarten
    Feb 5, 2005
  3. Josselin

    what's wrong in my loop ?

    Josselin, Oct 2, 2006, in forum: Ruby
    Replies:
    4
    Views:
    112
    Rick DeNatale
    Oct 2, 2006
  4. Replies:
    7
    Views:
    381
    涵曦
    Oct 18, 2012
  5. Isaac Won
    Replies:
    9
    Views:
    371
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page