std::bad_alloc

Hi there,

I'm getting a strange error with the program I'm writing:

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

My program is quite long, so I'll try to present the crashing part only.
I have two files: the main file and a function file. The main defines a structure arcs_i and creates a vector of arcs_i. The function adds to the vector the different elemtents I want it to contain.

main.cpp:

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
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

struct arc_i
    {
        int arcName;
        int arcTailType;
        int arcTailName;
        int arcHeadName;
        int arcRule;
        int arcValue;
        vector<int> arcDependency;
        int arcDelay;
    };

int main()
{
    int numberBlocks=4;
    int lineStructure[4][5]={1,1,1,1;0,0,3,0;0,0,0,0}; 
    int numbreAssets1=4;
    int numbreAssets2=1;
    int numbreAssets3=0;

    vector<arc_i> arcs; //creation of the arcs vector

    void arcscreator(std::vector<arc_i>& arcs, int numberBlocks, int numberAssets1, int numberAssets2, int numberAssets3, int arraylinestruct[][5]);

    arcscreator(arcs, numberBlocks, numberAssets1, numberAssets2, numberAssets3, lineStructure);


arcscreator.cpp:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <vector>
using namespace std;

struct arc_i
{
    int arcName;
    int arcTailType;
    int arcTailName;
    int arcHeadName;
    int arcRule;
    int arcValue;
    vector<int> arcDependency;
    int arcDelay;
};

void arcscreator(std::vector<arc_i>& arcs, int numberBlocks, int numberAssets1, int numberAssets2, int numberAssets3, int arraylinestruct[][5])
{
    //creation of the first element of the vector
    arcs.push_back(arc_i());                    
    arcs[0].arcName=0;
    arcs[0].arcTailType=2;                      
    arcs[0].arcTailName=0;
    arcs[0].arcHeadName=0;
    arcs[0].arcRule=3;                       
    arcs[0].arcDependency.push_back(6*numberBlocks+1+20*numberAssets2+ 8*numberAssets3+8);
    arcs[0].arcDelay=arraylinestruct[0][3]+arraylinestruct[0][4]*60;

    //then follows a series of for-loops to create the following elements of the vector
    for (int i=1; i<numberBlocks+1; i++)            
    {                                               
        arcs.push_back(arc_i());
        arcs[i].arcName=i;
        arcs[i].arcTailType=2;                      
        arcs[i].arcTailName=i;
        arcs[i].arcHeadName=i;
        arcs[i].arcRule=3;                         
        arcs[i].arcDependency.push_back(i+numberBlocks);
        arcs[i].arcDelay=arraylinestruct[i][3]+arraylinestruct[i][4]*60;
    }

    for (int i=numberBlocks+1; i<2*numberBlocks+1; i++) 
    {                                               
        arcs.push_back(arc_i());
        arcs[i].arcName=i;
        arcs[i].arcTailType=1;                      
        arcs[i].arcTailName=i-(numberBlocks3+1);
        arcs[i].arcHeadName=i-(numberBlocks3+1)+1;
        arcs[i].arcRule=1;
        arcs[i].arcDelay=0;
    }

    //more for-loops that I'm not putting here as they are similar and working fine.

    int sigBlockSize=0;
    int follSigBlockSize=0;
    int counter=1;

    for (int i=0; i<numberAssets2; i++)    
    {
        if (i<numberAssets2-1)         
            sigBlockSize=sigPosition[i+1]-sigPosition[i];
        else
            sigBlockSize=numberBlocks-sigPosition[i];

        for (int j=0; j<sigBlockSize; j++)    //this loop works fine
        {
            arcs.push_back(arc_i());                   
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcName = 6*numberBlocks+1+26*numberAssets2+10+counter;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcTailType = 1;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcTailName = numberBlocks+1+sigPosition[i]+j;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcHeadName = numberBlocks+1+2*numberAssets2+i;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcValue = 0;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcRule = 2;   
            counter=counter+1;
        }

        for (int j=0; j<sigBlockSize; j++)    //loop during which the error occurs
        {
            arcs.push_back(arc_i());   
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcName = 6*numberBlocks+1+26*numberAssets2+10+counter;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcTailType = 2;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcTailName = numberBlocks+1+2*numberAssets2+i;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcHeadName = numberBlocks+1+sigPosition[i]+j;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcValue = 0;
            arcs[6*numberBlocks+1+26*numberAssets2+10+counter].arcRule = 2;
            counter=counter+1;
        }

    }


The error occurs in this last loop (last presented, but I have more loops after that one in my program). The first iteration (j=0) is executed correctly. The error appears when the command "arcs.push_back(arc_i());" is executed for j=1. This element should be the 65th element added to the vector.

Any idea what could cause this error? I'm sort of at a loss because the point of the program where it crashes usually works correctly... From other topics I have read, I guess it is a memory issue. But when I checked the size of the vector with .size() it gives me 64 which is correct, and with .maxsize() it gives me 107374182...
I would need to be able to add much more elements to the vector. Any idea how to solve that issue?

Thanks for your help!!
Well, std::bad_alloc is saying you've not got enough memory to provde the reuested memory.

You haven't mentioned which IDE/debugger you have to hand, but with Visual Studio this is the kind of case where I use the Debug / Exceptions and check the Thown box for std::exception. Then the debugger will trap when the exception is raised and I can see what's going on (here, how much memory is being asked for.)

(I assume GDB has the same capability, but don't know what's involved.)

You might be getting a calculation wrong, or there might be an unitinitialized variable, or it could even be memory corruption.

But I've not tried to read though your code...

Andy

PS Out of curiousity, why don't you use a temp var to cut down on code, e.g.

1
2
3
4
5
6
7
8
9
10
11
12
        for (int j=0; j<sigBlockSize; j++)    //loop during which the error occurs
        {
            int index=6*numberBlocks+1+26*numberAssets2+10+counter;
            arcs.push_back(arc_i());   
            arcs[index].arcName = index;
            arcs[index].arcTailType = 2;
            arcs[index].arcTailName = numberBlocks+1+2*numberAssets2+i;
            arcs[index].arcHeadName = numberBlocks+1+sigPosition[i]+j;
            arcs[index].arcValue = 0;
            arcs[index].arcRule = 2;
            counter=counter+1;
        }

Last edited on
Topic archived. No new replies allowed.