how to determine the size of memory pointed by a int pointer?

Discussion in 'C Programming' started by Paul, Nov 29, 2007.

  1. Paul

    Paul Guest

    hi, there,

    I was asked such a question: how to determine the size of memory of
    the int
    pointer pointed? for example

    int GetTheSizeofMemory(int *buffer)
    {
    int size;
    //enter your code here, to calculate the memory size of buffer
    point to.
    return size;
    }
    we can not use sizeof(buffer) to get the value, how should we do?
    thanks.

    paul
    Paul, Nov 29, 2007
    #1
    1. Advertising

  2. Paul

    Ian Collins Guest

    Paul wrote:
    > hi, there,
    >
    > I was asked such a question: how to determine the size of memory of
    > the int
    > pointer pointed? for example
    >
    > int GetTheSizeofMemory(int *buffer)
    > {
    > int size;
    > //enter your code here, to calculate the memory size of buffer
    > point to.
    > return size;
    > }
    > we can not use sizeof(buffer) to get the value, how should we do?
    > thanks.
    >

    By remembering the size when the buffer is allocated. There is no other
    portable way.

    --
    Ian Collins.
    Ian Collins, Nov 29, 2007
    #2
    1. Advertising

  3. Paul

    cr88192 Guest

    "Paul" <> wrote in message
    news:...
    > hi, there,
    >
    > I was asked such a question: how to determine the size of memory of
    > the int
    > pointer pointed? for example
    >
    > int GetTheSizeofMemory(int *buffer)
    > {
    > int size;
    > //enter your code here, to calculate the memory size of buffer
    > point to.
    > return size;
    > }
    > we can not use sizeof(buffer) to get the value, how should we do?
    > thanks.
    >


    you know or you don't know.
    within the standard, nothing is provided for this.

    however, if you implement your own heap (or something similar), you can also
    provide your own means of determining object sizes...


    > paul
    cr88192, Nov 29, 2007
    #3
  4. Paul

    santosh Guest

    Paul wrote:

    > hi, there,
    >
    > I was asked such a question: how to determine the size of memory of
    > the int pointer pointed?


    By storing the size you specified when initialising the pointer to a
    block of memory.

    > for example
    >
    > int GetTheSizeofMemory(int *buffer)
    > {
    > int size;
    > //enter your code here, to calculate the memory size of buffer
    > point to.
    > return size;
    > }
    > we can not use sizeof(buffer) to get the value, how should we do?
    > thanks.


    Some platforms have special routines for this purpose but from a
    Standard C point of view there is no other way than to manually
    remember the size.

    You can also implement a wrapper on top of *alloc() that maintains a
    table of pointer values and their associated size requests and provide
    a querying function to retrieve the data associated with a valid
    pointer value.
    santosh, Nov 29, 2007
    #4
  5. Paul

    Remo D. Guest

    Paul ha scritto:
    > I was asked such a question: how to determine the size of memory of
    > the int pointer pointed? for example
    >
    > int GetTheSizeofMemory(int *buffer)
    > ...
    > we can not use sizeof(buffer) to get the value, how should we do?


    As others have already answered, you can't and one option is to write
    your own malloc/free functions.

    I've never really done it, but I've always thought that, in case I need
    them, I would have implemented something like this:

    void *mymalloc(size_t sz)
    {
    size_t *p;

    p = malloc(sz + sizeof(size_t));
    if (p) {
    *p = sz;
    return p + 1;
    }
    return NULL;
    }

    void myfree(void *p)
    {
    free(((size_t *)p)-1);
    }

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

    I can imagine all weird things happening if pointers obtained with
    malloc get passed to these functions, but avoiding to pass invalid
    pointer to functions is among the first things a C programmer learns.

    I understood that some malloc() implementation behaves this way, storing
    the relevant information just before the pointer returned.

    What I've never investigated is if there are issues with alignment,
    portability or any other drawback I might have missed.

    Remo.D
    Remo D., Nov 29, 2007
    #5
  6. "Remo D." wrote:
    [... keep track of malloc'ed size ...]
    > I've never really done it, but I've always thought that, in case I need
    > them, I would have implemented something like this:
    >
    > void *mymalloc(size_t sz)
    > {
    > size_t *p;
    >
    > p = malloc(sz + sizeof(size_t));
    > if (p) {
    > *p = sz;
    > return p + 1;
    > }
    > return NULL;
    > }

    [...]
    > What I've never investigated is if there are issues with alignment,
    > portability or any other drawback I might have missed.


    Anything that has a stricter alignment requirement than size_t
    will cause this to fail. Consider a not-too-unlikely scenario
    where size_t has 4-byte alignment and double has 8-byte alignment.
    The following will do "bad things"[tm]:

    double *mypt = mymalloc(10 * sizeof(*mypt));
    if ( mypt != NULL )
    mypt[0] = 1.23;

    You need to know the strictest alignment requirement, and use that
    in your wrapper. And there is no portable way to know what is the
    strictest alignment requirement. The closest you could do would
    be create a union containing every type that you might malloc, and
    hope you never call it for something stricter. Or, you could make
    a #define in a header file and document "make sure you set this
    properly, or else."

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Nov 29, 2007
    #6
  7. Paul

    Remo D. Guest

    Kenneth Brody ha scritto:
    > "Remo D." wrote:
    > [... keep track of malloc'ed size ...]
    >> I've never really done it, but I've always thought that, in case I need
    >> them, I would have implemented something like this:
    >>

    > [...]
    >> What I've never investigated is if there are issues with alignment,
    >> portability or any other drawback I might have missed.

    >
    > Anything that has a stricter alignment requirement than size_t
    > will cause this to fail. Consider a not-too-unlikely scenario
    > where size_t has 4-byte alignment and double has 8-byte alignment.


    I see! Thanks, I'll keep it in mind should I use somthing similar.

    Remo.D
    Remo D., Nov 29, 2007
    #7
    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. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Frodo Baggins
    Replies:
    12
    Views:
    654
    dbtid
    Jan 3, 2007
  3. Winsk

    Size of memory Pointed-to

    Winsk, Oct 7, 2007, in forum: C Programming
    Replies:
    4
    Views:
    381
    Keith Thompson
    Oct 8, 2007
  4. Disc Magnet
    Replies:
    1
    Views:
    624
    Ian Collins
    May 6, 2010
  5. tahir rauf
    Replies:
    12
    Views:
    730
    Keith Thompson
    Apr 30, 2011
Loading...

Share This Page