By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

simple question.

P: n/a
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;
}

Aug 10 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

On Aug 10, 4:34 pm, "raghuv...@gmail.com" <raghuv...@gmail.comwrote:
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;

}

Aug 10 '07 #2

P: n/a
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)
On Aug 10, 4:36 pm, "raghuv...@gmail.com" <raghuv...@gmail.comwrote:
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.

On Aug 10, 4:34 pm, "raghuv...@gmail.com" <raghuv...@gmail.comwrote:
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;
}

Aug 10 '07 #3

P: n/a
ra*******@gmail.com 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, "raghuv...@gmail.com" <raghuv...@gmail.com>
:: 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
Aug 10 '07 #4

P: n/a
ra*******@gmail.com wrote:
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).
Aug 10 '07 #5

P: n/a
On Aug 11, 2:56 am, Ron Natalie <r...@spamcop.netwrote:
raghuv...@gmail.com wrote:
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:
>on a 64 bit m/c if I just do g++ t.cpp everything works fine
are the standards different for 64 bit systems (not as far as i
know)... any ideas from ppl out there?
Aug 11 '07 #6

P: n/a
On 2007-08-11 17:38:39 -0400, "ar********@gmail.com"
<ar********@gmail.comsaid:
On Aug 11, 2:56 am, Ron Natalie <r...@spamcop.netwrote:
>raghuv...@gmail.com wrote:
>>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:
>>on a 64 bit m/c if I just do g++ t.cpp everything works fine

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".

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 11 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.