Ask Your Question
0

countnonzero seg fault

asked 2015-12-17 05:35:05 -0600

ggeo gravatar image

updated 2015-12-17 05:39:02 -0600

Hello , I want to use countNonZero , but I am getting a seg fault at the countNnZero line.

vector<Mat> mycompare;
int compareSize = (N / 2) * (N -1);
mycompare.resize( compareSize );

int myCounts [ compareSize ]; 

int index = 0;
for ( int idx = 0; idx < N; idx++ )
{
    for ( int k = idx + 1; k < N; k ++ )
    {
         mycompare[ index++ ] = ( A[ idx ] == A[ k ] );
         myCounts[ index++ ] = countNonZero( mycompare[ index ] );
     }
}

OR

Is there a way to use if statement?:

if ( mycompare[ index ] == 255 ) 
    count++;
edit retag flag offensive close merge delete

Comments

@LorenaGdL:But , I am setting the mycompare[0] , access it in the countNonZero function ,then at the 2nd loop , set the mycompare[1] , access it and so on. ( I am just asking for the if statement )

ggeo gravatar imageggeo ( 2015-12-17 06:06:42 -0600 )edit

No. The increment of index does not take place at each loop run, but just after it finishes its sentence. Check the printed values for this code:

for ( int k = idx + 1; k < N; k ++ )
{
     cout << "Index: " << index << endl;
     mycompare[ index++ ] = ( A[ idx ] == A[ k ] );
     cout << "Index: " << index << endl;
     myCounts[ index++ ] = countNonZero( mycompare[ index ] );
 }

If I'm not wrong, you'll have Index: 0 and Index: 1, showing than in the same loop run you're trying to access two different indexes

LorenaGdL gravatar imageLorenaGdL ( 2015-12-17 06:23:25 -0600 )edit

@LorennaGdL:Ok, thanks!( I was a little confused)

ggeo gravatar imageggeo ( 2015-12-17 07:30:47 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-12-17 05:43:58 -0600

LorenaGdL gravatar image

updated 2015-12-17 05:45:38 -0600

You're incrementing the index variable twice before using it in mycompare[index]. So, for the first run of the loop, you first set mycompare[0] to some binary matrix, and then you try to set the value of myCounts[1] to the number of non-zero elements of mycompare[2]. And yep, mycompare[2] is still an invalid matrix. Use this:

for ( int k = idx + 1; k < N; k ++ )
    {
         mycompare[ index ] = ( A[ idx ] == A[ k ] );
         myCounts[ index ] = countNonZero( mycompare[ index ] );
         index++;
     }
edit flag offensive delete link more

Comments

@LorenaGdL:Yes!You are right,I missed that!Is there a way to use if statement for this? ( please make it an answer if you want)Thanks!

ggeo gravatar imageggeo ( 2015-12-17 05:50:52 -0600 )edit
1

@LorenaGdL:If I just do mycompare[ index++ ] = ( A[ idx ] == A[ k ] ); myCounts[ index ] = countNonZero( mycompare[ index ] ); , should't work also ? ( because it doesn't)

ggeo gravatar imageggeo ( 2015-12-17 05:57:05 -0600 )edit

No, it shouldn't, because you're setting first matrix in the vector, but trying later to access second matrix. Just put the index++ at the end. And about the if statement, why would you want to use it? Your current code is nice, avoiding per-pixel loops

LorenaGdL gravatar imageLorenaGdL ( 2015-12-17 06:00:33 -0600 )edit

What about mycompare[ index ] = ( A[ idx ] == A[ k ] ); myCounts[ index ] = countNonZero( mycompare[ index++ ] );?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-17 07:27:05 -0600 )edit

@thdrksdfthmn that will work too, as the index is incremented on the last operation. I do however prefer to increment variables outside functions, because if I later add another line of code, I migh forget to change that increment too. But I guess that's a matter of personal taste

LorenaGdL gravatar imageLorenaGdL ( 2015-12-17 07:28:51 -0600 )edit

@thdrksdfthmn:Yes,that is ok, thanks!

ggeo gravatar imageggeo ( 2015-12-17 07:33:32 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-17 05:35:05 -0600

Seen: 203 times

Last updated: Dec 17 '15