0

I know there are a lot of similar questions(i am not sure for a possible duplicate) but my question is specific enough.I am running a C program in Windows and Unix and i am experiencing a segmentation fault(core dumped) error.I know the source of that error.It's because i sometimes use a huge amount of memory by allocating a big array of integers.The size of my array is different every time but i can't(mostly i don't want to) use dynamic allocation of memory.

What i want is to find a way or a tool to analyze the memory usage of my C program in order to set a limit to the size of that array or in any other big memory allocation i make.To be more specific let's say that the size of that array is between 4*(2^4) bytes and 4*(2^50) bytes.The minimum is only 64 bytes but the maximum is an enormous value.How can i find out how much memory my program needs and what is a proper limit to set? I define an array like this:

int bigarray[rows][columns] ,

where rows is between 2^4 and 2^50 and columns is between 4 and 50.

Dchris
  • 2,867
  • 10
  • 42
  • 73
  • 1
    So you're using VLAs, right? Then just check for `sizeof(array) > KNOWN_LIMIT_THAT_OVERFLOWS_THE_STACK` –  Jun 04 '13 at 12:58
  • 4
    2^50 is into the petabytes. Good luck! – RichieHindle Jun 04 '13 at 12:59
  • @RichieHindle I will set the limit a lot less than 2^50,maybe 2^10 – Dchris Jun 04 '13 at 13:00
  • 2^50 is even more that MAX int (32 bit), can `[]` even handle that? – A4L Jun 04 '13 at 13:11
  • printf("%d\n", sizeof(bigarray)); as suggested above. As to hwat may be proper/useful limits, depends on the platform (Windows, Linux, etc.). Also you might be able to squeeze a lot more size from the program using a malloc/new approach. – Nicholaz Jun 04 '13 at 13:13
  • I need a minimum limit according to as many platforms as possible – Dchris Jun 04 '13 at 13:14
  • Side note: See answers to this question for using malloc/new instead: http://stackoverflow.com/questions/16885016/printf-command-causing-a-seg-fault/16885058#16885058 – Nicholaz Jun 04 '13 at 13:15

3 Answers3

2

Hi you can use tool valgrind to check for memory consumption as well as memory leaks.

Below is link to Massif: a heap profiler,hope it helps you.

http://valgrind.org/docs/manual/ms-manual.html

Chirag Desai
  • 1,249
  • 1
  • 10
  • 22
1

Get memory from the heap (malloc() and friends) instead of using the stack. The heap permits much larger allocations.

int *bigarray = malloc(sizeof(int)*rows*columns);

/* to access row r, column c */
bigarray[r*columns+c] = 42;
/* equivalent method to access row r, column c */
*(bigarray+r*columns+c) = 42;
0

To calculate the (theoretical) memory consumption:

printf("%d MB", (rows*columns*sizeof(int))/1024/1024);

.

You will have to use a new/malloc approach to get the most from it (definitely more than with your current approach on stack), i.e. if you use:

int *bigarray= new int[columns*rows];

and then access it as

val= bigarray[ x*columns + y];  // instead of bigarray[x][y];

With that, on modern platforms (Windows, Linux, etc.) and 32bit program you could expect to be reasonably ok with sizes of 500 - 1000 MB

Nicholaz
  • 1,419
  • 9
  • 11
  • how fast will it run for sizes of 500-1000MB? – Dchris Jun 04 '13 at 13:23
  • What do you mean "how fast will it run"? Run what? Speed depends on what you do with it (read, compute, cross-search), and on the processor and memory and on how busy the computer is otherwise (how much memory allocated to other tasks). – Nicholaz Jun 04 '13 at 13:29