0

Trying to run this piece of code:

void print_matrix(matrix* arg)
{
     int i, j;
     for(i = 0; i < arg->rows; i++) {
         for(j = 0; j < arg->columns; j++) {          // gdb shows, that arg->columns value
             printf("\n %f", arg->data[i][j]);        // has been changed in this line (was 
         }                                            // 3, is 0)
         printf("\n");
     }
}

matrix is a structure:

typedef struct matrix_t 
{
    int rows;
    int columns;
    double** data;

} matrix;

arg is properly allocated 3x3 matrix, rows = 3, columns = 3

Function does print only \n's .

Compiler is gcc 4.5. Any ideas?

EDIT:

int main()
{
    matrix arg;
    arg.rows = 3;
    arg.columns = 3;
    arg.data = (double**)malloc(sizeof(double*) * arg.rows);
    int i;
    for(i = 0; i < arg.rows; i++) {
       arg.data[i] = (double*)malloc(sizeof(double) * arg.columns);
    }

    arg.data[0][0] = 1; 
    arg.data[0][1] = 2;
    //......
    print_matrix(&arg);


    for(i = 0; i < arg.rows; i++) {
        free(arg.data[i]);
    }
    free(arg.data);

    return EXIT_SUCCESS;
}
5
  • 1
    How did you allocate and populate data? Why is data declared to be a pointer to a pointer? Commented Feb 21, 2011 at 9:10
  • 1
    Please post a complete code sample. Incomplete samples, when fleshed out by others, may not demonstrate the same behavior you're experiencing. Commented Feb 21, 2011 at 9:19
  • 1
    Copy-pasted code here : ideone.com/SoyQH . Seems to work as expected. What's the question again? Commented Feb 21, 2011 at 9:44
  • @Shawn Chin: That's interesting. print_matrix does not print matrix properly. Debugger shows that arg->columns value changes in line with second for - loop. Looks like code works as it should. I'll try to figure this out. Commented Feb 21, 2011 at 9:50
  • How do you build this code? Do you get any compiler warnings, and do you see the same error both with and without optimizations enabled? Commented Feb 21, 2011 at 10:38

2 Answers 2

2

As mentioned in comments above, there seems to be nothing wrong with the code. Works fine when compiled on my machine and on IDEone. See http://ideone.com/SoyQH

I had a few minutes to spare, so I ran a few extra checks. These are pretty much the steps I take before each code commit or when I need some hints when debugging.

Testing with strict compiler flags

Compiling with gcc using -Wall -pedantic there were some warnings regarding ISO C90 incompatibilities but no show stoppers.

[me@home]$ gcc -Wall -pedantic -g k.c
k.c:17:55: warning: C++ style comments are not allowed in ISO C90
k.c:17:55: warning: (this will be reported only once per input file)
k.c: In function `main':
k.c:30: warning: ISO C90 forbids mixed declarations and code

Warnings related to :

  • use of C++ style comments, i.e. // instead of /* ... */
  • in main(), declaration of int i; was mixed in with code. C90 expects all declarations to be done at the beginning.

Using split

After addressing the above warnings, ran splint -weak on the code.

[me@home]$ splint -weak k.c
Splint 3.1.1 --- 15 Jun 2004

Finished checking --- no warnings

Nothing to report.

Valgrind

Valgrind confirms that there are no memory leaks but complains about the use of unitialised values in printf (not all elements in args->data were given values).

[me@home]$ valgrind ./a.out
==5148== Memcheck, a memory error detector.
... <snip> ...
==5148==

 1.000000
 2.000000
==5148== Conditional jump or move depends on uninitialised value(s)
==5148==    at 0x63D6EC: __printf_fp (in /lib/tls/libc-2.3.4.so)
==5148==    by 0x63A6C4: vfprintf (in /lib/tls/libc-2.3.4.so)
==5148==    by 0x641DBF: printf (in /lib/tls/libc-2.3.4.so)
==5148==    by 0x804842A: print_matrix (k.c:18)
==5148==    by 0x8048562: main (k.c:42)
... <snip> ...
==5148==
==5148== ERROR SUMMARY: 135 errors from 15 contexts (suppressed: 12 from 1)
==5148== malloc/free: in use at exit: 0 bytes in 0 blocks.
==5148== malloc/free: 4 allocs, 4 frees, 84 bytes allocated.
==5148== For counts of detected errors, rerun with: -v
==5148== All heap blocks were freed -- no leaks are possible.

Conclusion

Nothing to report. Moving on.

Sign up to request clarification or add additional context in comments.

Comments

1

matrix.data is a wild pointer. You need to allocate memory for the matrix and make data point to it.

2 Comments

OP : arg is properly allocated 3x3 matrix, rows = 3, columns = 3
@Aviral: anyone who needs to ask on SO what's going wrong with this code can't be trusted to make such a statement....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.