开发者

Pointer's weird behavior

开发者 https://www.devze.com 2023-02-12 21:54 出处:网络
Trying to run this piece of code: void print_matrix(matrix* arg) { int i, j; for(i = 0; i < arg->rows; i++) {

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_ma开发者_开发百科trix(&arg);


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

    return EXIT_SUCCESS;
}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消