-2

MAIN

tst_struct *tst1;
printf("P outside function1: %p \n", tst1); //Output: 0x0
tst_func(tst1);

Function

tst_struct *tst_func(tst_struct *tst1)
{
    printf("P from param: %p \n",tst1);  // Output 0x0
    tst_struct *tst2;
    printf("P inside function: %p \n", tst2); // Output 0x7ffeebcf8940
}

I dont understand why that happened.

I did the same thing in both functions.

If i create a variable and check for address on main, without assign any value, result is 0x0.

But if i create any variable inside another function, i receive an addresss.

There is some difference between main and "normal" functions? Thanks

roger taht
  • 51
  • 5
  • 6
    Welcome to *Undefined Behavior* - `tst_struct *tst1;` declares an *uninitialized* pointer - it's value is indeterminate. Same for `tst_struct *tst2;`. The address held be each pointer is simply garbage until assigned a valid memory address. Answer the question "What address does `tst1` hold before I attempt to print that value?" If your answer is "I don't know..." don't print it `:)` – David C. Rankin Apr 24 '19 at 03:43
  • 3
    Using the values of uninitialized variables is undefined behavior. Don't try to make sense of it. – R Sahu Apr 24 '19 at 03:43
  • Just always initialize your variables and don't worry about it :) – xaxxon Apr 24 '19 at 03:58
  • But in that case, i want to start to build a list. So for me, is necessary to start with a empty value to make a if statement like (!tst); So to do that, you guys recommend to simple after create, set tst = NULL? Cause if i try to access that, will get seg fault – roger taht Apr 24 '19 at 04:00
  • on that specific projects i have a limit of 25 lines per function, so dont want to waste to much lines – roger taht Apr 24 '19 at 04:00
  • @DavidC.Rankin But uninitialized variables takes unspecified values and usage of such unspecified values results in unspecified behavior, not undefined. The behavior is undefined since we have to cast `tst_struct *` to `void *` explicitly in order to use with `%p` in place of the ellipsis. – Some Name Apr 24 '19 at 04:47
  • Well granted, we can word-smith that down to the exact language of the standard, but the result is the same whether unspecified or undefined -- the train has already fallen off the tracks. Have you made that same comment in response to R Sahu? – David C. Rankin Apr 24 '19 at 04:53
  • @RSahu Uninitialized variables take unspecified values. Usage of unspecified values is unspecified behavior, not undefined. `N1570, 3.4.4. Unspecified behavior`: _use of an unspecified value_ – Some Name Apr 25 '19 at 11:45

2 Answers2

2

Undefined behaviour. If the value is zero, you're lucky. You've got an uninitialised variable, and are wondering why it has a random value? You may get zero in main, or in a debug build (which tends to init memory to zero), or you may not. It depends on the compiler/os/cpu, and it should never be taken for granted. It could be anything, because it's undefined.

robthebloke
  • 9,331
  • 9
  • 12
  • Getting UB that looks like what you want is the opposite of lucky - it's the hardest bug to find. – xaxxon Apr 24 '19 at 03:58
1

Undefined behaviours here.

First, I’ll just assume that you’ve included <stdio.h>.

tst1 is uninitialised. If you try to print the value, one of the possibilities is that you get 0x0. But, anything else can happen. Same thing with tst2.

Another thing, when you use %p format specifier in printf(), to be strictly conforming, you need to supply a void *. Therefore, strictly speaking, you should use, for example:

printf("P outside function1: %p \n", (void *)tst1);

See https://stackoverflow.com/a/24867850/10616174

adeishs
  • 79
  • 3