Question about Memory Allocation and the SHORT INT in STL

P

PhoneJack

I am writting an algorithm that generates very large tables in dynamic
memory on a linux system. These 2 tables are of the order of (30,000 x
30,000). They are being generated by the following code:

std::vector <short int*> table;
for(i=0; i < 30000; i++) {
row = new short int[30000];
table.push_back( row );
}

The algorithm crashes before creating the table. There is also no
feedback from the command line as to why the system crashes. I need to
determine two things:

1) What is the EXACT error that is produced by the system. Is there a
way to create an error log file that lists all errors generated by the
system at the time of the crash. Maybe, for example, it could show that
mmap failed to allocate enough memory, etc.

2) I need a way to predict the amount of memory that will be used by
the system. What size, in bits, would this table be? Is a SHORT INT
16bits when used in the vector class?

I would think that size = (30000)(30000)(16)

Thanks
 
R

red floyd

PhoneJack said:
I am writting an algorithm that generates very large tables in dynamic
memory on a linux system. These 2 tables are of the order of (30,000 x
30,000). They are being generated by the following code:

std::vector <short int*> table;
for(i=0; i < 30000; i++) {
row = new short int[30000];
table.push_back( row );
}

The algorithm crashes before creating the table. There is also no
feedback from the command line as to why the system crashes. I need to
determine two things:

1) What is the EXACT error that is produced by the system. Is there a
way to create an error log file that lists all errors generated by the
system at the time of the crash. Maybe, for example, it could show that
mmap failed to allocate enough memory, etc.

2) I need a way to predict the amount of memory that will be used by
the system. What size, in bits, would this table be? Is a SHORT INT
16bits when used in the vector class?

I would think that size = (30000)(30000)(16)

Thanks

I suspect you're running out of memory, and new is throwing
std::bad_alloc, which you don't appear to catch.

I would try the following:

std::vector <short int*> table;
int i;
try {
for (i = 0 ; i < 30000; ++i)
table.push_back(new short int[30000]);
}
catch (std::exception& e)
{
std::cout << "EXCEPTION: " << e.what << std::endl;
// drop dead here in some clean manner.
}
 
V

Victor Bazarov

PhoneJack said:
I am writting an algorithm that generates very large tables in dynamic
memory on a linux system. These 2 tables are of the order of (30,000 x
30,000). They are being generated by the following code:

std::vector <short int*> table;
for(i=0; i < 30000; i++) {
row = new short int[30000];
table.push_back( row );
}

The algorithm crashes before creating the table.

There is nothing in the code to suggest a problem _except_ if you're
running out of memory. Consider that 30k x 30k x sizeof(short) is only
1800 Mbytes (1.8 Gigabytes). And how much do you have virtual memory
in your process to play with?
> There is also no
feedback from the command line as to why the system crashes.

Surround your loop with

int i;
try {
<... your loop here ...>
}
catch (std::bad_alloc const& e) {
cout << "ran out of memory on " << i << "th loop\n";
}
> I need to
determine two things:

1) What is the EXACT error that is produced by the system. Is there a
way to create an error log file that lists all errors generated by the
system at the time of the crash. Maybe, for example, it could show that
mmap failed to allocate enough memory, etc.

Catch proper exceptions.
2) I need a way to predict the amount of memory that will be used by
the system. What size, in bits, would this table be? Is a SHORT INT
16bits when used in the vector class?

I would think that size = (30000)(30000)(16)

Yes. Got a calculator?

V
 
J

Jonathan Mcdougall

I am writting an algorithm that generates very large tables in dynamic
memory on a linux system. These 2 tables are of the order of (30,000 x
30,000). They are being generated by the following code:

std::vector <short int*> table;
for(i=0; i < 30000; i++) {
row = new short int[30000];
table.push_back( row );

}

The algorithm crashes before creating the table.

If you run out of memory, new throws an exception. Try to catch it and
to print a message to see if that's the case.
There is also no
feedback from the command line as to why the system crashes. I need to
determine two things:

1) What is the EXACT error that is produced by the system. Is there a
way to create an error log file that lists all errors generated by the
system at the time of the crash. Maybe, for example, it could show that
mmap failed to allocate enough memory, etc.

Not in standard C++. Ask in a newsgroup supporting your compiler or
system
2) I need a way to predict the amount of memory that will be used by
the system. What size, in bits, would this table be? Is a SHORT INT
16bits when used in the vector class?

Depends on the system. Try to do

std::cout << sizeof(short int);
I would think that size = (30000)(30000)(16)

If on your system a short int is 16 bits, that would give you the
number of bits you allocated. You probably want it in bytes.
sizeof(short int) returns the number of bytes:
(30000)*(30000)*(sizeof(short int)).

Jonathan
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top