-
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!
-
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
-
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.
-
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.
-
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...."
-
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
-
I have understood. Thanks all!
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks