size of an array without sizeof operator

Discussion in 'C Programming' started by subramanian100in@yahoo.com, India, Mar 5, 2007.

  1. , India

    , India Guest

    Is it possible to measure the size of an array without using the
    sizeof operator ?
    , India, Mar 5, 2007
    #1
    1. Advertising

  2. , India

    Ian Collins Guest

    , India wrote:
    > Is it possible to measure the size of an array without using the
    > sizeof operator ?
    >

    Why would you want to? That's what sizeof is for.

    --
    Ian Collins.
    Ian Collins, Mar 5, 2007
    #2
    1. Advertising

  3. , India

    santosh Guest

    , India wrote:
    > Is it possible to measure the size of an array without using the
    > sizeof operator ?


    Not portably. sizeof is the standard for a reason, why do you want to
    jump through hoops to simulate it imperfectly?
    santosh, Mar 5, 2007
    #3
  4. , India

    Ben Pfaff Guest

    ", India" <>
    writes:
    > Is it possible to measure the size of an array without using the
    > sizeof operator ?


    The answer is the same as it has been in every single thread
    that brings up this stupid question. Do some research.
    --
    "Am I missing something?"
    --Dan Pop
    Ben Pfaff, Mar 5, 2007
    #4
  5. On Mon, 05 Mar 2007 14:28:20 +1300, Ian Collins wrote:

    > , India wrote:
    >> Is it possible to measure the size of an array without using the
    >> sizeof operator ?
    >>

    > Why would you want to? That's what sizeof is for.
    >


    To please your professor?

    I was going to say "To pass a class". But that would be rather ambiguous ;)
    William Ahern, Mar 5, 2007
    #5
  6. , India

    , India Guest

    I saw this question in one of the earlier posts but the answer was not
    given.

    I am learning C from home. I have a PC at home. I am reading K & R
    second edition. I post my doubts in this forum to get clarified. I am
    preparing for interview. That is why I am asking.

    I thought of doing it in one way without using sizeof. But it ASSUMES
    that sizeof pointer should not be bigger than maximum value of
    unsigned long or unsigned long long. Before writing the code, let me
    explain it in plain English because, in this forum I wil get to know
    if the assumption is correct.

    In the following, assume that size, str, etc exist.

    Suppose I have, for some specific Type,
    Type a[size];

    I calculate

    sprintf(str1, "%p", (void *)(a+1));
    sprintf(str2, "%p", (void *)a);

    Then I have my own routine to convert str1 and str2 to unsigned long
    or unsigned long long based on the ASSUMPTION mentioned above. I find
    the difference of these converted numbers and then multiply the
    difference by size.

    This is what strikes me.

    Correct me for all errors.
    , India, Mar 5, 2007
    #6
  7. , India

    , India Guest

    I saw this question in one of the earlier posts but the answer was not
    given.

    I am learning C from home. I have a PC at home. I am reading
    K & R second edition. I post my doubts in this forum to get clarified.
    I am preparing for interview. That is why I am asking.

    I thought of doing it in one way without using sizeof. But it ASSUMES
    that sizeof pointer should not be bigger than maximum value of
    unsigned long or unsigned long long. Before writing the code, let me
    explain it in plain English because, in this forum I wil get to know
    if the assumption is correct.

    In the following, assume that size, str, etc exist.


    Suppose I have, for some specific Type,
    Type a[size];


    I calculate


    sprintf(str1, "%p", (void *)(a+1));
    sprintf(str2, "%p", (void *)a);


    Then I have my own routine to convert str1 and str2 to unsigned long
    or unsigned long long based on the ASSUMPTION mentioned above. I find
    the difference of these converted numbers and then multiply the
    difference by size.

    This is what strikes me.

    Correct me for all errors.
    , India, Mar 5, 2007
    #7
  8. , India

    Flash Gordon Guest

    , India wrote, On 05/03/07 01:47:
    > I saw this question in one of the earlier posts but the answer was not
    > given.


    <snip>

    How can you have seen the question but not seen the answer that it
    always given?

    Also, please quote just enough of the post you are replying to to
    provide context. There is no guarantee that the person reading your post
    will have seen (or will ever see) the post you are replying to.
    --
    Flash Gordon
    Flash Gordon, Mar 5, 2007
    #8
  9. , India

    CBFalconer Guest

    ", India" wrote:
    >

    .... snip ...
    >
    > Suppose I have, for some specific Type,
    > Type a[size];
    >
    > I calculate
    >
    > sprintf(str1, "%p", (void *)(a+1));
    > sprintf(str2, "%p", (void *)a);
    >

    .... snip ...
    >
    > Correct me for all errors.


    Why should the result of the sprintf be meaningful as an integer?
    Maybe the output of sprintf is "Go to schoolhouse, room C. Select
    small boy third from left in front row. Ask him where that data is
    stored. Give him a slate on which to write his answer."

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews
    CBFalconer, Mar 5, 2007
    #9
  10. <> wrote in message
    news:...
    >I saw this question in one of the earlier posts but the answer was not
    > given.
    >
    > I am learning C from home. I have a PC at home. I am reading
    > K & R second edition. I post my doubts in this forum to get clarified.
    > I am preparing for interview. That is why I am asking.
    >
    > I thought of doing it in one way without using sizeof. But it
    > ASSUMES that sizeof pointer should not be bigger than maximum
    > value of unsigned long or unsigned long long. Before writing the
    > code, let me explain it in plain English because, in this forum I wil
    > get to know if the assumption is correct.
    >
    > In the following, assume that size, str, etc exist.
    >
    > Suppose I have, for some specific Type,
    > Type a[size];
    >
    > I calculate
    >
    > sprintf(str1, "%p", (void *)(a+1));
    > sprintf(str2, "%p", (void *)a);
    >
    > Then I have my own routine to convert str1 and str2 to unsigned
    > long or unsigned long long based on the ASSUMPTION mentioned
    > above. I find the difference of these converted numbers and then
    > multiply the difference by size.
    >
    > This is what strikes me.
    >
    > Correct me for all errors.


    "(char*)(a+1)-(char*)(a)" will yield the same result as sizeof(Type).
    Multiply by "size" and you will get the size of the total array. No need to
    jump through any further hoops like you describe.

    Usually when people ask this question, they don't know "size", or they're
    trying to reimplement the sizeof() function in general. Neither of those
    problems can be solved portably; what you're asking is a more specific case
    that can.

    S

    --
    Stephen Sprunk "Those people who think they know everything
    CCIE #3723 are a great annoyance to those of us who do."
    K5SSS --Isaac Asimov



    --
    Posted via a free Usenet account from http://www.teranews.com
    Stephen Sprunk, Mar 5, 2007
    #10
  11. On Sun, 04 Mar 2007 18:40:49 -0800, , India
    wrote:
    <snip>
    > Suppose I have, for some specific Type,
    > Type a[size];
    >
    >
    > I calculate
    >
    >
    > sprintf(str1, "%p", (void *)(a+1));
    > sprintf(str2, "%p", (void *)a);
    >
    >
    > Then I have my own routine to convert str1 and str2 to unsigned long
    > or unsigned long long based on the ASSUMPTION mentioned above. I find
    > the difference of these converted numbers and then multiply the
    > difference by size.
    >
    > This is what strikes me.
    >
    > Correct me for all errors.


    That sounds like a roundabout way of simply doing:

    ptrdiff_t sz = (char *)(a + 1) - (char *)a;

    Your sprintf() method isn't viable because the format of the %p
    specifier is unspecified. It's too late for me to figure out whether the
    cast-and-subtract is well defined.

    Also, your original post was ambiguous. I had originally figured that
    the length of the array was also an unknown.
    William Ahern, Mar 5, 2007
    #11
  12. , India

    santosh Guest

    , India wrote:
    [ ... ]
    > I thought of doing it in one way without using sizeof. But it ASSUMES
    > that sizeof pointer should not be bigger than maximum value of
    > unsigned long or unsigned long long. Before writing the code, let me
    > explain it in plain English because, in this forum I wil get to know
    > if the assumption is correct.
    >
    > In the following, assume that size, str, etc exist.
    >
    > Suppose I have, for some specific Type,
    > Type a[size];
    >
    > I calculate
    >
    > sprintf(str1, "%p", (void *)(a+1));
    > sprintf(str2, "%p", (void *)a);
    >
    > Then I have my own routine to convert str1 and str2 to unsigned long
    > or unsigned long long based on the ASSUMPTION mentioned above. I find
    > the difference of these converted numbers and then multiply the
    > difference by size.
    >
    > This is what strikes me.


    This won't do what you want.
    santosh, Mar 5, 2007
    #12
  13. , India

    Guest

    On Mar 5, 6:26 am, ", India"
    <> wrote:
    > Is it possible to measure the size of an arraywithoutusingthesizeofoperator ?


    Yes, its possible. I am explaining it below with an example.

    int a[10];

    printf("total size occupied by the array is %d\n", ( ((&a+1)-&a) *
    ((char*)(a+1)-(char*)a) );
    this will yield the same size as sizeof(a) yeilds.

    Is that the answer that you are expecting?..

    Regards
    Jamsheed M()
    , Mar 27, 2007
    #13
  14. said:

    > On Mar 5, 6:26 am, ", India"
    > <> wrote:
    >> Is it possible to measure the size of an
    >> arraywithoutusingthesizeofoperator ?

    >
    > Yes, its possible.


    Albeit silly.

    > I am explaining it below with an example.
    >
    > int a[10];
    >
    > printf("total size occupied by the array is %d\n", ( ((&a+1)-&a) *
    > ((char*)(a+1)-(char*)a) );
    > this will yield the same size as sizeof(a) yeilds.


    Well, it's hard to see how, since it won't actually compile. (Check your
    parentheses.)

    When I fix that, and add a printf of the result of sizeof, I get the
    following code:

    #include <stdio.h>

    int main(void)
    {
    int a[10];

    printf("total size occupied by the array is %d\n",
    (((&a + 1) - &a) * ((char *)(a + 1) - (char *)a)));

    printf("sizeof a = %d\n", (int)sizeof a);
    return 0;
    }

    When I run this code, I get the following output:

    total size occupied by the array is 4
    sizeof a = 40

    > Is that the answer that you are expecting?..


    Sure as eggs as eggs it isn't the answer *you* were expecting!

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Mar 27, 2007
    #14
  15. , India

    Guest

    On Mar 27, 1:59 pm, Richard Heathfield <> wrote:
    > said:
    >
    > > On Mar 5, 6:26 am, ", India"
    > > <> wrote:
    > >> Is it possible to measure the size of an
    > >> arraywithoutusingthesizeofoperator ?

    >
    > > Yes, its possible.

    >
    > Albeit silly.
    >
    > > I am explaining it below with an example.

    >
    > > int a[10];

    >
    > > printf("total size occupied by the array is %d\n", ( ((&a+1)-&a) *
    > > ((char*)(a+1)-(char*)a) );
    > > this will yield the same size as sizeof(a) yeilds.

    >
    > Well, it's hard to see how, since it won't actually compile. (Check your
    > parentheses.)
    >
    > When I fix that, and add a printf of the result of sizeof, I get the
    > following code:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int a[10];
    >
    > printf("total size occupied by the array is %d\n",
    > (((&a + 1) - &a) * ((char *)(a + 1) - (char *)a)));
    >
    > printf("sizeof a = %d\n", (int)sizeof a);
    > return 0;
    >
    > }
    >
    > When I run this code, I get the following output:
    >
    > total size occupied by the array is 4
    > sizeof a = 40
    >
    > > Is that the answer that you are expecting?..

    >
    > Sure as eggs as eggs it isn't the answer *you* were expecting!
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
    > email: rjh at the above domain, - www.


    Yes you are right.

    the code need to be changed like this.

    int main()
    {
    int a[10];
    printf("total size =%d\n",(char*)(&a+1)-(char*)(&a));
    printf("sizeof value=%d\n",sizeof(a));
    }


    this both will print the same size 40.
    , Mar 27, 2007
    #15
  16. said:

    <snip>

    > Yes you are right.
    >
    > the code need to be changed like this.
    >
    > int main()
    > {
    > int a[10];
    > printf("total size =%d\n",(char*)(&a+1)-(char*)(&a));
    > printf("sizeof value=%d\n",sizeof(a));
    > }


    No, the code needs to be removed to take away the silly array
    calculation that takes far longer to type than sizeof. If you want to
    know the size of an array, use sizeof. That's what it's *for* - finding
    out the sizes of things (and types of things).

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Mar 27, 2007
    #16
    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. Manish_Ganvir
    Replies:
    13
    Views:
    1,559
    Keith Thompson
    Feb 14, 2005
  2. Robotnik

    Size of Stucture without sizeof()

    Robotnik, Dec 1, 2005, in forum: C Programming
    Replies:
    55
    Views:
    985
    Keith Thompson
    Dec 7, 2005
  3. Kislay

    The sizeof operator : sizeof(++i)

    Kislay, Oct 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    674
    Peter Pichler
    Oct 19, 2007
  4. srikar2097

    size of a struct without "sizeof"

    srikar2097, Dec 15, 2008, in forum: C Programming
    Replies:
    25
    Views:
    905
    Keith Thompson
    Dec 15, 2008
  5. Replies:
    46
    Views:
    631
    Shao Miller
    Jan 14, 2013
Loading...

Share This Page