Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It appears the program is accessing the same memory location for every array <code>int tab[n]</code> you declared but, as mentioned, it should not work. </p> <p>But I think what's happening here is something like: you are allocating tab[] inside main(), let's say, under the address 0x00000001 (take it as an example only). The array have n integers but no values at all. </p> <p>Then you go into myInput(), declare the array again (same size), in other address, like 0x001F0000, and then set the values, one by one. </p> <p>So, when the function terminates, it frees the allocated memory of its variables, so your array does not exist anymore. </p> <p>But wait, this is C, so when you free the memory, you only tells the heap (or the memory allocator, in general) that the addresses can be used again. You do NOT exactly remove the values from memory. </p> <p>Then you call myDisp() and declare your array again. It appears the memory you just requested has higher priority and then it is somewhat given again to your program. So your array is again instanciated and on the same address. </p> <p>So, even if you did not fill it with values, the memory is read (as it is always valid in C) and the values are still there. </p> <p>Oh, and the array declared inside main()? Nothing happens to that. Try to print its values and I bet you'll not have the correct ones.</p> <p>That is my guess. </p> <p>EDIT: Just to see things happening: try to declare another array after tab (do not rename tab), say tab2, same length, and use it to put your values instead of tab, then let the program run again :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload