CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Sep 2003
    Location
    India
    Posts
    196

    void* in a struct

    hi
    i am using a struct which contains "void *" I am using void * bcoz i need to assign any data type to the vdptrVal
    Code:
    struct myStruct
    {
    CString strVal;
    void *vdptrVal;
    };
    
    typedef myStruct testStruct;
    assume a class foo
    Code:
    class foo
    {
     private:
    	foo();
    	virtual ~foo();
    	CString valStr;
    	int valInt;
    	testStruct temp;
    public:
    	void someFoo()
    	{
    	 ..
    	}
    };
    
    foo::foo()
    {
    	CString str = "HELLO";
    	valInt = 10;	
    	temp.strVal = str;
    	temp.vdptrVal = (void*) &valInt;//here acutally i want to store the address of the variable valInt whose value keeps chgning 
    }
    Now can i retrieve the value of valInt from the struct variable temp. At any point of time when the app is executing.

  2. #2
    Join Date
    Aug 2004
    Location
    INDIA
    Posts
    260

    Re: void* in a struct

    If a pointer's type is void *, the pointer can point to any variable that is not declared with the const or volatile keyword.

    A void pointer cannot be dereferenced unless it is cast to another type.

    A void pointer can be converted into any other type of data pointer.

    You should be very careful when using void pointer, you must know the exact datatype the void pointer is holding when you cast back from void to specfic data type.

    For Example
    Code:
    void *pV;
    int i = 7;
    int *pInt;
    pV = &i;
    // Cast optional in C required in C++
    pInt = (int *)pV;
    If you feel this post is useful,
    Rate this Post by clicking right top corner (Rate this Post)
    Santhosh

    ***Add strength to your country - Interesting Poll
    ***

  3. #3
    Join Date
    Apr 2005
    Location
    Mumbai,India
    Posts
    185

    Re: void* in a struct

    Is there is any problem doing this?
    Code:
    int i =*((int*)temp.vdptrVal)  ///within member function i assume it is int GetValInt();
    Life is what u make it and u can make it more simple..........and woderful.
    Rate is what give and it give me pleasure.


  4. #4
    Join Date
    Sep 2003
    Location
    India
    Posts
    196

    Re: void* in a struct

    Quote Originally Posted by santoct2002
    If a pointer's type is void *, the pointer can point to any variable that is not declared with the const or volatile keyword.

    A void pointer cannot be dereferenced unless it is cast to another type.

    A void pointer can be converted into any other type of data pointer.
    hi thnx,
    its a typo thr in my original post instead of void* say int*
    Quote Originally Posted by venkyhyd
    foo::foo()
    {
    CString str = "HELLO";
    valInt = 10;
    temp.strVal = str;
    temp.vdptrVal = (void*) &valInt;//here acutally i want to store the address of the variable valInt whose value keeps chgning
    }
    Quote Originally Posted by upashu2
    Is there is any problem doing this?
    Code:
    int i =*((int*)temp.vdptrVal)  ///within member function i assume it is int GetValInt();
    there is no problem using this but now assume if ur using this vector of type myStruct, then how like
    class foo
    {
    private:
    foo();
    virtual ~foo();
    CString valStr;
    int valInt;
    testStruct temp;
    vector<testStruct > vectStruct;
    public:
    void someFoo()
    {
    ..
    }
    };

    foo::foo()
    {
    CString str = "HELLO";
    valInt = 10;
    temp.strVal = str;
    temp.vdptrVal = (void*) &valInt;
    vectStruct.push_back(temp);
    }
    here how do i retrieve the value from the vector vectStruct and assume that thr r different types variables like int, float, double ...etc...then how...

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: void* in a struct

    Why would you make tempStruct a member of the class Foo which contains the integer variable you are dealing with???

    Also, if you know it is supposed to deal with an integer variable then why would you make a void*??? Else santoct2002's post gives the answer.

    Cheers,
    Exterminator.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: void* in a struct

    Quote Originally Posted by venkyhyd
    here how do i retrieve the value from the vector vectStruct and assume that thr r different types variables like int, float, double ...etc...then how...
    You might need to keep track of the datatype that you would be storing in testStruct's vdptrVal member. For this you could add another member in testStruct. I hope there must be some better way to accomplish this though.

    Cheers

  7. #7
    Join Date
    Aug 2004
    Location
    INDIA
    Posts
    260

    Re: void* in a struct

    Have you anytime heard about VARIANT type?? variant data type is used to extensively in COM programming. This may help you to hold different data type value. but It needs some considerable time to spend on this datatype to use your requirements, that I leave it to you. You can get more details in MSDN and in Codeguru also. Search for Variant.

    Variant
    If you feel this post is useful,
    Rate this Post by clicking right top corner (Rate this Post)
    Santhosh

    ***Add strength to your country - Interesting Poll
    ***

  8. #8
    Join Date
    Sep 2003
    Location
    India
    Posts
    196

    Re: void* in a struct

    Quote Originally Posted by santoct2002
    Have you anytime heard about VARIANT type?? variant data type is used to extensively in COM programming. This may help you to hold different data type value. but It needs some considerable time to spend on this datatype to use your requirements, that I leave it to you. You can get more details in MSDN and in Codeguru also. Search for Variant.
    i was giving thought on variant too but as i have never used it and never had a free hand on it...i thought of using vector

    anyhow shld work on variant too, any small example will do good in using variant for me.

    thnx
    venky

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: void* in a struct

    How about a thin template?
    Code:
     template <typename T>
     struct foo
     {
     	void setval(T* v)
     	{
     		val_ = v;
     	}
     
     	T* getval() const
     	{
     		return static_cast<T*>(val_);
     	}
     
     private:
     	void* val_;
     };
    Some people claim that thin templates are more space-efficient than full-blown templates, but I remain to be convinced...
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  10. #10
    Join Date
    Sep 2003
    Location
    India
    Posts
    196

    Re: void* in a struct

    Quote Originally Posted by Graham
    Some people claim that thin templates are more space-efficient than full-blown templates, but I remain to be convinced...
    THIN TEMPLATE needto browse on this first then i shll try this suggestion, thnx

    venky

  11. #11
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: void* in a struct

    I've never heard of a "thin" template. And with your template since you're tied to type T anyway why not use a T* pointer. Or is that the concept of a thin template (no typed data-members).

    I prefer to use a binary buffer and have your class know how to create one and create itself from one such that it is a reversible process. You can then specialise common cases such as int, float, double, std::string whilst keeping it extensible.

  12. #12
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: void* in a struct

    I just looked up thin templates and found it on the Symbian page (and I know Graham works on the Symbian platform which is used for mobile-phone software, because he's stated that a few times).

    The concept is that the compiler will only create one set of code for all template parameter types.

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Question Re: void* in a struct

    Quote Originally Posted by Graham
    Some people claim that thin templates are more space-efficient than full-blown templates, but I remain to be convinced...
    I got some stuff about thin templates at this link :
    http://www.codeguru.com/Cpp/Cpp/cpp_...cle.php/c7927/
    There is a small note there:
    Some compilers can be configured to automatically inline class member functions if they are called from class itself. This can eliminate "thin templates" effects. In the "Microsoft C++ compiler," you can use the /Ob0 command-line switch to disable inline function expansion or use functions with variable number of arguments (for example - void ThinAdd(SThinItem *a_poDst, ...) ); such functions can't be inlined.
    Is this the reason (in-lining) why you are not convinced if this can be space-efficient? Any other reasons?

    I just read about "thin templates" and i feel if you are separting out the template-dependent code and the template-independent code, then for sure it is going to save some extra copies of the template-independent code into the final executable.

    How do we implement thin templates? I mean how is their implementation different from usual templates? The declarations etc seem same to me or am I missing something?

    Cheers

  14. #14
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: void* in a struct

    Quote Originally Posted by venkyhyd
    Code:
    struct myStruct
    {
    CString strVal;
    void *vdptrVal;
    };
    
    typedef myStruct testStruct;
    assume a class foo
    Code:
    class foo
    {
     private:
    	foo();
    	virtual ~foo();
    	CString valStr;
    	int valInt;
    	testStruct temp;
    public:
    	void someFoo()
    	{
    	 ..
    	}
    };
    
    foo::foo()
    {
    	CString str = "HELLO";
    	valInt = 10;	
    	temp.strVal = str;
    	temp.vdptrVal = (void*) &valInt;//here acutally i want to store the address of the variable valInt whose value keeps chgning 
    }
    The problem with your code is that you give another function the pointer to a function foo local variable which get's destroyed after the function foo returns, thus any other function after returning from foo cannot use this pointer anymore. If you want to store an int in the void* you can simply store the value of the integer as pointer...:

    Code:
    temp.vdptrVal = (void*)valInt;
    And reuse it in another function.:

    Code:
    void foo::foo2 ( const testStruct& argument ) 
    {
      int intVal = (int)argument.vdptrVal;
      // ...
    }
    This is just a fix for your code for understanding. There are many better solutions here than your's for example creating a template or using VARIANT type if you work on Windows.

    I would go for the template.

    Best Regards
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  15. #15
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: void* in a struct

    Quote Originally Posted by venkyhyd
    Code:
    struct myStruct
    {
    CString strVal;
    void *vdptrVal;
    };
    
    typedef myStruct testStruct;
    Just one small thing...basically there is no need for 'typedef'ing structure that way in C++....the keyword 'struct' already defines a new type...thus, you can use it directly...
    Code:
    myStruct temp;

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured