Question about Memory Allocation and the SHORT INT in STL



 DEVELOP > c-Plus-Plus > Question about Memory Allocation and the SHORT INT in STL

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "PhoneJack"
Date: 13 Jul 2005 03:48:33 PM
Object: Question about Memory Allocation and the SHORT INT in STL
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
.

User: "Jonathan Mcdougall"

Title: Re: Question about Memory Allocation and the SHORT INT in STL 13 Jul 2005 04:34:11 PM

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
.

User: "Victor Bazarov"

Title: Re: Question about Memory Allocation and the SHORT INT in STL 13 Jul 2005 04:03:08 PM
PhoneJack wrote:

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
.

User: "red floyd"

Title: Re: Question about Memory Allocation and the SHORT INT in STL 13 Jul 2005 03:59:25 PM
PhoneJack wrote:

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


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER