Huge memory problem

P

Pat

I am using VC++ 6.0 to develop my program.

class A
{
Object *obj
public:
A() { obj=new Object[10000] ; } ;

}

void main()
{
A *a=new A;
}

In my program, I need to allocate a huge memory for a Object obj (e.g. 10000
copies of object), which is inside the class A. However, when A's
constructor is being called. The program crash....

Could someone tell me how to solve this problem? Is it related to the C++
programming issue? BTW, my computer is 512MB RAM.

Pat
 
P

Pat

I try my program in g++, the problem is the same.
If the number of object is small (say 20). The program works well.
 
F

Frane Roje

Thi si my testing code:
#include <iostream>

#include <cstdlib>

using namespace std;

class Object {

int a;

int b;

int c;

};

class A {

Object* p;

public:

A() {p = new Object[10000000];}

~A() {delete p;}

};

int main()

{

A *a = new A;


system("pause");

return 0;

}



And it works just fine, and I have 192MB RAM,

The only error that I see in your code is that you have a

semicolon after the constructors code.



HTH


--
Frane Roje

Have a nice day

Remove (*dele*te) from email to reply
 
P

Peter van Merkerk

Pat said:
I am using VC++ 6.0 to develop my program.

class A
{
Object *obj
public:
A() { obj=new Object[10000] ; } ;

Last semicolon on this line is not needed.

Missing semicolon after the last bracket.
void main()

main() always returns int, even though VC++ 6.0 lets you get away with void
this is not legal C++.
{
A *a=new A;
}

In my program, I need to allocate a huge memory for a Object obj (e.g. 10000
copies of object), which is inside the class A. However, when A's
constructor is being called. The program crash....

In what way? Acces violation, out of memory ...etc?
Could someone tell me how to solve this problem?

You didn't provide a minimal yet complete (compilable) example that exhibits
the problem you are having. We don't know what 'Object' is and we don't know
the specifics of the crash you are seeing. Without that information, we can
only make wild guesses. Does the crash also happen when you allocate only 10
objects? If so the problem is probably not related to memory but with the
default constructor of Object.
Is it related to the C++ programming issue?

What is the 'C++ programming issue'?
BTW, my computer is 512MB RAM.

I'm very happy for you.
 
J

John Harrison

Pat said:
I am using VC++ 6.0 to develop my program.

class A
{
Object *obj
public:
A() { obj=new Object[10000] ; } ;

}

void main()
{
A *a=new A;
}

In my program, I need to allocate a huge memory for a Object obj (e.g. 10000
copies of object), which is inside the class A. However, when A's
constructor is being called. The program crash....

Could someone tell me how to solve this problem? Is it related to the C++
programming issue? BTW, my computer is 512MB RAM.

Pat

There's not much wrong with your program from a C++ point of view. You have
some semi-colons in the wrong places and void main is illegal.

Without seeing the definition of Object it hard to say what the problem is.
However it is not that you don't have enough memory. The VC++ 6 compiler
does the wrong thing when it runs out of memory. What happens with VC++ 6 is
that new returns NULL when you don't have enough memory. So if you are
running out of memory then obj would be NULL, but that isn't happening, you
are getting a crash instead.

There's only one thing for you to do, post a complete compileable program
that crashes, its the only way to get this sort of problem fixed. Without a
complete program everyone is just guessing.

john
 
J

John Harrison

Pat said:
I try my program in g++, the problem is the same.
If the number of object is small (say 20). The program works well.

Great, but you still need to post a complete program. Otherwise everyone is
guessing what the problem is.

john
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top