Explanation for void *ptr=value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarandnl08
    New Member
    • Feb 2008
    • 3

    Explanation for void *ptr=value?

    1. I need explanation for void *ptr=value?

    [code=c]
    int main()
    {
    void *the_data;

    the_data=20;
    // the_data=(int *) 20;

    printf("the_dat a now :%d", the_data);
    // printf("the_dat a now :%d", *(int*) the_data);

    return 0;
    }[/code]

    When I compile this program in gcc compiler I got the output as

    the_data now:20

    But I can't do the step I commented, my compiler core dumped.


    1. Can a pointer variable directly point a value? How can we call it?
    2. I can’t deference it like *(int*) the_data? Why?
    3. As I know that void* cannot be deference directly such as *the_data,
    What (the_data) pointer contains address or value?


    Thanks in advance,
    Saravanan.S
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by sarandnl08
    1. Can a pointer variable directly point a value? How can we call it?
    No a pointer cannot point to a value, it has to point to a memory location which means another variable or a string stored in constant memory (string literals a slightly different to numeric literals in that the compiler has to place them in memory somewhere).
    Originally posted by sarandnl08
    2. I can’t deference it like *(int*) the_data? Why?
    Yes you can, the reason it did not work for you is that you have not allocated any memory to your pointer. That is when you attempted to dereference it it was not pointing to a valid piece of memory and so a memory exception occured.
    Originally posted by sarandnl08
    3. As I know that void* cannot be deference directly such as *the_data,
    What (the_data) pointer contains address or value?
    All pointers contain an address regardless of what type they point to.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by sarandnl08
      1. I need explanation for void *ptr=value?
      The gist of your program is ...

      Code:
      int *the_data;
      the_data = (void*)20;
      printf ("the_data now: %d", *the_data);
      Notice that I took some liberties with int* versus void* to enhance clarity. I'm confident that my variant is equivalent to your original program.

      Your program
      1. Declares the_data as a pointer-to-int.
      2. Sets the pointer value to "20". Perhaps this value will look more like an address if I express it in hex: 0x001C.
      3. Prints the contents of this_data; that is, the contents of 0x001C.

      Notice that there is no variable within your program whose address is 0x001C. That location is outside the domain of your program. What do you expect to find at that location? Perhaps a memory-mapped I/O register, or perhaps a flag set by another thread of execution?

      Your program dumps core because the execution environment won't let you access locations that aren't in your program. Not all execution environments are so fastidious. What you're trying to do is not unusual in embedded systems.

      One last thought -- whatever you're trying to do; address 0x001C is not written to by your program. You would therefore be strongly advised to declare this_data as follows:
      Code:
      volatile int *this-data;
      The volatile type qualifier alerts the compiler that accesses to this location must occur precisely as specified in your source code -- the optimizer is not permitted to translate your source code into an equivalent layout.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by sarandnl08
        1. I need explanation for void *ptr=value?

        [code=c]
        void *the_data;
        the_data=(int *) 20;
        printf("the_dat a now :%d", *(int*) the_data);
        [/code]

        1. Can a pointer variable directly point a value? How can we call it?
        2. I can’t deference it like *(int*) the_data? Why?
        3. As I know that void* cannot be deference directly such as *the_data,
        What (the_data) pointer contains address or value?


        Thanks in advance,
        Saravanan.S
        From your question #1 I suspect you really meant ...
        Code:
        int the_data = 20;
        int *the_data_ptr = &the_data;
        printf("the_data now :%d", *the_data_ptr);
        A pointer doesn't "pointer directly to a value"; it can only point to a memory location. However, that memory location can [will] contain a specific value.

        Comment

        Working...