Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. "For" works with less iterations...

"For" works with less iterations...

Scheduled Pinned Locked Moved Solved C++ Gurus
10 Posts 6 Posters 4.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    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.

    raven-worxR 1 Reply Last reply
    0
    • AlvaroSA AlvaroS

      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.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

            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.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pablo_worker
              wrote on last edited by pablo_worker
              #6

              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

              AlvaroSA 1 Reply Last reply
              0
              • P pablo_worker

                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

                AlvaroSA Offline
                AlvaroSA Offline
                AlvaroS
                wrote on last edited by
                #7

                @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

                ? 1 Reply Last reply
                0
                • AlvaroSA AlvaroS

                  @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

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  You forgot to remove the &:

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

                  AlvaroSA 1 Reply Last reply
                  0
                  • ? A Former User

                    You forgot to remove the &:

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

                    AlvaroSA Offline
                    AlvaroSA Offline
                    AlvaroS
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      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];
                      }
                      }
                      

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved