malloc size

M

madireddy

Hi,

Inside a program how do i find out what size has been allocated to
pointer using malloc. eg...

int *p;
p=(int*)malloc(1024*sizeof(int));
now how do find how much has been allocated to p?

mojozoox!
 
D

Derrick Coetzee

Inside a program how do i find out what size has been allocated to
pointer using malloc. eg...
int *p=(int*)malloc(1024*sizeof(int));
now how do find how much has been allocated to p?

If malloc succeeds, the result will have the size you asked for (in this
case, 1024*sizeof(int)). If you're passing pointers around and don't
know how much was malloc'ed, you simply have to track that information
yourself along with the pointer. This may seem silly, since the
underlying memory allocation system almost always needs to track this
information itself, but there are conceivably allocation systems that
would keep it only implicitly and make it hard to obtain.
 
A

Anitha Adusumilli

Hi

AFAIK, its not possible to find the size of array allocated by malloc.
You provide this value to malloc...so, you are the one who has to keep
track of it.
usually, a constant say "#define ARRAYSIZE 1024" is used and this
constant is used in malloc and anywhere else the array size needs to be
found

HTH
Anitha
 
E

E. Robert Tisdale

How do I find out inside a program
what size has been allocated to pointer using malloc. e.g.:

int *p=(int*)malloc(1024*sizeof(int));

Now how do find how much has been allocated to p?

It's implementation dependent.
> cat main.c
#include <stdio.h>
#include <stdlib.h>

size_t allocation(const void* p) {
return ((size_t*)p)[-1] - 1;
}

int main(int argc, char* argv[]) {
if (1 < argc) {
const size_t n = atoi(argv[1]);
const void* p = malloc(n);
fprintf(stdout, "size = %u\n", allocation(p));
free((void*)p);
}
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main 1
size = 16
 
B

Barry Schwarz

Hi,

Inside a program how do i find out what size has been allocated to
pointer using malloc. eg...

int *p;
p=(int*)malloc(1024*sizeof(int));
now how do find how much has been allocated to p?
If you don't keep track of it yourself, there is no portable way.


<<Remove the del for email>>
 
A

Anitha Adusumilli

Hi

AFAIK, its not possible to find the size of array allocated by malloc.
You provide this value to malloc...so, you are the one who has to keep
track of it.
usually, a constant say "#define ARRAYSIZE 1024" is used and this
constant is used in malloc and anywhere else the array size needs to be
found

HTH
Anitha
 
M

Mike Wahler

Hi,

Inside a program how do i find out what size has been allocated to
pointer using malloc. eg...

int *p;
p=(int*)malloc(1024*sizeof(int));
now how do find how much has been allocated to p?

You already know. 1024 * sizeof(int) (if malloc()
does not return NULL, in which case it's zero).

-Mike
 
I

infobahn

Anitha said:
Hi

AFAIK, its not possible to find the size of array allocated by malloc.
Right.

You provide this value to malloc...so, you are the one who has to keep
track of it.
Right.

usually, a constant say "#define ARRAYSIZE 1024" is used and this
constant is used in malloc and anywhere else the array size needs to be
found

Wrong. This defeats the point of using malloc in the first place.

If you know you need ARRAYSIZE Ts, just use T arr[ARRAYSIZE]. malloc
is useful when you don't know at compile time how many objects you
will need. All the more reason to keep track.
 
I

infobahn

E. Robert Tisdale said:
It's implementation dependent.

But not implementation-defined. And it's not always possible.
cat main.c
#include <stdio.h>
#include <stdlib.h>

size_t allocation(const void* p) {
return ((size_t*)p)[-1] - 1;

6.5.6 (8): "If both the pointer operand and the result point to
elements of the same array object, or one past the last element
of the array object, the evaluation shall not produce an overflow;
otherwise, the behavior is undefined."

Consequently, the behaviour of this code is undefined.
 
K

Keith Thompson

E. Robert Tisdale said:
It's implementation dependent.

Yes. You might as well have stopped there.
cat main.c [Non-portable code snipped]
gcc -Wall -std=c99 -pedantic -o main main.c
./main 1
size = 16

The provided code is useless other than to demonstrate how one
implementation happens to behave. Incidentally, the behavior shown
has nothing to do with gcc; it's a characteristic of the C runtime
library, not of the compiler.
 
C

Chris Croughton

Wrong. This defeats the point of using malloc in the first place.

What is 'the' purpose of using malloc? Hint: it isn't just to make
variable sized arrays. How about linked lists, where the size of each
element is known (and may include an array, for instance)?
If you know you need ARRAYSIZE Ts, just use T arr[ARRAYSIZE]. malloc
is useful when you don't know at compile time how many objects you
will need. All the more reason to keep track.

You can try allocating a large object on the stack, but it will fail on
many systems, and having it static may well waste valuable data space.
Using malloc is more usual for that.

Chris C
 
I

infobahn

Chris said:
What is 'the' purpose of using malloc?

In the current context (i.e. the context quoted above, and in terms
of which my answer should be read), it's to allocate space for an
array. In other contexts, it can have other purposes (although one
could reasonably argue that malloc always makes space for an array,
even if it's an array only one item in size!).
Hint: it isn't just to make
variable sized arrays. How about linked lists, where the size of each
element is known (and may include an array, for instance)?

I do not dispute that such a useful tool can solve other tasks too. :)
 
M

Micah Cowan

infobahn said:
In the current context (i.e. the context quoted above, and in terms
of which my answer should be read), it's to allocate space for an
array. In other contexts, it can have other purposes (although one
could reasonably argue that malloc always makes space for an array,
even if it's an array only one item in size!).

Who says he didn't interpret it in that context? But nobody said
we were talking about dynamically sized arrays, so his correction
remains, that a compile-time constant is a valid answer. However,
I disagree with Anitha's "usually" part.
 
A

Andrey Tarasevich

infobahn said:
...
usually, a constant say "#define ARRAYSIZE 1024" is used and this
constant is used in malloc and anywhere else the array size needs to be
found

Wrong. This defeats the point of using malloc in the first place.

If you know you need ARRAYSIZE Ts, just use T arr[ARRAYSIZE]. malloc
is useful when you don't know at compile time how many objects you
will need. All the more reason to keep track.

Wrong. There at least two situations when 'malloc' might be used with
constant size argument

1. A need for manual control of the object's storage duration (in this
case it is an array).

Your suggestion to use 'T arr[ARRAYSIZE]' won't work simply because the
lifetime of, say, automatic 'arr's would automatically end at the end of
the block. That might be not what's needed.

2. When there's a good reason to believe that the object's size is to
large for, say, automatic memory
 
I

infobahn

Andrey said:
...
usually, a constant say "#define ARRAYSIZE 1024" is used and this
constant is used in malloc and anywhere else the array size needs to be
found

Wrong. This defeats the point of using malloc in the first place.

If you know you need ARRAYSIZE Ts, just use T arr[ARRAYSIZE]. malloc
is useful when you don't know at compile time how many objects you
will need. All the more reason to keep track.
Wrong.


There at least two situations when 'malloc' might be used with
constant size argument

1. A need for manual control of the object's storage duration (in this
case it is an array).

Your suggestion to use 'T arr[ARRAYSIZE]' won't work simply because the
lifetime of, say, automatic 'arr's would automatically end at the end of
the block. That might be not what's needed.

2. When there's a good reason to believe that the object's size is to
large for, say, automatic memory

You forgot dynamic data structures, and initialisation of dynamic
vectors to a sensible chunk size, to name but two.
 
A

Anitha Adusumilli

From what the person who originally posted asked, I felt this int array
is used for returning value in a funtion. You cant return
"arr",declared as T arr[ARRAYSIZE], because it is a local
variable..allocated on stack. By using malloc for arrays, u can return
that array and the caller can later deallocate it by "free". Hope my
last pt is clear now.


Anitha
 
A

Andrey Tarasevich

infobahn said:
If you know you need ARRAYSIZE Ts, just use T arr[ARRAYSIZE]. malloc
is useful when you don't know at compile time how many objects you
will need. All the more reason to keep track.

Wrong.

<sigh> /me must remember there ain't no such animal as context in
Usenet.
Huh?
There at least two situations when 'malloc' might be used with
constant size argument

1. A need for manual control of the object's storage duration (in this
case it is an array).

Your suggestion to use 'T arr[ARRAYSIZE]' won't work simply because the
lifetime of, say, automatic 'arr's would automatically end at the end of
the block. That might be not what's needed.

2. When there's a good reason to believe that the object's size is to
large for, say, automatic memory

You forgot dynamic data structures, and initialisation of dynamic
vectors to a sensible chunk size, to name but two.

"Dynamic data structures" is quite a generic term. I don't understand
what exactly do you mean by this.

In any case, I didn't try to come up with a complete list of such
situations. It's just a couple of examples.
 
H

hotadvice

Hi,

Inside a program how do i find out what size has been allocated to
pointer using malloc. eg...

int *p;
p=(int*)malloc(1024*sizeof(int));
now how do find how much has been allocated to p?

mojozoox!


well , i think if malloc has not returned 0 ie failure
then the size allocated is same as that was demanded.

by the way what will sizeof operator applied on p will return.
i think it is the size allocated to pointer p .if that is true
u get ur answer

goodluck
 
I

infobahn

hotadvice said:
well , i think if malloc has not returned 0 ie failure
then the size allocated is same as that was demanded.

At least what was demanded, actually. And of course what you
asked for is all you can rely on having (if, as you say, it
didn't fail).
by the way what will sizeof operator applied on p will return.
i think it is the size allocated to pointer p .if that is true
u get ur answer

sizeof p yields the size of the pointer, not the size of the
thing pointed to.

sizeof *p works if you only asked for space for one object
of that size (and provided p isn't a void *, obviously!).
 
H

hotadvice

so finally
1.not returning 0 implies at least the size that was asked is allocated
2.sizeof(*p) gives u th e size

so shall we say pack up
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top