Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <assert.h>
- #define VECTOR_COL 10000000
- #define VECTOR_ROW 10
- uint32_t position_to_size(uint32_t count)
- {
- count+=1;
- if (count < UCHAR_MAX)
- {
- return count*sizeof(uint8_t);
- }
- else if (count < USHRT_MAX)
- {
- return UCHAR_MAX * sizeof(uint8_t) +
- (count - UCHAR_MAX) * sizeof(uint16_t);
- }
- else if (count < UINT_MAX)
- {
- return UCHAR_MAX * sizeof(uint8_t) +
- (USHRT_MAX - UCHAR_MAX) * sizeof(uint16_t) +
- (count - UINT_MAX - USHRT_MAX - UCHAR_MAX) * sizeof(uint32_t);
- }
- return -1;
- }
- void alloc_large_vector_slot(void* vector, uint32_t offset, int value)
- {
- if (value < UCHAR_MAX)
- *(int8_t*)(vector+offset) = value;
- else if (value < USHRT_MAX)
- *(int16_t*)(vector+offset) = value;
- else if (value < UINT_MAX)
- *(int32_t*)(vector+offset) = value;
- }
- int main (void)
- {
- // Too big for stack, allocate elsewhere
- void* large_vector = malloc(position_to_size(VECTOR_COL*VECTOR_ROW));
- // We only ever count [0,VECTOR_COL*VECTOR_ROW-1]
- uint32_t i, j, count = 0;
- for (i = 0; i < VECTOR_COL; i++)
- {
- for (j = 0; j < VECTOR_ROW; j++)
- {
- alloc_large_vector_slot(large_vector,
- position_to_size(i*VECTOR_ROW+j),
- i*j);
- }
- }
- free(large_vector);
- return 0;
- }
Add Comment
Please, Sign In to add comment