-
Casting again
Is there any limitation with casting objects (or pointers) allocated in static memory? (not in heap) I think so but I will like if someone can supply me more about this.
-
The limit is only that the stack is a limited size (so is the heap, for that matter). If you can create the object on the stack (the limit has not yet been hit) then you can create a reference to it (if the pointer also fits on the stack). Is this what you meant?
-
For example,
Say you create a void pointer:
....
void *p;
Foo f;
....
If you want to cast *p to a wider pointer, are you restricted by the layout in stack of *p and f?
-
you can do
p = (Foo) &(f);
p does not matter. its a word sized place on the stack that contains the memory address that you put into it. Unless you are on some odd system that has near and far and all that? I don't even remember how near and far work, but I don't think you can cast them around -- I think you have to create it like far void* fp, near void* np etc.
You can't change the size of f either. You can't do a realloc to a pointer that points to stuff on the stack. Is that what you mean? you can try it and it will crash, and you can go off the end of f and that will also crash, just like any pointer!
I still am not sure I understand what you want to do??
-
I think you got me.
You wrote:
p = (Foo) &(f);
what about if Foo* is larger than void*? Thatīs the point I am trying to reach.
-
 Originally Posted by FededS
I think you got me.
You wrote:
p = (Foo) &(f);
what about if Foo* is larger than void*? Thatīs the point I am trying to reach.
First off, jonnin must have meant:
As for
Code:
sizeof(Foo*)> sizeof(void*)
there is no such possibility. a void * variable should be large enough t contain the largest possible data pointer. Technically speaking, this is mostly applicable to DOS style segmented architectures, which are pretty much extinct these days. Mainstream systems use uniform pointer sizes, so sizeof(void *) should be the same as sizeof(Foo*). However, as I said, even if there are systems with different pointer sizes, void* should be the largest one.
Danny Kalev
-
So does the compiler scan every type in the source files and then decides the size of void *? Is that ok?
-
one last time:
all pointers are the same size.
They are the size of a word on your machine.
so
sizeof(int *) == sizeof(char *) = sizeof(ofstream *) .....
with a few exceptions, most notably DOS.
This is not the same as saying something about the size of the memory block that the pointer points to. THAT is a different concept. The compiler does not know or care about that. Its up to the programmer to know and use allocated memory or references to arrays or whatever correctly.
and yes, I typo'ed -- the * got lost between my head and the keyboard. sorry about that.
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