From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


"For" works with less iterations...



  • Hello to everyone.

    First of all thanks a lot for reading this post and being able to help.

    I have this for loop:

        int cells[width_i*height_i];
        for(int i=0;i<height_i;++i){
            for(int j=0;j<width_i;++j){
                cells[i*width_i+j]= matrix[i][j];
            }
        }
    

    Where width_i and height_i are "int".
    If this values are more than 3000 more or less, program gives me segmentation fault. However if they are 1000 more or less it works fine...

    Any help? It makes me crazy.


  • Moderators

    @AlvaroS
    whats the system you are developing on?
    Maybe you cannot allocate enough memory for this array?


  • Lifetime Qt Champion

    Hi,

    To add to @raven-worx, how are you allocating that array ? Because 3000 * 3000 * sizeof(int) might be 36MB depending on your architecture which is not something you can get on the stack with most systems defaults.


  • Qt Champions 2018

    I don't see the link to Qt but anyway...

    The problem might be with matrix. make sure it is declared as int matrix[height_i][width_i];
    if that doesn't fix it try converting

    • int cells[width_i*height_i]; into std::array<int, width_i*height_i>();
    • int matrix[height_i][width_i]; into std::array<std::array<int, width_i>,height_i>();

    The error will be much clearer

    To @SGaist and @raven-worx I might horribly wrong but I thought that if it was an insufficient memory problem, the error would be stack overflow or uncaught exception (bad_alloc) not segmentation fault



  • Hi! Like @raven-worx / @SGaist said, it must be a stack overflow. For everything that big you must allocate the memory on the heap:

    int (*matrix)[width_i] = new int[height_i][width_i];
    

    Or even better, avoid unsafe C arrays and use a modern data structure.



  • Yes, like the previous answers, you have to allocate in the heap,
    But remember to delete it at the end!

    int *cells = new int[16000000];
    ...
    delete [] cells
    

    Or use a qVector



  • @pablo_worker said in "For" works with less iterations...:

    Yes, like the previous answers, you have to allocate in the heap,
    But remember to delete it at the end!

    int *cells = new int[16000000];
    ...
    delete [] cells
    

    Or use a qVector

    @Wieland said in "For" works with less iterations...:

    Hi! Like @raven-worx / @SGaist said, it must be a stack overflow. For everything that big you must allocate the memory on the heap:

    int (*matrix)[width_i] = new int[height_i][width_i];
    

    Or even better, avoid unsafe C arrays and use a modern data structure.

    @VRonin said in "For" works with less iterations...:

    I don't see the link to Qt but anyway...

    The problem might be with matrix. make sure it is declared as int matrix[height_i][width_i];
    if that doesn't fix it try converting

    • int cells[width_i*height_i]; into std::array<int, width_i*height_i>();
    • int matrix[height_i][width_i]; into std::array<std::array<int, width_i>,height_i>();

    The error will be much clearer

    To @SGaist and @raven-worx I might horribly wrong but I thought that if it was an insufficient memory problem, the error would be stack overflow or uncaught exception (bad_alloc) not segmentation fault

    @SGaist said in "For" works with less iterations...:

    Hi,

    To add to @raven-worx, how are you allocating that array ? Because 3000 * 3000 * sizeof(int) might be 36MB depending on your architecture which is not something you can get on the stack with most systems defaults.

    @raven-worx said in "For" works with less iterations...:

    @AlvaroS
    whats the system you are developing on?
    Maybe you cannot allocate enough memory for this array?

    Hello again to all.
    Thanks for replying.

    Now I have tried this like you said:

            int *cells = new int[width_i*height_i]; //I have changed this line.
            for(int i=0;i<height_i;++i){
                for(int j=0;j<width_i;++j){
                    cells[i*width_i+j]= matrix[i][j];
                }
            }
    
            myFile.write (reinterpret_cast<const char *>(&cells), sizeof(int)*width_i*height_i);
            std::cout << "size of myfile after:" << myFile.tellp() << std::endl;
    
    
    

    Now it does not give an error in "For", but now it does not write fine in binary file... becuase std:cout gives: "size of myfile after: -1"

    Have I to change something in line "myFile.write..."

    Thanks a lot



  • You forgot to remove the &:

    myFile.write (reinterpret_cast<const char *>(cells), sizeof(int)*width_i*height_i);



  • @Wieland said in "For" works with less iterations...:

    You forgot to remove the &:

    myFile.write (reinterpret_cast<const char *>(cells), sizeof(int)*width_i*height_i);

    Yes! it works. Thanks a lot to all.


  • Qt Champions 2018

    if myFile is ostream why don't you use it directly instead of going through all this hassle?

    for(int i=0;i<height_i;++i){
    for(int j=0;j<width_i;++j){
    myFile << matrix[i][j];
    }
    }
    

Log in to reply