simple question.

R

raghuv119

what am I doing wrong here. on a 64 bit m/c if I just do g++ t.cpp
everything works find. if I do g++ -m32 t.cpp it just breaks.
====================
#include <iostream>

static int mcount = 0;
using namespace std;
class Base
{
public:
Base( int i, int j) { cout <<i<<"\t"<<j<<endl; };
virtual ~Base() { };
virtual void hui() { cout <<"hello world"<<endl; };
protected:
int temp12;
};

class Child : public Base
{
public:
Child(int i, int j) : Base(i,j) {};
~Child() {};
virtual void hui() { cout <<"world hello world"<<endl; };
protected:
int temp23;
};

int main(int argc, char **argv)
{
Base *temp = new Child [16384] (3, 2);
temp[0].hui();

delete [] temp;
}
 
R

raghuv119

forgot to mention, The breakage is at the line where

Base *temp = new child[16385] (3, 2);

and then delete [] temp;- <--- crash

if temp is a child pointer everything is ok.
 
R

raghuv119

one more information
g++ -v
=====
Reading specs from /usr/lib/gcc-lib/x86_64-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
disable-checking --with-system-zlib --enable-__cxa_atexit --
host=x86_64-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-54)


forgot to mention, The breakage is at the line where

Base *temp = new child[16385] (3, 2);

and then delete [] temp;- <--- crash

if temp is a child pointer everything is ok.

what am I doing wrong here. on a 64 bit m/c if I just do g++ t.cpp
everything works find. if I do g++ -m32 t.cpp it just breaks.
====================
#include <iostream>
static int mcount = 0;
using namespace std;
class Base
{
public:
Base( int i, int j) { cout <<i<<"\t"<<j<<endl; };
virtual ~Base() { };
virtual void hui() { cout <<"hello world"<<endl; };
protected:
int temp12;

class Child : public Base
{
public:
Child(int i, int j) : Base(i,j) {};
~Child() {};
virtual void hui() { cout <<"world hello world"<<endl; };
protected:
int temp23;

int main(int argc, char **argv)
{
Base *temp = new Child [16384] (3, 2);
temp[0].hui();
delete [] temp;
 
B

Bo Persson

(e-mail address removed) wrote:
:: forgot to mention, The breakage is at the line where
::
:: Base *temp = new child[16385] (3, 2);
::
:: and then delete [] temp;- <--- crash
::
:: if temp is a child pointer everything is ok.

You have answered it yourself! :)

The type of the pointer passed to delete[] must be the same as the one
returned from new[].

C style arrays are not polymorphic, and can't be used this way.


::
:: On Aug 10, 4:34 pm, "(e-mail address removed)" <[email protected]>
:: wrote:
::: what am I doing wrong here. on a 64 bit m/c if I just do g++
::: t.cpp everything works find. if I do g++ -m32 t.cpp it just
::: breaks. ====================
::: #include <iostream>
:::
::: static int mcount = 0;
::: using namespace std;
::: class Base
::: {
::: public:
::: Base( int i, int j) { cout <<i<<"\t"<<j<<endl; };
::: virtual ~Base() { };
::: virtual void hui() { cout <<"hello world"<<endl; };
::: protected:
::: int temp12;
:::
::: };
:::
::: class Child : public Base
::: {
::: public:
::: Child(int i, int j) : Base(i,j) {};
::: ~Child() {};
::: virtual void hui() { cout <<"world hello world"<<endl; };
::: protected:
::: int temp23;
:::
::: };
:::
::: int main(int argc, char **argv)
::: {
::: Base *temp = new Child [16384] (3, 2);
::: temp[0].hui();

This isn't really working either. Don't try temp[1].hui() !!

:::
::: delete [] temp;
:::
::: }



Bo Persson
 
R

Ron Natalie

int main(int argc, char **argv)
{
Base *temp = new Child [16384] (3, 2);

You have a number of problems here. First, you can't pass initializers
to array_new. Second converting the array of CHILD to array of BASE
is bogus. It's an annoying quirk of the language that it even lets
you do that conversion. Anything other than temp[0] is INVALID.
temp[0].hui();

delete [] temp;
}
You must pass the delete [] the EXACT same value and type you got
from new [].

Base class versions can only be passed to the non-array delete (and
only then if the base class has a virtual destructor).
 
A

arun.darra

int main(int argc, char **argv)
{
Base *temp = new Child [16384] (3, 2);

You have a number of problems here. First, you can't pass initializers
to array_new. Second converting the array of CHILD to array of BASE
is bogus. It's an annoying quirk of the language that it even lets
you do that conversion. Anything other than temp[0] is INVALID.
temp[0].hui();
delete [] temp;
}

You must pass the delete [] the EXACT same value and type you got
from new [].

Base class versions can only be passed to the non-array delete (and
only then if the base class has a virtual destructor).


I agree with both Ron and Bo... i agree with what they say...
but just wondering about Raghu's statement:

are the standards different for 64 bit systems (not as far as i
know)... any ideas from ppl out there?
 
P

Pete Becker

int main(int argc, char **argv)
{
Base *temp = new Child [16384] (3, 2);

You have a number of problems here. First, you can't pass initializers
to array_new. Second converting the array of CHILD to array of BASE
is bogus. It's an annoying quirk of the language that it even lets
you do that conversion. Anything other than temp[0] is INVALID.
temp[0].hui();
delete [] temp;
}

You must pass the delete [] the EXACT same value and type you got
from new [].

Base class versions can only be passed to the non-array delete (and
only then if the base class has a virtual destructor).


I agree with both Ron and Bo... i agree with what they say...
but just wondering about Raghu's statement:

are the standards different for 64 bit systems (not as far as i
know)... any ideas from ppl out there?

The C++ standard doesn't distinguish between 64-bit systems and any
other systems: the behavior on all systems is undefined. Anything can
happen, including "works fine" for some unstated meaning of "fine".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top