1

The programm falls in the infinite loop. But arr->count printf prints a normal value (4, for example). count has a type unsigned int and arr is a pointer to int. What's the problem here? loop prints arr values at first and then continues to print trash values

In arrat_get it prints array just fine

struct _arr {
    size_t count;
    int* arr;
} ;

typedef struct _arr array_t;


array_t* array_get(FILE* file){
    int* arr = NULL;
    size_t count = 0;
    array_t* arr_t;
    array_t temp;
    int i = 0;

    if (!file) {
        fprintf(stderr, "there is no such file\n");
        return;
    }



    if (fscanf(file, "%u", &count) == EOF) {
        fprintf(stderr, "can't read count from file\n");
        return;
    }

    temp = array_create(arr, count);
    arr_t = &temp;

    printf("%i\n", arr_t->count);


    for (i = 0; i < arr_t->count; i++){
        if (fscanf(file, "%d", &arr_t->arr[i]) == EOF) {
            fprintf(stderr, "can't read arr from file\n");
            return;
        }
    }

    for (i = 0; i<arr_t->count; i++)
        printf("%d ", arr_t->arr[i]);
    printf("\n");

    return arr_t;
}


int main(){
   array_t* arr_t;
   int i = 0;


   printf("enter count and arr:\n");
   arr_t = array_get(stdin);

   printf("count in main=%u\n", arr_t->count);
   for (i = 0; i<arr_t->count; i++)
       printf("%d ", arr_t->arr[i]);



   getch();

   return 0;
}
  • 3
    Please show the code that sets this up, complete with the type definition of your struct. – Sergey Kalinichenko Nov 18 '13 at 08:57
  • Do you really only call `printf` in the body of the loop? If you are doing something more complex, it is possible that an off-by-one error is modifying the value of `i` and/or `arr_t->count`. Try also printing `i` and `arr_t->count` inside the loop, and things should become clearer. – user4815162342 Nov 18 '13 at 08:58
  • Also, please don't end your variable names with `_t` - that suffix is by convention used for types, and even then it's inadvisable to use it because POSIX reserves it for system types. – user4815162342 Nov 18 '13 at 08:59
  • 2
    Wild guess: `arr_t` is initialized by a function call, and that function is returning a pointer to a local variable. – Mat Nov 18 '13 at 09:00
  • i tried to print `i`. now i tried `arr_t->count` in a loop and it is a trash value –  Nov 18 '13 at 09:04
  • 1
    @user3000073: it's unclear to me whether or not you understand the problem that Mat described. Returning a pointer to a local variable results in an invalid pointer. In this particular case whatever it's pointing to is trashed by the `printf()` call. – Michael Burr Nov 18 '13 at 09:21
  • @michael-burr: why `count` value is valid, when i print it not in the loop? –  Nov 18 '13 at 09:24
  • Because it hasn't been trashed yet. – Michael Burr Nov 18 '13 at 09:25
  • **Never** declare names which start with underscore `_`. Those are reserved for the compiler use. Using `_arr` is not legal. – user694733 Nov 18 '13 at 09:52
  • 1
    Where is `array_create()`? – Shafik Yaghmour Nov 18 '13 at 10:28
  • It can be integer overflow. Maximum value of `unsigned int` larger than maximum value of `int`. Does program crash after it prints trash values? UPD. Anyway, it is only assumption. Why not show all the code? – JIghtuse Nov 18 '13 at 08:56
  • i have tried to print `count` with `i` insteed of `u`, but that changes nothing. and 4 count value can pass in both signed and unsigned int –  Nov 18 '13 at 08:59

1 Answers1

0

This is not the solution of your problem, but may help you to find it:

#include <stdio.h>
#include <stdlib.h>

struct _arr {
    int count;
    int* arr;
} ;


int main(){
    int i = 0;
    struct _arr myArray[10];

    for (i = 0; i < 10; i++) {
        myArray[i].count = 9;
        myArray[i].arr = &myArray[i].count;
    }


    for (i = 0; i < (myArray->count); i++)
       printf("%d    %p\n", myArray[i].count, myArray[i].arr);


    return 0;
}

OUTPUT:

Compiling the source code....
$gcc main.c -o demo -lm -pthread -lgmp -lreadline 2>&1

Executing the program....
$demo 
10 0x7fffeb764c00
10 0x7fffeb764c10
10 0x7fffeb764c20
10 0x7fffeb764c30
10 0x7fffeb764c40
10 0x7fffeb764c50
10 0x7fffeb764c60
10 0x7fffeb764c70
10 0x7fffeb764c80
yulian
  • 1,601
  • 3
  • 21
  • 49