Bonjour tous ,
je désire savoir combien d'octets restent libres dans le tas pendant l'execution
d'un programme . Voici le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
#include <iostream>
#include <cmath>
 
using namespace std;
 
int Tas_restant(void);
 
int main()
{
    long int *i1;
 
    cout << Tas_restant() << "  octets" << endl;
    while(1)
    {
        i1 = new (nothrow) long int[1234];
        if(!i1) break;
    }
    cout << Tas_restant() << "  octets" << endl;
 
    return 0;
}
int Tas_restant(void)
{
    double  puissance = 32.0;
    unsigned int heap_allocation_size = pow(2.0,puissance)-1;// 2 puissance 32
    unsigned long    heap_maxi = 0;
    char    *pChar;
 
    while(puissance>=0)
    {
        pChar = new (nothrow) char[heap_allocation_size];
        if(pChar)
        {
            heap_maxi += heap_allocation_size;
        }
        else
        {
            heap_allocation_size = pow(2.0,--puissance)-1;
        }
    }
    return heap_maxi;
}
Le premier appel à "Tas_restant()" semble cohérent ; par contre ,le second
renvoit invariablement 0 alors qu'il semble évident qu'il doit bien rester quelques petits octets de libre , non !