franziss

I encountered the error message below:

First-chance exception at 0x7c812a5b in mda2.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f764..
Unhandled exception at 0x7c812a5b in mda2.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f764..

Any possible answers Am I out of memory It occurred at this line

m1pos [ i ] = new int [m1[0]+1];//allocate new array

where m1pos is an array of pointers.

When my input file is small, there is no error. When my input file is large, I will have this error.



Re: Visual C++ General Problems in debugging

Burhan Ateeq MSFT

Hi,

Can you, please, include a larger code snipplet I might be able to help you with this if I look at the context in which you are using this.

Generally, when you allocate a new array, don't you have to use a constant instead of an expression to specify the size.

thanks,

Burhan





Re: Visual C++ General Problems in debugging

Simple Samples

If you were out of memory then you should get a null for return value. Right

When debugging, can you determine what it is that is at memory location 0x0012f764 It must either be m1[ 0 ] or m1pos[ i ], right Are you sure that "i" has a valid index/subscript/iterator






Re: Visual C++ General Problems in debugging

franziss

thanks! when i allocate a new array, i use an "MARGIN-RIGHT: 0px">

int i, j, k, *m1, *m2, *tid, *temp, *p, *c1, *c2, step, base;

int a1[90], pos, num, *chain1, *chain2, *chain, **m1pos, **m2pos, index;

int *motif1, *motif2, motif1_size, motif2_size;

mqbStructure t;

m1 = final_mqb.motif1[final_mqb.num];

m2 = final_mqb.motif2[final_mqb.num];

p = final_mqb.protein[final_mqb.num];

c1 = final_mqb.m1_chain[final_mqb.num];

c2 = final_mqb.m2_chain[final_mqb.num];

num = final_mqb.cross_num[final_mqb.num];

final_mqb.m1pos[final_mqb.num].pos = new int*[num];

final_mqb.m2pos[final_mqb.num].pos = new int*[num];

m1pos = final_mqb.m1pos[final_mqb.num].pos;

m2pos = final_mqb.m2pos[final_mqb.num].pos;

//got to do these coz even is using chain 1 and chain 2 of mqb_result

//odd is using chain 2 and chain 1 of mqb_result

for (i=0; i<rtidset_size; i++) {//naive implementation! for each tid of mqb_result

if (rtidsetIdea%2==0) {//even number

index = rtidsetIdea/2;//chain 1 of cross mqb is different f

motif1 = mqb_result[index].side1;

motif2 = mqb_result[index].side2;

motif1_size = mqb_result[index].side1_num;

motif2_size = mqb_result[index].side2_num;

}

else {//odd number

index = (rtidsetIdea-1)/2;

motif1 = mqb_result[index].side2;

motif2 = mqb_result[index].side1;

motif1_size = mqb_result[index].side2_num;

motif2_size = mqb_result[index].side1_num;

}

t = mqb_result[index];//t is the closedpattern in the chain

chain = Motif.protein[pIdea].chain[c1Idea];

m1posIdea = new int [m1[0]+1];//allocate new array





Re: Visual C++ General Problems in debugging

franziss

when the error occurs, m1[0] is 3 and m1[pos] is an null address, something like dxdxdxd, since it is pointing to nothing.

my debugger breaks in the new function, where it is trying to allocate the memory





Re: Visual C++ General Problems in debugging

Simple Samples

Notice that the left side is m1pos[ i ], not m1[ pos ], right

So assuming that m1pos has a valid address for m1pos, then does "i" have a valid index/subscript/iterator






Re: Visual C++ General Problems in debugging

franziss

yes i is 256. the frustrating part is when i input a small dataset, the code runs smoothly and i use

_CrtDumpMemoryLeaks();

to detect memory leaks, which indicate there is not memory leak. But when i use a large dataset, which takes days to run, i will encountered this memory error.





Re: Visual C++ General Problems in debugging

Simple Samples

Is m1pos "an array of pointers" or a pointer to an array of ints This is the kind of stuff that still spins my head around, but I would sure check the documentation or a tutorial or something like that.




Re: Visual C++ General Problems in debugging

franziss

m1pos is an array of pointers, where each pointer points to an array of ints



Re: Visual C++ General Problems in debugging

Simple Samples

Perhaps you need to add some bounds checking. It is unfortunate that that won't diagnose the problem until the program has run a long time, but since the program does a lot of array processing, the problem is likely an out-of-bounds problem.




Re: Visual C++ General Problems in debugging

franziss

may i know what is bounds checking is there any website that teaches this thanks!



Re: Visual C++ General Problems in debugging

Simple Samples

I am using the term bounds checking in a general manner. It is the type of stuff that makes sense; the type of stuff that programmers should do but we often avoid doing. It just means that we check indexes and subscripts and such to ensure that they have valid values. So if an array has 10 items and if the subscript is 10 or above then the program should not use that subscript. The program could instead issue an error message and quit, or do something like that. Then you would know immediately what the problem is; at least, you probably would know well enough that you can diagnose further with a much more limited scope.






Re: Visual C++ General Problems in debugging

jdr5

Check that the arrays are being allocated, so that you have a non-zero length:

assert( num > 0 );

final_mqb.m1pos[final_mqb.num].pos = new int*[num];

...

m1pos = final_mqb.m1pos[final_mqb.num].pos;


If you get a value of zero for num, the m1pos would point to a zero length array. Then just assert everything else you can think of, e.g.:

assert( m1[0] >= 0 );
assert( (0 <= i) && (i < num) );
m1posIdea = new int [m1[0]+1];//allocate new array




Re: Visual C++ General Problems in debugging

Holger Grund

Burhan Ateeq MSFT wrote:

Generally, when you allocate a new array, don't you have to use a constant instead of an



Re: Visual C++ General Problems in debugging

Holger Grund

Just use your debugger. When an unhandled exception occurs, you still have a callstack. Just move up the callstack to the operator new. What's the size argument I'd bet it's just a bogus large value.

Oc, your code does not look very clean and actually it screams for that type of hard-to-track-down intermittent failures. It probably helps to use wrappers for memory management and at least be aware of the loop invariants.

-hg