-
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.
-
@AlvaroS
whats the system you are developing on?
Maybe you cannot allocate enough memory for this array? -
Hi,
To add to @raven-worx, how are you allocating that array ? Because
3000 * 3000 * sizeof(int)
might be36MB
depending on your architecture which is not something you can get on the stack with most systems defaults. -
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 convertingint cells[width_i*height_i];
intostd::array<int, width_i*height_i>();
int matrix[height_i][width_i];
intostd::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 convertingint cells[width_i*height_i];
intostd::array<int, width_i*height_i>();
int matrix[height_i][width_i];
intostd::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 be36MB
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.