pointer to an array of structs
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: pointer to an array of structs

  1. #1
    Join Date
    Jan 2006
    Posts
    35

    pointer to an array of structs

    This works:

    struct FOO{
    int junk;
    };

    struct FOO *ptfoo;

    ptfoo=(struct FOO*) malloc(sizeof(struct FOO)*iNumOfFoos);


    So why does it not work if struct FOO is now a member of a class and the allocation occurs in a member function?


    class Test{

    private:

    struct FOO{
    int junk;
    };

    struct FOO *ptfoo;

    int doStuff(void);

    };

    int Test::doStuff(){
    .......
    ptfoo=(struct FOO*) malloc(sizeof(struct FOO)*iNumOfFoos);
    }


    I get an error stating "Cannot convert from 'struct Test::FOO*' to 'struct Foo*'

    Thanks!

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: pointer to an array of structs

    Quote Originally Posted by eeboy
    I get an error stating "Cannot convert from 'struct Test::FOO*' to 'struct Foo*'
    C/C++ is case sensitive.

    - petter

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: pointer to an array of structs

    Please do not mix C with C++. In C++, you should be using new and delete for memory allocation and deallocation rather than malloc and free. A good reason for not doing so is that new will invoke the constructor to initializing the class/structure member variable while malloc does not.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  4. #4
    Join Date
    Jun 2005
    Posts
    67

    Re: pointer to an array of structs

    struct Test::FOO *ptfoo;

  5. #5
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: pointer to an array of structs

    One more thing in C++, it is not necessary to declare an instance of struct like, "struct FOO *ptfoo;"

    Instead you should use "FOO *ptfoo;"
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  6. #6
    Join Date
    Jan 2006
    Posts
    35

    Re: pointer to an array of structs

    Thanks! I am now using new and delete. Initially I had the same exact issue when compiling. I took Butterfly's advice and added the scope (Test: to FOO.... that worked like a champ.

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

    Re: pointer to an array of structs

    And since you are using C++....forget about the whole dynamic allocation and use a vector instead...
    Code:
    #include <vector>
    
    class foo
    {
    private:
      struct bar
      {
        int i;
      };
    
      std::vector<bar> vec;
    };
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

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

    Re: pointer to an array of structs

    Apart from all that, Test::FOO and FOO are different classes (or structs) even if they happen to have the same members.

    So you can't simply convert automatically between them unless you have defined the conversion.

  9. #9
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: pointer to an array of structs

    I think what wildfrog has said is true here. It is a matter of case sensitivity.

    Originally posted by Butterfly
    Code:
     struct Test::FOO *ptfoo;
    I think it is not necessary to qualify the FOO pointer inside the Test class with Test::FOO. the FOO pointer and the FOO struct are in the same scope.

    if FOO structure is declared in the global level as well as in the class (Test) level, to use the global struct scope resolution operator should be used. Any function inside the Test class can use the local FOO struct without qualifying it as Test::FOO.
    If there is no love sun won't shine

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

    Re: pointer to an array of structs

    Correct, it is not necessary to qualify Test::FOO within Test but it would be necessary to qualify ::FOO if you wanted to use it.

  11. #11
    Join Date
    Jan 2006
    Posts
    35

    Re: pointer to an array of structs

    Does a vector call the constructor? If so, that would work great!

  12. #12
    Join Date
    Apr 1999
    Posts
    27,423

    Re: pointer to an array of structs

    Quote Originally Posted by eeboy
    Does a vector call the constructor? If so, that would work great!
    Yes. A vector has to create a copy of your object, and to do that, it must construct the object, therefore the constructor is called.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Nov 2005
    Posts
    128

    Re: pointer to an array of structs

    ei, I've got a follow up question..

    what if I declared an array of structs. And made it as a member variable of my main dialog. Then I've got a modal child dialog which retrieves the values and also replace the values in my struct. How will i free the struct?

    Code:
    MainDlg.h:
    
    typedef struct VALUE{
    	int Lngth;
    	CString Stng;
    }stVAL;
    
    stVAL m_nOw[200];
    
    
    
    Modal ChildDlg:
    
    for( iT = 0; iT<= 5; iT++){
         
         cMainDlg->m_nOw[iT].Lngth;
         cMainDlg->m_nOw[iT].Stng;
    }
    Where and How will I free the array of structs inorder for me to reuse it?

  14. #14
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: pointer to an array of structs

    If the array is being declared as a member of your dialog class. It will only be freed when the dialog class is destroyed, e.g. when you program terminates.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  15. #15
    Join Date
    Nov 2005
    Posts
    128

    Re: pointer to an array of structs

    ei thnx,

    I over looked the struct that i have created with struct pointers. I don't need to free it because it is an array and not a pointer. But thanks again.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center