returning array size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • theAnswer

    returning array size

    hello,
    Is there a way to implement a little function to return the size of an array
    (not a string)?

    Something like int size(int *array)

    thanx


  • Joona I Palaste

    #2
    Re: returning array size

    theAnswer <i.got@what.it. takes> scribbled the following:[color=blue]
    > hello,
    > Is there a way to implement a little function to return the size of an array
    > (not a string)?[/color]
    [color=blue]
    > Something like int size(int *array)[/color]

    If the array is passed as an int * parameter, such a function is
    impossible. However, if its declaration is in scope, then sizeof array
    will return the array's size in bytes.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Nothing lasts forever - so why not destroy it now?"
    - Quake

    Comment

    • Derk Gwen

      #3
      Re: returning array size

      "theAnswer" <i.got@what.it. takes> wrote:
      # hello,
      # Is there a way to implement a little function to return the size of an array
      # (not a string)?
      #
      # Something like int size(int *array)

      The language does not define any way of extracting the allocated size
      from a pointer to the first (or other) element. While an implementation
      might (for example if pointer are segregated to different pages based on
      allocation size), the portable solutions are to pass the length around
      with the pointer or use a sentinel vector element.

      --
      Derk Gwen http://derkgwen.250free.com/html/index.html
      Why are we here?
      whrp

      Comment

      • Martin Ambuhl

        #4
        Re: returning array size

        theAnswer wrote:
        [color=blue]
        > hello,
        > Is there a way to implement a little function to return the size of an array
        > (not a string)?
        >
        > Something like int size(int *array)[/color]

        No. Please check the FAQ before posting.


        --
        Martin Ambuhl

        Comment

        • Leor Zolman

          #5
          Re: returning array size

          On Sat, 07 Feb 2004 20:19:34 -0000, Derk Gwen <derkgwen@HotPO P.com>
          wrote:
          [color=blue]
          >"theAnswer" <i.got@what.it. takes> wrote:
          ># hello,
          ># Is there a way to implement a little function to return the size of an array
          ># (not a string)?
          >#
          ># Something like int size(int *array)
          >
          >The language does not define any way of extracting the allocated size
          >from a pointer to the first (or other) element. While an implementation
          >might (for example if pointer are segregated to different pages based on
          >allocation size), the portable solutions are to pass the length around
          >with the pointer or use a sentinel vector element.[/color]

          If the OP chooses to pass the length along with the pointer, I could
          show him a _real_ efficient implementation of size() ...
          -leor

          P.S. Have I gauged this right, such that it would be perceived as
          humorous rather than sarcastic? I didn't want to put a smiley there
          and prejudice the verdict...


          Leor Zolman
          BD Software
          leor@bdsoft.com
          www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
          C++ users: Download BD Software's free STL Error Message
          Decryptor at www.bdsoft.com/tools/stlfilt.html

          Comment

          • Richard Heathfield

            #6
            Re: returning array size

            Leor Zolman wrote:
            [color=blue]
            > On Sat, 07 Feb 2004 20:19:34 -0000, Derk Gwen <derkgwen@HotPO P.com>
            > wrote:
            >[color=green]
            >>"theAnswer" <i.got@what.it. takes> wrote:
            >># hello,
            >># Is there a way to implement a little function to return the size of an
            >># array (not a string)?
            >>#
            >># Something like int size(int *array)
            >>
            >>The language does not define any way of extracting the allocated size
            >>from a pointer to the first (or other) element. While an implementation
            >>might (for example if pointer are segregated to different pages based on
            >>allocation size), the portable solutions are to pass the length around
            >>with the pointer or use a sentinel vector element.[/color]
            >
            > If the OP chooses to pass the length along with the pointer, I could
            > show him a _real_ efficient implementation of size() ...[/color]

            Quite so. I near... but then I thought better of it.

            [color=blue]
            > -leor
            >
            > P.S. Have I gauged this right, such that it would be perceived as
            > humorous rather than sarcastic?[/color]

            There's a difference? Sarcasm is the lowest form of wit only because nobody
            is beneath using it.
            [color=blue]
            > I didn't want to put a smiley there
            > and prejudice the verdict...[/color]

            Not putting smileys in often enough has got me into lots of trouble. :-)


            --
            Richard Heathfield : binary@eton.pow ernet.co.uk
            "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
            C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
            K&R answers, C books, etc: http://users.powernet.co.uk/eton

            Comment

            Working...