-
December 18th, 2013, 02:39 AM
#1
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.
-
December 18th, 2013, 08:03 AM
#2
Re: Memory allocation for the array
-
December 18th, 2013, 07:14 PM
#3
Re: Memory allocation for the array
Hi,
Originally Posted by GCDEF
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.
-
December 18th, 2013, 09:29 PM
#4
Re: Memory allocation for the array
Originally Posted by OneEyeMan
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
-
December 19th, 2013, 01:21 PM
#5
Re: Memory allocation for the array
Originally Posted by Paul McKenzie
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).
-
December 20th, 2013, 06:05 AM
#6
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.
-
December 20th, 2013, 06:57 AM
#7
Re: Memory allocation for the array
How has m_foo been allocated?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 20th, 2013, 08:17 AM
#8
Re: Memory allocation for the array
Posting your current code would help.
-
December 20th, 2013, 11:20 AM
#9
Re: Memory allocation for the array
Originally Posted by OneEyeMan
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|