Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc size

Reply
Thread Tools

malloc size

 
 
madireddy@HotPop.com
Guest
Posts: n/a
 
      01-06-2005
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!

 
Reply With Quote
 
 
 
 
Derrick Coetzee
Guest
Posts: n/a
 
      01-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
 
Reply With Quote
 
 
 
 
Anitha Adusumilli
Guest
Posts: n/a
 
      01-06-2005
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-Mail Removed) wrote:
> 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!


 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-06-2005
(E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      01-06-2005
On 5 Jan 2005 19:29:18 -0800, (E-Mail Removed) wrote:

>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>>
 
Reply With Quote
 
Anitha Adusumilli
Guest
Posts: n/a
 
      01-06-2005
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-Mail Removed) wrote:
> 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!


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-06-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
infobahn
Guest
Posts: n/a
 
      01-06-2005
Anitha Adusumilli wrote:
>
> 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.
 
Reply With Quote
 
infobahn
Guest
Posts: n/a
 
      01-06-2005
"E. Robert Tisdale" wrote:
>
> (E-Mail Removed) wrote:
>
> > How do I find out inside a program
> > what size has been allocated to pointer using malloc. e.g.:
> >

> 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 (: "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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-06-2005
"E. Robert Tisdale" <(E-Mail Removed)> writes:
> (E-Mail Removed) wrote:
>
>> 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.


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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments