array of any type

This is a discussion on array of any type within the C Programming forums, part of the General Programming Boards category; I would like to have an array of any type, which means that I can store anything in that array, ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    array of any type

    I would like to have an array of any type, which means that I can store anything in that array, such as structs, char, int, etc.. etc. How can I do this.. so far this is what I have in mind:

    void** newArray = (void**) malloc(newBox->size * newBox->numslots);

    where size is the size of each array slot and numslots is the number of slots...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    The size of each array slot would be the size of a pointer to void, so it should be:
    Code:
    void **newArray = malloc(sizeof(*newArray) * newBox->numslots);
    However, I am not sure how are you going to keep track of each type in order to cast the pointers to void appropriately.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by laserlight View Post
    The size of each array slot would be the size of a pointer to void, so it should be:
    Code:
    void **newArray = malloc(sizeof(*newArray) * newBox->numslots);
    However, I am not sure how are you going to keep track of each type in order to cast the pointers to void appropriately.
    hmm..what do you mean by that?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by -EquinoX-
    hmm..what do you mean by that?
    Recall that you cannot dereference a pointer to void.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    If I want to copy a struct to index i at the array above, can I do this.. as I tried and it didn't work..

    memcpy((void*) &(newArray[i]), DiskStruct, size);
    Last edited by -EquinoX-; 04-02-2009 at 09:12 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by -EquinoX-
    If I want to copy a struct to index i at the array above, can I do this.. as I tried and it didn't work..
    You have a dynamically allocated array of pointers to void, so I do not think you can go around copying a struct to it, or any of its elements. At best you can have a pointer to void point to the given struct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so you're saying I should use an array of pointers? is that the way to go?

    if this is true then say the pointer to the given struct is changed then this will be changed as well.. I don't want this to happen.. so should I copy the pointer to the array? this is getting confusing...

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    or maybe is there a way so that I can copy the actual data, i.e a struct to an array... if there is how can I do that??

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you ACTUALLY want to store, and once it is stored, how do you know what it is?

    There are several ways to store generic data dynamically, but it's generally more complicated than simply malloc() a block of memory and stufff the data in there. You need to know WHAT is stored in some way - be that the order of elements, a tag of some sort, a second list, or some other way.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by -EquinoX-
    so you're saying I should use an array of pointers? is that the way to go?
    I don't know, but I presumed that that was your intention.

    Quote Originally Posted by -EquinoX-
    if this is true then say the pointer to the given struct is changed then this will be changed as well..
    You could declare the pointers as pointing to const void.

    Quote Originally Posted by -EquinoX-
    or maybe is there a way so that I can copy the actual data, i.e a struct to an array... if there is how can I do that??
    An array can only contain objects of the same type, so you cannot have an array of objects of different types, nor can you have an array of pointers to different types.

    At a more macro level, what exactly are you trying to do? That is, why do you want "an array of any type"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    why I want to have an array of any type is because I want to implement a mailbox system, where users can send/receive from it.. the users basically can send anything, struct, char, int.. so I am thinking this is a way to go... any ideas how people usually implement mailboxes? if I can't use an array?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And the user-code that looks in the mailbox will know what is in the mailbox how?

    I have worked with a message-based OS previously, and it used separate memory allocations with a special tag to identify the content of the packet. So you would have a generic pointer to a message object, and then put that in the message-queue of the receiving process.

    I think you could do something similar, but without knowing how you expect the overall concept to work, it's hard to say.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Are those mboxes going to contain MIME attachments?

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes, this is the message for the inter process communication of the OS that I am talking about.. if you just have a pointer to that message then that would mean the message could be changed anytime during execution.... we don't want that to happen right, we want whatever the user sends and someone else receives to be the same.. if we're passing a pointer then between the period of sending and receiving the message content could be changed or lost

    >>Are those mboxes going to contain MIME attachments?

    no it won't, the biggest thing it's support to work is for a struct.. it should work for chars, ints, as well...

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    well back to my first post.. someone pointed out that an array can't have different size.. that I agree.. so therefore say I have an array of void* with a fixed size slot each.... then the size of the message, whatever the type of the message is, would be less than the size of each slots (so say the size of struct is 40 bytes and size of each slot is 100 bytes0... but now the issue is how can I declare an array of void* of each slot size is SLOT_SIZE and copy the contents that I want to store to a particular slot in the array?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Dynamically allocated array of an abstract type
    By Zerok in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2008, 05:50 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 05:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21