problem accessing an element from large size array

#include <stdlib.h>
#include <stdio.h>
#include <iostream>

main() {
int int_len = 942132421;
//int int_len = 505262043;
size_t x(int_len);
std::cout << "length in size_t = " << x << std::endl;
std::cout << "total size required = " << x*sizeof(double) << std::endl;
size_t chunkSize = x*sizeof(double);
std::cout << "arg passed to malloc in size_t = " << chunkSize << std::endl;
double * darr = (double *)malloc(chunkSize);
if (darr != NULL) {
size_t index = 405262041;
//size_t index = 405262040;
darr[index] = 1;
std::cout << " OK till here" << std::endl;
std::cout << darr[index] << std::endl;
}else {
std::cout << "allocation failed" << std::endl;
}
}

Observations ( on my machine linux 32bit, gcc/v4.4.3p1/bin/g++ compiler, 4G ram.)
(i) If I use int_len = 505262043, malloc fails and program exits gracefully.
(ii) If I use int_len = 942132421, malloc DOES NOT FAIL and it crashes. Strange it allocates more memory but fails for lower one.
(iii) If I use int_len = 942132421, and in the lower section if I use index = 405262040 it does not crash. But the moment I increase the index by one more it starts crashing again.

Totaly perplexed by this behaviour.
In case the malloc is successful, then how can we figure it out that we will really be able to access the memory or not??

thanks
Ashishk


Last edited on
Applogy for bad indentation, somehow I am not able to indent the code in this editor.
> If I use int_len = 942132421, malloc DOES NOT FAIL

On your implementation, 942132421 * sizeof(double) is greater than the largest value that a size_t can hold. The value has been truncated.
Hi JLBorges,
I got the following output on my machine....
--------------------------------------------------------------------------------
/grid/common/pkgs/gcc/v4.4.3p1/bin/g++ -g -m32 ./main.cc ; ./a.out
length in size_t = 942132421
total size required = 3242092072
arg passed to malloc in size_t = 3242092072
OK till here
Segmentation fault (core dumped)
--------------------------------------------------------------------------------

The chunkSize is being calculated as 942132421*sizeof(double) and is equal to 3242092072. The type of chunkSize has been purposefully kept as size_t just to ensure that size_t is never truncated. malloc takes arguments as size_t and that was passed. Are you saying that the print statement just before malloc showing something else?? maximum value of unsigned int is much more.
Where exactly is this truncation happening?? In my code or inside malloc?
> on my machine linux 32bit

On a 32-bit architecture:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <limits>
#include <iostream>

int main()
{
    std::cout << "size_t bits: " << std::numeric_limits<std::size_t>::digits << '\n' ;
    std::cout << "max value size_t can hold: " << std::numeric_limits<std::size_t>::max() << '\n' ;

    const unsigned long long FIRST = 505262043ULL * sizeof(double) ;
    const unsigned long long SECOND = 942132421ULL * sizeof(double) ;
    std::cout << "value: " << FIRST << " after narrowing to size_t: " << std::size_t(FIRST) << '\n' ;
    std::cout << "value: " << SECOND << " after narrowing to size_t: " << std::size_t(SECOND) << '\n' ;
}

Output:
size_t bits: 32
max value size_t can hold: 4294967295
value: 4042096344 after narrowing to size_t: 4042096344
value: 7537059368 after narrowing to size_t: 3242092072

Thanks, I get the point.

Topic archived. No new replies allowed.