Array length

Discussion in 'C Programming' started by Jamil Anwar Zaman, Aug 9, 2003.

  1. If I dynamically allocate memory to a pointer, then is it possible to see
    afterwards what is the memory size the pointer is looking at.
    e.g

    int SIZE = 10;
    int *a = malloc(SIZE*sizeof(int));
    printf("Size : %d", sizeof(a));

    this actually gives me 4 bytes, which essentially is the sizeof the
    pointer. But I want to know the actual memory size. Any idea how can I do
    that.


    --
    Jamil Anwar Zaman
     
    Jamil Anwar Zaman, Aug 9, 2003
    #1
    1. Advertising

  2. On Sun, 10 Aug 2003 02:43:49 +1000
    Jamil Anwar Zaman <> wrote:
    > If I dynamically allocate memory to a pointer, then is it possible to see
    > afterwards what is the memory size the pointer is looking at.
    > e.g
    >
    > int SIZE = 10;
    > int *a = malloc(SIZE*sizeof(int));
    > printf("Size : %d", sizeof(a));
    >
    > this actually gives me 4 bytes, which essentially is the sizeof the
    > pointer. But I want to know the actual memory size. Any idea how can I do
    > that.


    Yes: remember the size of the memory you allocate.

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
     
    Pieter Droogendijk, Aug 9, 2003
    #2
    1. Advertising

  3. Jamil Anwar Zaman

    Serve La Guest

    "Jamil Anwar Zaman" <> wrote in message
    news:eek:...
    > If I dynamically allocate memory to a pointer, then is it possible to see
    > afterwards what is the memory size the pointer is looking at.
    > e.g
    >
    > int SIZE = 10;
    > int *a = malloc(SIZE*sizeof(int));
    > printf("Size : %d", sizeof(a));
    >
    > this actually gives me 4 bytes, which essentially is the sizeof the
    > pointer. But I want to know the actual memory size. Any idea how can I do
    > that.


    This is not possible in standard C. If you want the size you should store it
    yourself.

    One implementation I've seen has a function _msize, but this is of course
    not portable and it's slow for some reason.
     
    Serve La, Aug 9, 2003
    #3
  4. Jamil Anwar Zaman <> writes:

    > If I dynamically allocate memory to a pointer, then is it possible to
    > see afterwards what is the memory size the pointer is looking at.


    No; you need to track it yourself if you need it.

    > e.g
    >
    > int SIZE = 10;
    > int *a = malloc(SIZE*sizeof(int));
    > printf("Size : %d", sizeof(a));
    >
    > this actually gives me 4 bytes, which essentially is the sizeof the
    > pointer.


    Not "essentially." Precisely.

    > But I want to know the actual memory size. Any idea how can I
    > do that.


    The normal method is to carry the size along; I generally use a
    structure that holds a size and either a pointer or an array.
     
    Lowell Gilbert, Aug 9, 2003
    #4
  5. Jamil Anwar Zaman

    Mike Wahler Guest

    Jamil Anwar Zaman <> wrote in message
    news:eek:...
    > If I dynamically allocate memory to a pointer, then is it possible to see
    > afterwards what is the memory size the pointer is looking at.
    > e.g
    >
    > int SIZE = 10;
    > int *a = malloc(SIZE*sizeof(int));
    > printf("Size : %d", sizeof(a));
    >
    > this actually gives me 4 bytes, which essentially is the sizeof the
    > pointer. But I want to know the actual memory size. Any idea how can I do
    > that.


    Yes. The size is SIZE*sizeof(int). Why ask what you
    already know?

    -Mike
     
    Mike Wahler, Aug 10, 2003
    #5
  6. On Sat, 9 Aug 2003 23:30:25 -0700, Mike Wahler <>
    wrote:

    >
    > Jamil Anwar Zaman <> wrote in message
    > news:eek:...
    >> If I dynamically allocate memory to a pointer, then is it possible to
    >> see
    >> afterwards what is the memory size the pointer is looking at.
    >> e.g
    >>
    >> int SIZE = 10;
    >> int *a = malloc(SIZE*sizeof(int));
    >> printf("Size : %d", sizeof(a));
    >>
    >> this actually gives me 4 bytes, which essentially is the sizeof the
    >> pointer. But I want to know the actual memory size. Any idea how can I
    >> do
    >> that.

    >
    > Yes. The size is SIZE*sizeof(int). Why ask what you
    > already know?
    >
    > -Mike
    >


    I was trying not to pass the array size when I pass array as a parameter to
    soem other function. I'm wondering how java actually does that ? Direct
    memory manipulation ?



    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Jamil Anwar Zaman, Sep 3, 2003
    #6
  7. in comp.lang.c i read:
    >On Sat, 9 Aug 2003 23:30:25 -0700, Mike Wahler <>
    >wrote:


    >> Yes. The size is SIZE*sizeof(int). Why ask what you
    >> already know?


    >I was trying not to pass the array size when I pass array as a
    >parameter to soem other function. I'm wondering how java actually does
    >that ? Direct memory manipulation ?


    it passes a reference, which contains (amongst other things) the size. c
    is not java, don't try to apply any of the rules you learned about java to
    c or you will just frustrate yourself.

    --
    a signature
     
    those who know me have no need of my name, Sep 3, 2003
    #7
  8. Jamil Anwar Zaman

    Eric Sosman Guest

    Jamil Anwar Zaman wrote:
    >
    > > Jamil Anwar Zaman <> wrote in message
    > > news:eek:...
    > >> If I dynamically allocate memory to a pointer, then is it possible to
    > >> see
    > >> afterwards what is the memory size the pointer is looking at.

    > [...]
    >
    > I was trying not to pass the array size when I pass array as a parameter to
    > soem other function. I'm wondering how java actually does that ? Direct
    > memory manipulation ?


    <off-topic>

    An array in Java is a full-fledged Object. In addition
    to the "contents" portion, the Java array Object also has a
    `length' member that gives the number of array elements.
    In short, Java passes the array size along with the array;
    they're just bundled together conveniently.

    </off-topic>

    A way to imitate Java's "packaging" of the array contents
    with the array size is to use a struct, e.g.

    struct array_of_int {
    size_t element_count;
    int *elements;
    };

    It's still not quite as convenient as Java's formulation
    because you've got to maintain both the count and the pointer,
    and because the struct's presence is so obtrusive. Still,
    this sort of "descriptor" style can be useful.

    --
     
    Eric Sosman, Sep 3, 2003
    #8
    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. Mitchua
    Replies:
    5
    Views:
    2,745
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,963
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. Sam
    Replies:
    3
    Views:
    14,109
    Karl Seguin
    Feb 17, 2005
  4. Tom
    Replies:
    3
    Views:
    213
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    476
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page