2

Why aren't the numbers after the first one of each line of the matrix random numbers? Why are they zeros? For example if I print a line of this matrix I get4 0 0 0 0 but I should get the numbers after 4 as random numbers instead.

void readfile(FILE *input,int **matrix){
    int i=0, num;
    while(fscanf(input, "%d ", &num) == 1){
        matrix[i] = malloc((num+1)*sizeof(int));
        matrix[i][0] = num;
        i++;
    }
}
  • 2
    What makes you expect random numbers? – Jeremy Friesner Jan 21 '19 at 23:18
  • @JeremyFriesner my professor said they should be random, not zeros. –  Jan 21 '19 at 23:19
  • Because it just happened that this space was filled with zeroes before. –  Jan 21 '19 at 23:19
  • 6
    `indeterminate` is not `random`. – Osiris Jan 21 '19 at 23:19
  • 2
    I suspect your professor meant to say "undefined", which is different from "random". If you are looking at uninitialized memory, its contents will depend on what it was previously used for. – Jeremy Friesner Jan 21 '19 at 23:20
  • 1
    A list of four zeros is just as random as a list of any other four numbers – Code-Apprentice Jan 21 '19 at 23:20
  • @dyukha int *matrix[4] is the only declaration I did in the main before using the matrix. –  Jan 21 '19 at 23:20
  • @Code-Apprentice they're not random numbers. That can't be possible. –  Jan 21 '19 at 23:23
  • @user10947338, To begin with, your process is not the only one using this memory. Also, if process space is indeed zeroed by OS, as stated in the answer, then it makes perfect sense why the first `malloc` returned zeroed memory. –  Jan 21 '19 at 23:23
  • @user10947338 If you are selecting 4 numbers randomly, say from 0 to 100. Four zeros in a row is just as likely as any other sequence of 4 numbers. – Code-Apprentice Jan 21 '19 at 23:26
  • _IF_ you want random numbers, you could generate pseudo-random numbers with the functions `srand` and `rand` (be sure to read the documentation) which are provided by the header `stdlib.h`. – Dustin Nieffenegger Jan 21 '19 at 23:32

1 Answers1

6

Why aren't the numbers after the first one of each line of the matrix random numbers?

Why should they be?

Yes, malloc returns a newly allocated block of uninitialized memory, but nobody said that it had to be random.

Indeed, typically at process start you are going to get blank pages, just zeroed out by the operating system and provided to your process (the OS cannot recycle pages from other processes without blanking them out for security reasons), while later you are more likely to get back pages that your process has freed, so generally containing old data from your own program.

All this is strictly non-contractual, and is often violated - for example, so-called "debug heaps" generally fill in free pages with a known pattern (e.g. 0xCDCDCDCD on Visual C++) to spot usages of uninitialized memory.

Long story short: don't make any kind of assumption about the content of memory provided by malloc.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299