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. On 4/28/2014 7:09 AM, Milica Milojevic wrote:
    > 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 list I 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 measuresQueue with the: cout << "measuresQueue. size: " << measuresQueue.size() << '\n';
    >
    > I always get the number 4 instead of ten. I don't know what is the reason?


    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
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 28, 2014
    #2
    1. Advertisements

  3. On Mon, 28 Apr 2014 04:09:11 -0700 (PDT), Milica Milojevic
    <> wrote:

    >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 list I 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]);


    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.

    >Everything was fine until I wanted to see the size of my list measuresQueue with 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.
    >
    >


    --
    Remove del for email
     
    Barry Schwarz, Apr 28, 2014
    #3
    1. Advertisements

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. ^CrazyCoder^
    Replies:
    3
    Views:
    7,789
    Terrence Benade
    Sep 15, 2003
  2. David
    Replies:
    1
    Views:
    672
    David
    Jul 19, 2004
  3. Sam
    Replies:
    0
    Views:
    588
  4. Replies:
    1
    Views:
    2,032
    Ian Collins
    Jun 20, 2007
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    13,613
    Michael Jung
    May 25, 2008
  6. Nagu
    Replies:
    1
    Views:
    392
    Martin v. Löwis
    Jul 7, 2008
  7. Nagu
    Replies:
    1
    Views:
    411
    Martin v. Löwis
    Jul 7, 2008
  8. Richard A. DeVenezia
    Replies:
    4
    Views:
    408
    Lasse Reichstein Nielsen
    Sep 5, 2003
Loading...