0

I have read in: How does free know how much to free? that when one has some memory allocation denoted with a pointer such as

float (*ptr)[10]=malloc(sizeof(float)*100)

for a 10x10 array, ptr has a "head" to it with "accounting" information telling of the "step size" and what not so that you can properly perform pointer arithmetic and use free and whatnot.

Is there a consistent (not architecture dependent) and reliable (defined behavior) that can allow one to get their hands on this information?

I have read elsewhere that the de facto way to track array length when there are casts and dynamic memory allocations about is to manually allocate a slot to store the size. This naturally leads me to believe the answer to my question is 'no' yet I think I'd rather not make assumptions or I'll get my own sort of memory leakage.

Ron
  • 14,674
  • 4
  • 34
  • 47
CircArgs
  • 570
  • 6
  • 16
  • If you're writing C++, don't use `malloc()`. However, you must be writing C because you'd need a cast if you were using C++. Don't spam tags — C++ is a very different language from C (and C# is different again from both, though there isn't a C# tag on the question now, so you're OK on that). – Jonathan Leffler Aug 19 '17 at 03:05
  • There is no defined standard way to get at the 'size of the block of allocated memory'. Each implementation has to have a way of knowing, but there's no way for a programmer using the implementation to know the size (in general). – Jonathan Leffler Aug 19 '17 at 03:06
  • 6
    @JonathanLeffler Just took the 3 suggested tags assuming the system knew better than I did – CircArgs Aug 19 '17 at 03:06
  • 1
    OK — 'tis interesting that both C and C++ got suggested. Please don't accept that suggestion in future. I wonder if this is worth raising as an issue on MSO (Meta Stack Overflow) — it isn't helpful to those of us who frequent the C and/or C++ tags (it is a very common complaint about questions, but I was not aware that the system made the suggestion). – Jonathan Leffler Aug 19 '17 at 03:08
  • @JonathanLeffler So it is dependent on some number of things, but if all is known, system, architecture, compiler, you're saying there is no resource to find out how things are formatted in memory? – CircArgs Aug 19 '17 at 03:08
  • You could look at your chosen compiler's implementation of `malloc` and `free` to see what it does. – 1201ProgramAlarm Aug 19 '17 at 03:11
  • There is no standard (neither de jure nor de facto standard) way to get at the information about the size of a block of memory allocated. All else apart, the size allocated by the library is usually bigger than the size requested (definitely because of the housekeeping data, but even the data portion may be rounded up to a multiple of 8 or a multiple of 16) — should the code report the size requested or the size allocated? – Jonathan Leffler Aug 19 '17 at 03:11
  • @JonathanLeffler Well, I guess that about sums it up. Got the answer I expected. What is done with a question such as this now? – CircArgs Aug 19 '17 at 03:12
  • One of three options: (1) you delete it, or (2) I convert my commentary into an answer, or (3) someone else creates an answer which rehashes my commentary. – Jonathan Leffler Aug 19 '17 at 03:13
  • 5
    @Ron: I've created MSO question [How can we stop SO from suggesting both C and C++ tags on questions?](https://meta.stackoverflow.com/questions/355331/how-can-we-stop-so-suggesting-both-c-and-c-tags-on-questions) If you're fed up with having to edit them, please add a vote in support. – Jonathan Leffler Aug 19 '17 at 03:25

1 Answers1

2

Converting comments into an answer.

There is no defined standard way to get at the 'size of the block of allocated memory'. Each implementation has to have a way of knowing the size of each block it allocates, but there's no way for a programmer using the implementation to know the size (in general).

So it is dependent on some number of things, but if all is known, system, architecture, compiler, you're saying there is no resource to find out how things are formatted in memory?

There is no standard (neither de jure nor de facto standard) way to get at the information about the size of a block of memory allocated. All else apart, the size allocated by the library is usually bigger than the size requested (definitely because of the housekeeping data, but even the data portion may be rounded up to a multiple of 8 or a multiple of 16) — should the code report the size requested or the size allocated?

And, as 1201ProgramAlarm noted, one option on open source systems is to look at the C library's implementation of malloc() and free() to see what it does and devise a mechanism to provide the answer to the programmer. However, any such research is specific to that system — different systems will do it differently, in general — and the whole idea runs into a stone wall if the system is a closed source system.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278