What is the meaning of “wild pointer” in C?

前端 未结 11 1493
终归单人心
终归单人心 2020-11-30 04:14

Can anybody tell me, the meaning of wild pointer in C, how to obtain it and is this available in C++?

相关标签:
11条回答
  • 2020-11-30 04:35

    A pointer which is not initialized with any address is called as a wild pointer. It may contain any garbage address, so dereferencing a wild pointer is dangerous

    0 讨论(0)
  • 2020-11-30 04:36

    A wild pointer in C is a pointer that has not been initialised prior to its first use. From Wikipedia:

    Wild pointers are created by omitting necessary initialization prior to first use. Thus, strictly speaking, every pointer in programming languages which do not enforce initialization begins as a wild pointer.

    This most often occurs due to jumping over the initialization, not by omitting it. Most compilers are able to warn about this.

    eg

    int f(int i)
    {
        char* dp;    //dp is a wild pointer
        ...
    }
    
    0 讨论(0)
  • 2020-11-30 04:41

    To get a wild (aka dangling) pointer you:

    • Create an object
    • Create a pointer to that object
    • Delete the object
    • Forget to set the pointer to null

    The pointer is now classed as "wild" as it's pointing to an arbitrary piece of memory and using it in this state could cause problems with your program.

    0 讨论(0)
  • 2020-11-30 04:41

    A wild pointer is any pointer that is used (particularly as an L_value {ie (*pointer) = x } ) while having a value that is either not correct or no longer correct. It could also be used to describe the using of memory that is not defined as a pointer as a pointer (possibly by having followed a wild pointer or by using outdated structure definitions).

    There's no official definition. It's just words that we use when referring to certain pointer errors or the results of those errors.

    0 讨论(0)
  • 2020-11-30 04:41

    Wild pointer is a pointer which declaration is present but it has not been defined yet.Means we have declare a pointer - data_type *ptr; //but not define which address it is containing *ptr=100//wild pointer does not point to any valid address.so we will get ERROR printf("ptr:%d",ptr);//we will get:0(in gcc compiler)

    0 讨论(0)
  • 2020-11-30 04:43

    It is not s standard term. It is normally used to refer to the pointers pointing to invalid memory location. int *p; *p = 0; //P is a wild pointer

    Or

    int *p = NULL;
    {
      int a;
      p = &a; // as soon as 'a' goes out of scope,'p' is pointing to invalid location
    }
    
    *p = 0;
    
    0 讨论(0)
提交回复
热议问题