Thread: Typecasting on a larger scale

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Typecasting on a larger scale

    Though I faced the problem while writing C++ code, I thought that C programmers would have more experience with these kinds of ideas..!
    Suppose, I have an array of 20 integers.
    How can I use that memory for a struct which has ..say..an int, a long, a char, and a small string? (I'm considering that it fits. )
    Last edited by manasij7479; 07-02-2011 at 11:39 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I can't imagine a scenario where this seems like a good idea. On the off chance you have such a scenario, what about a union?

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    The scenario is that : I need to make the main memory of a simple(and rather high level...skipping 0s and 1s) virtual machine.

    I considered an union...but..is using a union a good idea when it can contain a 500 byte stream as well as a small char ?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're doing memory management, why are you trying to do something one-size-fits-all? You've got your memory pool; why not allocate from it as you need it, and store a list of pointers to where your various memory allocations are?

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    why are you trying to do something one-size-fits-all
    I thought about allocating the total memory of each object upon creation, and then allocate from that as necessary.
    Also, I would have allocated it, divided into blocks of a certain size....emulating the segmented model of memory.

    But I also tried to store about 100 void pointers and use them to allocate as necessary, but got some error regarding void* not being a pointer to object.
    [Though it is c++, could you also show how to allocate to a void pointer using new, not malloc ?]
    Last edited by manasij7479; 07-02-2011 at 11:58 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you're working on windows... use the Windows Heap*() functions and create your own heaps as needed.

    HeapCreate Function (Windows)

    You can make as many as you need.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by CommonTater View Post
    If you're working on windows... use the Windows Heap*() functions and create your own heaps as needed.

    HeapCreate Function (Windows)
    Nope....on linux.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by manasij7479 View Post
    But I also tried to store about 100 void pointers and use them to allocate as necessary, but got some error regarding void* not being a pointer to object.
    You don't get that error if you allocate to void*. You get that error if you try to use a void* pointer, and based on just what's in this thread, there is no earthly reason for you to be using any of these pointers. Your job is to get the memory and hand it off to the user, and then your job is done (until they try to free). Don't try to access the memory, because that's Not Your Job.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I'm not sure I understand that.
    Suppose I have an array of void* s.
    I allocate its first element to an int* capable of holding a single int.

    Then, if I get an error if I try to assign :
    *myarray[0] = 56;

    Won't that error be there even when user ...in my case, the function called shell, accesses it
    ?
    I got it.. I need to typecast it into a int* whenever I want to access it.
    Last edited by manasij7479; 07-02-2011 at 12:13 PM. Reason: I got it.. I need to typecast it into a int* whenever I want to access it.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course not, because your user knows it's an int*. You have no idea what it is, you just know how big it is.

    (EDIT: After all, this is exactly how malloc works -- all it has to hand out are void*, but it can set aside the memory from the pool and hand you that void*, and then you happily use it because you assigned the value to an int* or a char* or a whatever.)

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Nope....on linux.
    My condolences....

  12. #12
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Hello again...
    What I finally have is...[forgive a little C++ usage !]

    void* mem[100]; //i.e std::array<void*, 100> mem; //inside the main struct/class of the virtual machine

    Whenever a user application/function wants memory it can demand some with memalloc ..declared as
    template<class T> T* memalloc(size); //which takes the void* just below the 'virtual' stack pointer and returns it as a T* capable of holding 'size' no. of T objects .

    But I can't find a way to define memalloc without using malloc or new. Is it possible to do so ?
    Also, just for the sake of curiosity, How would I do this totally in C, i.e..without templates ?


    ***and***
    My condolences....
    :P __noflame__("It'd surprise you that I'm quite happy using Linux , unlike your favourite os;.
    Last edited by manasij7479; 07-02-2011 at 10:41 PM.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    :P __noflame__("It'd surprise you that I'm quite happy using Linux , unlike your favourite os;.
    ... and quite thoroughly humourless, too.

    It was a reference to not being able to use the Heap*() functions... NOT your OS.
    Last edited by CommonTater; 07-03-2011 at 07:51 AM.

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    It was a reference to not being able to use the Heap*() functions... NOT your OS.
    Well, I got on the shorter end of the ambiguity .

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by manasij7479 View Post
    Hello again...
    What I finally have is...[forgive a little C++ usage !]

    void* mem[100]; //i.e std::array<void*, 100> mem; //inside the main struct/class of the virtual machine

    Whenever a user application/function wants memory it can demand some with memalloc ..declared as
    template<class T> T* memalloc(size); //which takes the void* just below the 'virtual' stack pointer and returns it as a T* capable of holding 'size' no. of T objects .

    But I can't find a way to define memalloc without using malloc or new. Is it possible to do so ?
    If you've got your linux sources, you can probably look at what malloc() does itself in glibc (which might be "call the system function", which is the linux version of calling the *heap functions). I can't remember (or more likely never knew) whether there's a kalloc or something like that in the kernel code.
    Also, just for the sake of curiosity, How would I do this totally in C, i.e..without templates ?
    You would do the right and proper thing and return a void*. Remember, C knows what a void* is for, so you can assign a void* to a anything* (unlike C++ which requires a cast).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scale in perfmon tool
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 02-03-2008, 01:54 AM
  2. Large scale crawler with C/C++???
    By joshskender in forum C++ Programming
    Replies: 12
    Last Post: 10-12-2006, 05:13 AM
  3. Scale a picture
    By VOX in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-05-2004, 09:54 AM
  4. Scale MDI child to parent
    By bludstayne in forum C# Programming
    Replies: 0
    Last Post: 07-10-2004, 02:26 PM
  5. What is the IQ scale?
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-23-2002, 03:51 AM