Memory allocation for the array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Memory allocation for the array

  1. #1
    Join Date
    Aug 2002
    Posts
    581

    Memory allocation for the array

    Hi, ALL,
    I have very stupid problem that I'm looking at and I just need a pair of fresh eyes to help me solve it.

    Here is my code:

    Code:
    class Base
    {
    };
    
    class Derived1 : public Base
    {
    };
    
    class Derived2 : public Base
    {
    }
    
    class Bar
    {
    public:
          void SomeFunc();
    private:
          bool m_check
          Base *m_foo;
    };
    
    void Bar::SomeFunc()
    {
         if( m_check )
            m_foo[0] = new Derived1();
        else
            m_foo[0] = new Derived2();
    }
    MSVC2010 throws out compiler error which says:

    Code:
    no operator found which takes a right-hand operand of type 'Derived *' (or there is no acceptable conversion)
    What I don't understand is why?
    The pointer is an address of 0 element of an array. So what is the problem?

    I can eliminate the error by using double pointer but it will be an overkill.

    Thank you.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: Memory allocation for the array

    m_foo is not an array.

  3. #3
    Join Date
    Aug 2002
    Posts
    581

    Re: Memory allocation for the array

    Hi,
    Quote Originally Posted by GCDEF View Post
    m_foo is not an array.
    Yes, I understand this.
    But then following code shouldn't compile as well for the same reason, right?

    Code:
    class Base
    {
    };
    
    class Derived1 : public Base
    {
    };
    
    class Derived2 : public Base
    {
    }
    
    class Bar
    {
    public:
          void SomeFunc();
    private:
          bool m_check
          Base **m_foo;
    };
    
    void Bar::SomeFunc()
    {
         m_foo = new Base*();
         if( m_check )
            m_foo[0] = new Derived1();
        else
            m_foo[0] = new Derived2();
    }
    Thank you.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Memory allocation for the array

    Quote Originally Posted by OneEyeMan View Post
    Hi,


    Yes, I understand this.
    But then following code shouldn't compile as well for the same reason, right?
    Look at the simplest of examples:
    Code:
    int main()
    {
       int *p;
       p[0] = new int;  // error.  p[0] is of type int, not int*
    }
    Code:
    int main()
    {
       int **p;
       p = new int*;
       p[0] = new int; // compiles.  p[0] is of type int*
       //...
    }
    Regards,

    Paul McKenzie

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Memory allocation for the array

    Quote Originally Posted by Paul McKenzie View Post
    Look at the simplest of examples:
    Code:
    int main()
    {
       int *p;
       p[0] = new int;  // error.  p[0] is of type int, not int*
    }
    Code:
    int main()
    {
       int **p;
       p = new int*;
       p[0] = new int; // compiles.  p[0] is of type int*
       //...
    }
    Regards,

    Paul McKenzie
    Let's be careful not to confuse the issue. Even though that last example works, there is a semantic difference between a pointer and an array, and that example mixes their syntax. It should be EITHER:
    Code:
    int main()
    {
       int **p;
       p = new int*;
       *p = new int; // Pointer syntax
    }
    OR
    Code:
    int main()
    {
       int **p;
       p = new int*[1]; // Array syntax
       p[0] = new int;
    }
    Of course, the real answer is that this sort of manual memory management is an holdover from C, and usually shouldn't be done in C++. Instead, prefer to use std::array for fixed-size arrays or std::vector for dynamic-size arrays, and use std::unique_ptr or std::shared_ptr for pointers (if you have a compiler supporting C++11; otherwise, use the Boost versions).

  6. #6
    Join Date
    Aug 2002
    Posts
    581

    Re: Memory allocation for the array

    Hi, guys,
    And the memory release should be done by:

    Code:
    	for( int i = 0; i < m_count; i++ )
    		delete m_foo[i];
    	delete[] m_foo;
    but its crashing on the last line.
    Even using "delete m_foo;" is crashing.

    Thank you.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: Memory allocation for the array

    How has m_foo been allocated?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: Memory allocation for the array

    Posting your current code would help.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Memory allocation for the array

    Quote Originally Posted by OneEyeMan View Post
    Hi, guys,
    And the memory release should be done by:

    Code:
    	for( int i = 0; i < m_count; i++ )
    		delete m_foo[i];
    	delete[] m_foo;
    but its crashing on the last line.
    Even using "delete m_foo;" is crashing.

    Thank you.
    You can't just post snippets, as they do not live in a vacuum.

    Code just plucked out of a larger program doesn't help in solving the problem, as "snippets" tells us nothing about the state of the program, whether you're doing something prior to that code that causes the problem, what values those variables happen to have, etc.

    You should post all of your code that shows the allocation, usage of the data, and deallocation of the data, as any one of those three items can cause an issue.

    Regards,

    Paul McKenzie

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center