1

I'm writing a program which accepts the size of an array as an argument which should then dynamically allocate memory for that size.

I want my program to return the size of the memory which was allocated. Currently I'm using argv to read the input value and then mmap for the dynamic allocation. I also use a for loop to display what number is saved in each index of the array.

I know allocating could be done with malloc, but I'm not able to use it. I guess I need to start at the last element and check all cells until I get a SEGFAULT signal.

Thanks for your help.

EDIT: Short code without libraries etc...

input = (atoi(argv[1]));
int* arr = mmap(0, input, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);

for (i = 0; i < input; i++) {
    arr[i] = i + 1; 
}
frostblue
  • 176
  • 2
  • 14
F. Mark
  • 33
  • 7
  • 1
    why arent u able to use malloc? – jonas_toth Nov 23 '16 at 14:13
  • 6
    "check all cells untill I get SEGFAULT". That looks very dodgy to me. How can you guarantee a SEGFAULT? – Fitzwilliam Bennet-Darcy Nov 23 '16 at 14:13
  • 1
    @CherubimAnand Editted. – F. Mark Nov 23 '16 at 14:14
  • @Someprogrammerdude There is no crash at the moment, I get all the values, but I don't know how to get size of allocated mem. – F. Mark Nov 23 '16 at 14:15
  • @FitzwilliamBennet-Darcy I guess I could check all the cells in 'array' for last to first, and than when i get SEGFAULT I know I reach the memory which is not allocated to me? But that was just my guess. – F. Mark Nov 23 '16 at 14:16
  • The length will be the length you asked for (`input`), unless this couldn't be satisfied, in which case it will return `MAP_FAILED` (=-1). – TripeHound Nov 23 '16 at 14:20
  • @TripeHound How to check how much memory it costs for like 5 int numbers? – F. Mark Nov 23 '16 at 14:22
  • Note that the SEGFAULT will only occur after you try to access a page that does not belong to you. It does not have fine enough resolution to stop at a byte. This assumption would break when, for example, two memory allocations used nearby chunks of memory, which is perfectly valid. – The Vee Nov 23 '16 at 14:22
  • You would then think that the whole page was allocated for your array and would end up overwriting the other allocation, or worse, some of its metadata used by `free()` and friends. – The Vee Nov 23 '16 at 14:23
  • 5
    XY problem and very wrong approach. Not clear what you try to solve, but provoking undefined behaviour is definitively wrong. It's like trying to find electrical wires in a wall by hammering nails into the wall until you get sparks. Don't follow that path any further! What is the problem allocationg the memory with `malloc`? You should provide more details and a [mcve]. – too honest for this site Nov 23 '16 at 14:27
  • @F.Mark the _actual_ size of memory allocated by `malloc` in bytes is the number you provided as parameter to `malloc` plus a certain non specified number of bytes used for internal bookkeeping by the memory manager (typically 8 to 16 bytes or so). But I'm still not sure what you actually are asking or what you are trying to achieve. – Jabberwocky Nov 23 '16 at 14:49
  • @MichaelWalz I'm not using malloc. – F. Mark Nov 23 '16 at 14:56
  • @F.Mark but you should. Why do you need to use `mmap`? – Jabberwocky Nov 23 '16 at 14:56
  • @MichaelWalz Answered below. – F. Mark Nov 23 '16 at 15:00
  • You should check the return value from `mmap()`. You should show how you set `fd` — when using MAP_ANONYMOUS, the value should be `-1` for maximum portability. – Jonathan Leffler Nov 23 '16 at 18:42

1 Answers1

6

You can't check the amount of allocated space in C or check the size of arrays. You need to save it as a variable explicitly by yourself.

pi_pi3
  • 159
  • 4
  • So using mmap I can't check how much memory was allocated? Beter void using mmap? Or is possible to solve this using mmap? – F. Mark Nov 23 '16 at 14:30
  • @F.Mark `mmap` or `malloc` the problem is the same. When you allocate memory _you_ know how much you did allocate, just keep track of that number. That's it. Period. – Jabberwocky Nov 23 '16 at 14:35
  • @MichaelWalz So this could be solved by storing the default size of memory and than compare it with the size after allocating? Because I want my program to print out like "You allocated 5MB of memory". – F. Mark Nov 23 '16 at 14:37
  • @F.Mark I don't understand your question. The quantity of memory allocated is in the `input` variable. What do you mean by _default size of memory_ ? There is no such thing. – Jabberwocky Nov 23 '16 at 14:43
  • @MichaelWalz Yes, if I input 5, than what would be stored in array? 1, 2, 3, 4, 5? If yes, than I want to know how much MB is that, or I want my program to display how much memory was allocated to it. – F. Mark Nov 23 '16 at 14:47
  • For mmap, there's /proc/self/maps but I can't think of a situation when parsing it would be preferable to just saving the size somewhere. – arsv Nov 23 '16 at 14:48
  • 1
    @F.Mark If you input 5, you've just allocated 5 bytes, which is roughly 0.000005 MB. What you store in the array doesn't matter for the size – pi_pi3 Nov 23 '16 at 14:51
  • @F.Mark (forget about `mmap`, you need `malloc`). If your input is 5 (for storing 5 `int`s) and you use `int* arr = malloc(input * sizeof (int));` the system allocates 20 bytes (assuming 32 bit ints) plus an undetermined (but small) number of bytes for of bookkepping. – Jabberwocky Nov 23 '16 at 14:55
  • @pi_pi3 So if I input 2^8 elements, than I allocated 256 bytes? – F. Mark Nov 23 '16 at 14:57
  • @MichaelWalz Well that exactly is the problem. I know how to use malloc, because we have been learning this for the past month. But now we are at mmap and working with it. – F. Mark Nov 23 '16 at 14:58
  • @F.Mark you mmap `input` bytes. So I suppose the _actual_ size used size is `input` rounded up the the next page boudary (typically 4K), but who cares ?. Still not sure what your actual problem is. – Jabberwocky Nov 23 '16 at 15:06
  • @MichaelWalz So if I input 100 then I mmaped 100 bytes? The problem is I should input the size of array and than my program should return how much memory was allocated. – F. Mark Nov 23 '16 at 15:12
  • @F.Mark sorry, I'dont get what your _actual_ problem is, nor did anybody else who was dealing your question. I'm giving up. – Jabberwocky Nov 23 '16 at 15:13
  • @F.Mark If you input 100, then you've mmaped 100 bytes. An int is 4 bytes wide, so you're gonna have to multiply your input by 4, or rather sizeof(int) to be portable. int* arr = mmap(0, input*sizeof(int), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0); – pi_pi3 Nov 23 '16 at 15:15