simple std::queue question

Hello all,
In the following code I have tested the amount of ram the program consumes, and I realized that when the second loop was executed, the memory is not being freed until the program ends.

What am I doing wrong here?
cheers
santiagorf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <queue>
using namespace std;


int main(){
    int in;

    queue<long double*> q;

    for(int i=0;i<9190000;i++){
        long double *a=new long double;
        *a=i;
        q.push(a);
    }
    for(int i=0;i<9190000;i++){
        long double *a=q.front();
        delete a;
        q.pop();
    }
/// the memory hasn't been freed

    return 0;
}

Well, you're definitely deleting the pointers, so that's not the problem.
Unless you tell it otherwise, std::queue will use std::deque internally. std::deque is similar in some aspects to a vector. For example, it doesn't shrink back as you remove elements from it.
You can try this:
1
2
3
4
5
6
7
8
int main(){
    {
        queue<long double*> q;
        //Your loops here.
    }
    //Check memory usage here.
}
Thanks. I check your solution to no avail.
I also tried this, same thing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(){
    int in;
    queue<long double*> * pq=new queue<long double*>;

    for(int i=0;i<9190000;i++){
        long double *a=new long double;
        *a=i;
        pq->push(a);
    }
    for(int i=0;i<9190000;i++){
        long double *a=pq->front();
        delete a;
        pq->pop();

    }
    delete pq;


}
Beyond a buggy implementation, I don't see anything that could be causing a leak.
Topic archived. No new replies allowed.