-1

Every 1 second, function works. my system the linux. Runs suddenly dies.

-----global-------

static int arrayNum[33000];

-------------------

 function(){
        unsigned short int**  US_INT;
try{
        US_INT= new unsigned short int*[255];
                for(int i = 0; i < 255; i++)
                {
                    US_INT[i] = new unsigned short int[128];
                    memset(US_INT[i], 0, sizeof(unsigned short int) * 128);
                }
        double x;
        double y;
        int cnt= 0;  
                int nArrayCount=0;
              for(int i = 0; i < 255; i++)
                {

                    for(int j=0;j<128;j++){

                        x=j;
        y=cnt
                        arrayNum[nArrayCount]=US_INT[i][j];

                        nArrayCount++;

                    }
                    cnt=cnt+(256/255); 

                }


         for(int i = 0; i < 255; i++)
                {
                    delete[] US_INT[i];
                }

                delete[] US_INT;
    }
catch (const std::bad_alloc&) {
            qDebug() << "alloc error";
            for(int i = 0; i < 255; i++)
                {
                    delete[] US_INT[i];
                }

                delete[] US_INT;
            }
}

When memory allocation fails, the exception handling. However, the program will die. I made a mistake? output:

Error in `./TEST': double free or corruption (out): 0x34b00418

OMG_Soruce
  • 96
  • 1
  • 2
  • 9
  • Possible duplicate of [How to track down a "double free or corruption" error](https://stackoverflow.com/questions/2902064/how-to-track-down-a-double-free-or-corruption-error) – Raedwald Dec 06 '18 at 13:25

3 Answers3

3

Preface. It is strongly recommended to avoid raw pointers, like

unsigned short int** US_INT

Either use vector of vectors, or single-dimension vector and access elements like vec.at(X + Y*sizeX);

Why program dies?

Exception probably thrown in a middle of allocation of US_INT elements, so, part of array is valid, and part of array is uninitialized garbage. And in CATCH you are deleting pointers in whole array. Since deleting uninitialized pointer is undefined behavior - this is one of possible outcomes of UB here.

Starl1ght
  • 4,422
  • 1
  • 21
  • 49
  • thank you. my testing...Memory allocation on the part of the flag. int count =0 ; for(int i = 0; i < 255; i++) { US_INT[i] = new unsigned short int[128]; memset(US_INT[i], 0, sizeof(unsigned short int) * 128); }
    catch(){ for(int i=0;i
    – OMG_Soruce Jan 28 '16 at 09:00
0

Your catch clause is broken. You start with delete[] US_INT[i] which isn't valid if the first new caused the exception. Fix: use std::vector<>. There's no reasonable way to figure out which of the 257 allocations threw, and therefore you don't know what to undo.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • You could of course `std::fill()` null pointers into the array before allocating the real pointers, which would make a "reasonable way" to know what to undo. Not that it's worth such effort when a much better approach is available. – Toby Speight Dec 06 '18 at 13:31
0

I can recomend same as the others, use std::vector<>!!! You will achieve the same on few lines of code which can everybody read.

cnt=cnt+(256/255);

well 256/255 is equal to 1 so u can rewrite it to ++cnt;

Radek
  • 518
  • 5
  • 12