If the OP uses a GNU based platform and for some versions of the
GNU libc. But if it's available it doesn't tell him/her how many
elements have been allocated for the array(and it doesn't help
at all if it's not a dynamically allocated array) but only how
much memory was set aside for the array, which very well could
be more than was originallu asked for. So it's absolutely use-
less for what the OP is looking for.
How do you know? The OP's specification was vague, and it might or might
not be what he was looking for. At least it's a starting point.
The OP said he had a pointer returned by malloc, but didn't have the
size he'd passed to malloc() to obtain it to hand. The only portable
advice is: don't forget the size you passed to malloc(). Of course
that's good advice, and it's what I usually do myself. But sometimes
there are good reasons for seeking another solution.
Here's an example. Suppose we have a function like this:
struct node *array_copy(struct node *dst, struct node *src);
where the arrays are terminated by a sentinel value (e.g.
last_node->data == NULL). We might want (in a debugging phase - allow
for the moment that a small number of programmers outside the clc Clique
actually do have to debug their code) to bounds-check writes to the dst
list. Or we might want to allow our function to grow the dst list
dynamically if necessary, and if so return the realloc()d pointer (or
NULL). In each of these cases, malloc_usable_size will be just perfect.
On the other hand, if the OP is hoping to take a function
double compute_mean(double *x, size_t nelts);
and replace it with one like
double compute_mean(double *x);
and calculate the size of the array at runtime, then yes, he'll be
disappointed (I imagine on every platform - it's not in malloc()'s
interest to record the memory asked for, only the memory actually
allocated). But he would surely realize this when he read the
documentation of the function I suggested might be useful.
Maybe and if the OP uses a Microsoft system. Why can't you stop
posting stuff that is wrong or maybe wrong or, if you're lucky,
is correct instead of pointing the people asking not really C
related questions to the groups where the questions can be really
answered? You do not help them that way. In the right newsgroup
they will find enough people that know the correct answers and
enough people that can intervene if wrong answers are given.
There are plenty of people here that can intervene if wrong answers are
given, but apart from Jacob every man Jack of them would rather play
their silly topicality oneupmanship games than help new posters. I made
it crystal clear that I'm not sure about the exact function of _msize,
but I'm 100% sure that using that as a search term will lead the OP to
useful information if he's using the Windows platform. Why is pointing
people to other newsgroups any better than pointing them to Google with
a useful search term?