1

Do C pointer (always) start with a valid address memory? For example If I have the following piece of code:

int *p;
*p = 5;
printf("%i",*p); //shows 5

Why does this piece of code work? According to books (that I read), they say a pointer always needs a valid address memory and give the following and similar example:

int *p;
int v = 5;  
p = &v;
printf("%i",*p); //shows 5
Naive Developer
  • 700
  • 1
  • 9
  • 17
  • 6
    Because the behavior of such code is undefined, anything can happen. Including program output matching expected result. – SergeyA Mar 19 '19 at 15:37
  • No, only a static variable is initialised, which will be to `NULL` unless otherwise stated, which is an invalid pointer value. – Weather Vane Mar 19 '19 at 15:38
  • 2
    *"Why does this code work?"* - it doesn't; you're confusing *observed behavior* with *defined behavior*. Your code breaks the latter, and as such you may end up observing something that appears to work, but has no spine of defined behavior to ensure that it will. – WhozCraig Mar 19 '19 at 15:38
  • 3
    Even a broken clock is right twice a day. – Jose Mar 19 '19 at 15:39
  • @ryyker, I don't want to show the address memory, I want to show its integer value – Naive Developer Mar 19 '19 at 15:46
  • The initial value of `p` is *indeterminate*. In your specific case, it just happens to contain a value that corresponds to an address that's not protected. so your code *appears* to work without any issue. However, you've overwritten *something* that doesn't belong to you, which could cause a runtime error in a larger program. – John Bode Mar 19 '19 at 15:47
  • 1
    [How to explain undefined behavior to know-it-all newbies?](https://stackoverflow.com/questions/2235457/how-to-explain-undefined-behavior-to-know-it-all-newbies). – Lundin Mar 19 '19 at 15:51
  • Try this: `for(volatile int i=0; i<1000; i++) { volatile int* seg_fault_generator; *seg_fault_generator = 5; }` So far I'm unable to avoid a crash on any Windows compiler at least. – Lundin Mar 19 '19 at 15:53
  • Thanks @Lundin!!! An excellent example!!! – Naive Developer Mar 19 '19 at 15:56
  • 1
    @Lundin You linked to a C++ tagged question. We wouldn't want new starters to think C++ is C, would we? – machine_1 Mar 19 '19 at 16:02
  • @machine_1 I'll link to a different one if you first explain the difference of UB in C and UB in C++. – Lundin Mar 19 '19 at 16:05
  • still don't understand why SO did away with its Documentation pages ... the one for UB was by far the best resource on the topic I've ever seen. Code samples and explanations for an ever-expanding list of causes for UB .. all straight to the trash – yano Mar 19 '19 at 16:07
  • @Lundin The concept of UB is indeed the same in C and C++, but as you know the similarities between C and C++ is the cause of confusion amongst many. – machine_1 Mar 19 '19 at 16:10
  • 1
    @yano Perhaps it was a copy pasta of this: https://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults – Lundin Mar 19 '19 at 16:13
  • @Lundin Perhaps! That looks familiar .. thanks for sharing, didn't know that existed. – yano Mar 19 '19 at 16:16
  • 1
    @yano As for an actual list of all cases of UB... the very brief summary of all _documented_ cases of UB in the C standard is 15 pages long. To write a single post about all cases of UB in C, documented + undocumented, would be madness. And for C++, it would be madness++. Not that madness would have stopped people from trying in the Documentation Project... – Lundin Mar 19 '19 at 16:18

4 Answers4

5

Do C pointer (always) start with a valid address memory?

No.

Why does this code work?

The code invokes undefined behavior. If it appears to work on your particular system with your particular compiler options, that's merely a coincidence.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
3

No. Uninitialized local variables have indeterminate values and using them in expressions where they get evaluated cause undefined behavior.

machine_1
  • 4,266
  • 2
  • 21
  • 42
2

The behaviour is undefined. A C compiler can optimize the pointer access away, noting that in fact the p is not used, only the object *p, and replace the *p with q and effectively produce the program that corresponds to this source code:

#include <stdio.h>

int main(void) {
    int q = 5;
    printf("%i", q); //shows 5
}

Such is the case when I compile the program with GCC 7.3.0 and -O3 switch - no crash. I get a crash if I compile it without optimization. Both programs are standard-conforming interpretations of the code, namely that dereferencing a pointer that does not point to a valid object has undefined behaviour.

0

No.

On older time, it was common to initialize pointer to selected memory addresses (e.g. linked to hardware).

char *start_memory buffer = (char *)0xffffb000;

Compiler has no way to find if this is a valid address. This involve a cast, so it is cheating.

Consider

static int *p;

p will have the value of NULL, which doesn't point to a valid address (Linux, but on Kernel, it invalidate such address, other OS could use memory on &NULL to store some data.

But you may also create initialized variables, so with undefined initial values (which probably it is wrong).

Giacomo Catenazzi
  • 8,519
  • 2
  • 24
  • 32