Casting and Void


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Casting and Void

  1. #1
    Join Date
    Mar 2005
    Posts
    67

    Casting and Void

    I have several doubts concerning casting and void pointers.

    Whenever you cast a void pointer to something else, can you say that this cast is like constructing a new pointer starting from the void address in memory?

    Is a void pointer directly related to the size of the processor, for example 32 bits, 64 bits, etc? What I want to know is if you can consider a void pointer as a simple address offset?

    Thanks!

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    the size of void* is platform dependent but as you guessed, it usually reflects the architecture's model: 16-bit, 32-bit or 64-bit.
    When you cast void * to a concrete type, say int *, you're not constructing (or creating) a new pointer, rather, you simply treat the original void* as a different type. A good example is malloc. This code however works only in C:

    int * p = malloc(sizeof(int));

    Here, malloc returns void *, which is implicitly converted to int *.
    You can't treat void * as an offset because it's typeless. How would you increment it for example or decrement it? If you're looking for a pointer that store offsets in bytes use char *.
    Danny Kalev

  3. #3
    Join Date
    Mar 2005
    Posts
    67
    I understand. Just help me to clear out: although the word "offset" was not the correct phrase to use when refering to a void pointer, but can you say it is a place in memory from where the compiler treats the beginning of that block of memory as something else?

    Thanks.

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    Yes, a void pointer is just like any other pointer, it is be the first location of an allocated block in the sense that you mean. But remember it is just like any other pointer... it can be null, point to bad memory, point to a function, point to the middle of a global array, be allocated, etc.

  5. #5
    Join Date
    Mar 2005
    Posts
    67
    Jonnin, can you give me more details when you say: "...But remember it is just like any other pointer... it can be null, point to bad memory, point to a function, point to the middle of a global array, be allocated, etc...."

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    int x;

    void * vp = NULL; //just like any other
    void * vp = &x; // not a block of free store memory, its a single address on the stack

    int x[1000];
    void * vp = &(x[500]);

    void * oops; //bad pointer -- not initialized yet, do not use

  7. #7
    Join Date
    Mar 2005
    Posts
    67
    I have understood. Thanks all!

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    Using void* as the address of a function is a bit problematic. The standard doesn't guarantee that this works; on most implementations it does but there are cases in which it doesn't. There are two potential problems here: a segmented architecture whereby data pointers (including void*) occupy 32 bits, and function pointers have a larger address space, say 36 bits or 64 bits. There is also the issue of address regions. Some implementations store functions' code in special memory sections that can't be accessed by void * or other data pointers. Again, this isn't very commonplace but remember that storing a function pointer in void* isn't necessarily portable.
    Danny Kalev

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    Bad example I suppose ;)
    I have actually never used a function pointer beyond supplying one to qsort or the like (i.e. using someone else's code). It seems like something to avoid unless some odd circumstance comes along? I just can't see a need for it in c++

    speaking of qsort -- while my homemade sort is no match for std::sort, and is certainly one of the worst pieces of code ever, it timed at 3 times faster than qsort in this C program I am having to work on. I'm sure implementations vary, so that could be it, or its a fluke because N is very small (100-200), but I was all ready to toss my toy out but now I think I will use it for this app. I wish I could remember how it worked...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center