error while using the function size ()

Discussion in 'C++' started by Milica Milojevic, Apr 28, 2014.

  1. Hi,

    I have created a list called measuresQueue (list<pssMeasure *> measuresQueue; ) which is consisted of ten elements. Then I used measuresQueue.size() function. When I did the printf of the previous function it prints me the number four not ten. Can someone help me with this problem.

    Here is my code:

    In for loop I have entered my elements of the list:

    for (filtertime_t=0; filtertime_t<=1.8; filtertime_t=filtertime_t+0.2) {

    pssMeasure *mes = measureReceived(filtertime_t,y,mes_vect,mes_cov);
    y++;

    process_th->critical_section->writeMeasures(mes);

    }
    Here it is the measureReceived function description:
    pssMeasure * measureReceived(double timeStamp,int j,double array[3][10], double array_cov[3]) {


    pssMeasure *mes = new pssMeasure();


    mes->track_id =7;
    mes->timeStamp =timeStamp;

    for (int i=0; i<3; i++)
    {
    mes->z_t=array[j];

    }


    for(int i = 0; i < 3; i++) {

    for(int k = 0; k < 3; k++)
    mes->cov_z[k] = 0.0;

    mes->cov_z = array_cov;


    }
    return mes;
    }
    After creating mes I store them using the function writeMeasures:

    void CPSSCriticalSection::writeMeasures(pssMeasure* pss_mes) {

    measuresQueue.push_back(pss_mes);}

    In order to check that my all elements are stored in the measuresQueue listI have used this after the measuresQueue.push_back(pss_mes);

    cout << "hello1" << endl
    printf("%f %f %f %f \n", pss_mes->timeStamp, pss_mes->z_t[0], pss_mes->z_t[1], pss_mes->z_t[2]);

    Everything was fine until I wanted to see the size of my list measuresQueuewith the: cout << "measuresQueue. size: " << measuresQueue.size() << '\n';

    I always get the number 4 instead of ten. I don't know what is the reason?





    Thank you,
    M.
     
    Milica Milojevic, Apr 28, 2014
    #1
    1. Advertisements



  2. The reason is likely some kind of logic mistake in some other part of
    your program.

    Can you simplify your program and exclude all parts that you don't show
    here and try a very simple test:

    #include <list>
    #include <iostream>
    struct pssMeasure {}; // yes, empty
    struct CPS {
    std::list<pssMeasure*> measuresQueue; // yes, only this
    void writeMeasures(pssMeasure* m) {
    measuresQueue.push_back(m);
    }

    void dump(std::eek:stream& os) {
    os << "measuresQueue.size : " << measuresQueue.size();
    }
    };

    int main() {
    CPS cps;
    for (double t = 0; t <= 1.8 ; t+=0.2)
    {
    pssMeasure *pm = new pssMeasure;
    cps.writeMeasures(pm);
    }
    cps.dump(std::cout);
    }

    ? What output do you get? I get '10'.

    V
     
    Victor Bazarov, Apr 28, 2014
    #2
    1. Advertisements



  3. This code tells you absolutely nothing about the success of the
    push_back operation. It does tell you what data you intended to store
    in the list but not whether it actually was.

    You could add a debugging cut before and after the push_back to print
    out the size to verify the list actually grows to ten.
     
    Barry Schwarz, Apr 28, 2014
    #3
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.