malloc size

Discussion in 'C Programming' started by madireddy@HotPop.com, Jan 6, 2005.

  1. Guest

    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!
    , Jan 6, 2005
    #1
    1. Advertising

  2. 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.
    Derrick Coetzee, Jan 6, 2005
    #2
    1. Advertising

  3. 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

    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!
    Anitha Adusumilli, Jan 6, 2005
    #3
  4. 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
    E. Robert Tisdale, Jan 6, 2005
    #4
  5. On 5 Jan 2005 19:29:18 -0800, 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>>
    Barry Schwarz, Jan 6, 2005
    #5
  6. 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

    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!
    Anitha Adusumilli, Jan 6, 2005
    #6
  7. Mike Wahler Guest

    <> wrote in message
    news:...
    > 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
    Mike Wahler, Jan 6, 2005
    #7
  8. infobahn Guest

    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.
    infobahn, Jan 6, 2005
    #8
  9. infobahn Guest

    "E. Robert Tisdale" wrote:
    >
    > 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 (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.
    infobahn, Jan 6, 2005
    #9
  10. "E. Robert Tisdale" <> writes:
    > 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) <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.
    Keith Thompson, Jan 6, 2005
    #10
  11. On Thu, 6 Jan 2005 07:40:28 +0000 (UTC), infobahn
    <> wrote:

    > Anitha Adusumilli wrote:
    >>
    >> 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.


    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
    Chris Croughton, Jan 6, 2005
    #11
  12. infobahn Guest

    Chris Croughton wrote:
    >
    > On Thu, 6 Jan 2005 07:40:28 +0000 (UTC), infobahn
    > <> wrote:
    >
    > > Anitha Adusumilli wrote:
    > >>
    > >> 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.

    >
    > 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. :)
    infobahn, Jan 6, 2005
    #12
  13. Micah Cowan Guest

    infobahn wrote:

    > Chris Croughton wrote:
    >
    >>On Thu, 6 Jan 2005 07:40:28 +0000 (UTC), infobahn
    >> <> wrote:
    >>
    >>
    >>>Anitha Adusumilli wrote:
    >>>
    >>>>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.

    >>
    >>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!).


    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.
    Micah Cowan, Jan 6, 2005
    #13
  14. infobahn wrote:
    > ...
    >> 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

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 6, 2005
    #14
  15. infobahn Guest

    Andrey Tarasevich wrote:
    >
    > infobahn wrote:
    > > ...
    > >> 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.


    <sigh> /me must remember there ain't no such animal as context in
    Usenet.

    > 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.
    infobahn, Jan 7, 2005
    #15
  16. >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
    Anitha Adusumilli, Jan 7, 2005
    #16
  17. infobahn wrote:
    >> > 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.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 7, 2005
    #17
  18. hotadvice Guest

    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!



    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
    hotadvice, Jan 7, 2005
    #18
  19. infobahn Guest

    hotadvice wrote:
    >
    > 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!

    >
    > 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!).
    infobahn, Jan 7, 2005
    #19
  20. hotadvice Guest

    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
    hotadvice, Jan 7, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    684
  2. ravi
    Replies:
    0
    Views:
    439
  3. Peter
    Replies:
    34
    Views:
    1,916
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    467
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    311
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page