Get the size of a generic type

This is a discussion on Get the size of a generic type within the C Programming forums, part of the General Programming Boards category; Hi all! Im working on a library which manages raw memory (void *). Tipically the data are structures that may ...

  1. #1
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60

    Get the size of a generic type

    Hi all! Im working on a library which manages raw memory (void *). Tipically the data are structures that may contain pointers, thus their size can change during the execution. I need to know at run time the size oh these structures starting from a generic void*, how can i get it?.. I tried to use sizeof but without success!..Any help please?

    Cheers.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    How sizeof is causing problem?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    >> I tried to use sizeof but without success

    sizeof isn't going to tell you anything about the data referenced by a pointer. If you want to do something like this, it's going to require quite a bit of work on your part. Is there some compelling reason why you need to do this?
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  4. #4
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Well is not causing problem, but it gives me wrong answers!

    Code:
    void *p=malloc(10); //allocates 10 bytes
    sizeof(p); //this is 4 bytes as any pointer
    sizeof(*p); //this is 1 byte
    isn't that normal?

  5. #5
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Hi sebastiani, basically the library manages mathematical sets, and every set must be able to contain any kind of data (even different types in the same set) at any given time. I thought to do this using void, in a way that the library can work with the data without knowing what the element is. Thus i need to know, when i add an element to a set (and also for other operations), how much memory is needed in order to contain this element.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    >> sizeof(*p); //this is 1 byte

    You can't dereference a void pointer, so sizeof(*p) is meaningless. The point is, though, you can't retrieve the size of the data referenced by the pointer, even if it were a concrete type, since you don't know if it's a single object or an array.
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Keep in mind that sizeof is an operator whose result is determined at compile time. Consequently, you cannot possibly use it to determine the size of something when what exactly that something is is only known at run time (except in the case where all the possible options for what that something can be have the same size).
    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

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    >> basically the library manages mathematical sets, and every set must be able to contain any kind of data (even different types in the same set) at any given time. I thought to do this using void, in a way that the library can work with the data without knowing what the element is. Thus i need to know, when i add an element to a set (and also for other operations), how much memory is needed in order to contain this element.

    I still don't see why you would need the size. Can you give a compilable (stripped-down) example of what you're trying to do?
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  9. #9
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Code:
    /**	Il tipo che definisce un elemento */
    typedef struct elem_t {
    	/** tipo dell'elemento	*/
    	int type;
    	/** il puntatore alla zona di memoria doce
    	    l'elemento  allocato */
    	void *elm;
    	/** dimensione (in byte) dell'elemento */
    	unsigned int size;
    }
    elem_t;
    
    elem_t* new_elem(int type, unsigned int size, void *e) {
    	elem_t *tmp;
    	ec_null( (tmp=(elem_t*)malloc(sizeof(elem_t))), NO_SET )
    	ec_null( (tmp->elm=malloc(size+1)), NO_SET )
    	ec_null( (tmp->elm=memcpy(tmp->elm, e, size+1)), NO_SET )
    	tmp->type=type;
    	tmp->size=size;
    	return tmp;
    }
    I would like to avoid the argument "size" int the function. thus to know the size of the memory referenced by "e".
    Any suggestion??

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    >> I would like to avoid the argument "size" int the function. thus to know the size of the memory referenced by "e".

    Well, regardless, the size has to be stored somewhere. Theoretically, you could "piggyback" the size information on the allocated buffer (before or after the data), but that might be more of a hassle than its worth...
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  11. #11
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    It is a hassle!.. what i do infact is to carry the size into the elem_t structure, and pass it into the functions that requires that! Unfortunately this library is an assignment and there is some function with a prefixed signature that i cannot change!...am i scruid?..this is a pain!! HEEEELP!!

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    The only way to know the size of a generic pointer is to remember that size somewhere. There is NO, and I repeat, NO other way to get the size of a generic pointer.
    C does not offer any way of solving this problem without remembering the size, either. You are going to have make to home-baked solution or remember the size.
    An alternative is C++, which due to its generic code support can do what you want better, but I suppose this is out of the question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    Quote Originally Posted by Elysia View Post
    An alternative is C++, which due to its generic code support can do what you want better, but I suppose this is out of the question.
    Well, yeah, if you redesign. However, starting with a void pointer, C++ has no generic way to obtain the size of whatever is pointed at, unless you explicitly store the size somewhere, either. In that respect, C++ is no different from C.
    Right 98% of the time, and don't care about the other 3%.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    Yes, but replace void* with a template and you can query your size. It isn't much of a change and is the only way I can think of the be able to always query the size of a generic object.
    But I would think that you wouldn't even need the size if designed properly in C++.
    Well, I digress. It's always one option.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Thanks Elysia.. I suspected that there was noway to do that and you confirmed it! what i do is to store in a structure the pointer and the size of the memory it references, this structure is meant as an element of a set, so once an application creates a set and fills it, the type is known (from the application) thus the size can be passed as parameter to a function "add_toSet()". what happen is that the library offers the possibility of apply a function to a set of elements(using pointers to functions), this takes an element as parameter and gives back another element of the same type that might have a different size. the problem is that only the application that uses the library knows about types and so sizes, but the library only works with void* and if it needs to create an element it can't know the size!..moreover, the application talks only with a public interface of the library, because of this it doesn't have any idea of what an element (the structure described above) is, but it just knows the type set that contains void *. how would you solve this? :-)

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

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  3. Help specifying a specific size type in C++
    By indigo0086 in forum C++ Programming
    Replies: 23
    Last Post: 06-12-2007, 10:51 AM
  4. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  5. Replies: 11
    Last Post: 03-25-2003, 04:13 PM

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