简体   繁体   中英

Display struct variable with printf(“\n”)

I tried implemented matrix struct in C, ( generate and show that ). I don't have a trouble with generate but i discovered something I didn't understand. When i uncomment printf("\n") i don't get variable from matrix but whith commented printf("\n") everything working correct. It's not a big problem but I'm curious why this happen. It's looks like printf has destructed my structure.

void show_matrix(struct matrix matrix1){
     for(int i =0; i < matrix1.number_of_row; i++){
           for(int j = 0; j <matrix1.number_of_columns; j++){
            printf("%d " , matrix1.variable[i * matrix1.number_of_columns + j]);
           }
           // printf("\n");
     } 
}

Here is my main:

#include <stdio.h>
#include "matrix_operations.h"
int main(){

   int columns;
   int rows;
   printf("Enter the number of columns \n");
   scanf("%d",&columns);
   printf("Enter the number of rows \n");
   scanf("%d", &rows);
   struct matrix matrix1;
   matrix1  = generate_matrix(columns,rows);
   show_matrix(matrix1);
   return 0;
}

Here is my file matrix_operations.h:

#include "matrix.h"
#include <stdlib.h>

struct matrix generate_matrix(int columns, int rows){
int value;
struct matrix matrix1;
matrix1.number_of_columns = columns;
matrix1.number_of_row = rows;
int values[rows * columns];
matrix1.variable = values;
for(int i = 0 ; i < rows ; i++){
     for(int j =0 ; j <columns ; j++){
          value = random() %2;
          matrix1.variable[i * columns +j] = value;
          /* Uncomment to see generated matrix 
          printf("%d ", matrix1.values[ i * columns +j]);
          */
          }
   /* Uncomment to see generated matrix 
   printf("\n");
    */
   }
 return matrix1;
 }
 void show_matrix(struct matrix matrix1){
      for(int i =0; i < matrix1.number_of_row; i++){
          for(int j = 0; j <matrix1.number_of_columns; j++){
             printf("%d " , matrix1.variable[i * matrix1.number_of_columns + j]);
           }
      /* Uncomment to not working correctly
      printf("\n");
      */
      } 
} 

Here is my struct in file matrix.h:

struct matrix{
   int number_of_columns;
   int number_of_row;
   int *variable;
};

Sample input:

4
4

Sample output with commented printf (16 random numbers 0 or 1):

1 0 1 1 0 0 1 1 0 0 1 1 0 1 0 1

Sample output with uncommented printf

1 0 1 1 
-2051332800 32571 0 0 
0 0 -2053095405 32571 
-2051684704 32571 10 0 

The problem is that generate_matrix returns a struct matrix with a variable field that points at a local variable ( values ) of the function. This goes out of scope when the function returns, leaving you with a dangling pointer, and when you later dereference it, you get undefined behavior.

You need to ensure that this pointer points at something with sufficient lifetime to not become dangling. One way to do that would be to use malloc in the generate_matrix function rather than using a local variable:

int *values = malloc(rows * columns * sizeof(int));

This will allocate memory that lasts until you explicitly free it. Now you have the issue that you need to figure out when to free it (when you no longer need it), or you'll have a memory leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM