DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 8 of 8

Thread: Casting again

  1. #1
    Join Date
    Mar 2005
    Posts
    67

    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.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    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?

  3. #3
    Join Date
    Mar 2005
    Posts
    67
    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?

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    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??

  5. #5
    Join Date
    Mar 2005
    Posts
    67
    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.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    Quote 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:
    Code:
    p = (Foo*) &(f);
    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

  7. #7
    Join Date
    Mar 2005
    Posts
    67
    So does the compiler scan every type in the source files and then decides the size of void *? Is that ok?

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    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.

Bookmarks

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


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


Sponsored Links