Checking the size of an array

Discussion in 'C++' started by Noah Roberts, Feb 14, 2007.

  1. Noah Roberts

    Noah Roberts Guest

    The Cool Giraffe wrote:
    > I have a dynamically declared array of integers and i'd
    > like to check the number of elements. For some reason,
    > the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
    > work. I've been googling and i got a head ache more
    > than understanding.


    You can't do it.

    >
    > Let me put my question this way. In Java, i'd go as
    > mtx.length to learn the size. How can it be done
    > _easily_ in C++? The shorter the better. :)
    >


    If you use std::vector<> instead of a dynamically allocated array you
    will be able to get the element count from the size() member.
     
    Noah Roberts, Feb 14, 2007
    #1
    1. Advertising

  2. Noah Roberts

    Guest

    On Feb 14, 1:06 pm, "The Cool Giraffe" <>
    wrote:
    > I have a dynamically declared array of integers and i'd
    > like to check the number of elements.


    You cannot do this in C++ since an array is essentially a pointer to a
    memory region.

    > Let me put my question this way. In Java, i'd go as
    > mtx.length to learn the size. How can it be done
    > _easily_ in C++? The shorter the better. :)


    Use a std::vector instead.
    Then you can use mtx.size() down the road.

    Cheers,
    Andre
     
    , Feb 14, 2007
    #2
    1. Advertising

  3. On Feb 14, 4:06 pm, "The Cool Giraffe" <>
    wrote:

    > I have a dynamically declared array of integers and i'd
    > like to check the number of elements. For some reason,
    > the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
    > work. I've been googling and i got a head ache more
    > than understanding.
    >
    > Let me put my question this way. In Java, i'd go as
    > mtx.length to learn the size. How can it be done
    > _easily_ in C++? The shorter the better. :)


    To do this easily in C++, don't use an array, use a std::vector.
    std::vector::size() returns the size of the vector.

    E.g.:

    #include <vector>

    int main()
    {
    using namespace std;
    vector<int> v;
    v.push_back(7);
    v.push_back(-3);
    v.push_back(6);
    v.push_back(12);
    // etc.
    int theSize = v.size();
    }

    Best regards,

    Tom
     
    Thomas Tutone, Feb 14, 2007
    #3
  4. I have a dynamically declared array of integers and i'd
    like to check the number of elements. For some reason,
    the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
    work. I've been googling and i got a head ache more
    than understanding.

    Let me put my question this way. In Java, i'd go as
    mtx.length to learn the size. How can it be done
    _easily_ in C++? The shorter the better. :)

    --
    Vänligen Kerstin Viltersten
    (The Cool Giraffe)
     
    The Cool Giraffe, Feb 14, 2007
    #4
  5. The Cool Giraffe wrote:
    > I have a dynamically declared array of integers and i'd
    > like to check the number of elements. For some reason,
    > the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
    > work. I've been googling and i got a head ache more
    > than understanding.
    >
    > Let me put my question this way. In Java, i'd go as
    > mtx.length to learn the size. How can it be done
    > _easily_ in C++? The shorter the better. :)


    There is no [portable] way.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 14, 2007
    #5
  6. Noah Roberts

    Vallabha Guest

    On Feb 15, 2:06 am, "The Cool Giraffe" <>
    wrote:
    > I have a dynamically declared array of integers and i'd
    > like to check the number of elements. For some reason,
    > the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
    > work. I've been googling and i got a head ache more
    > than understanding.
    >
    > Let me put my question this way. In Java, i'd go as
    > mtx.length to learn the size. How can it be done
    > _easily_ in C++? The shorter the better. :)
    >
    > --
    > Vänligen Kerstin Viltersten
    > (The Cool Giraffe)


    I prefer vectors over dynamic array in such situaltions, as vectors
    are more handy (say initilization, growing elements etc) ..and using
    vectors you can easily find out the length also..

    -Vallabha
    S7 Software Solutions
    http://www.s7solutions.com
     
    Vallabha, Feb 15, 2007
    #6
  7. Noah Roberts

    Noah Roberts Guest

    Victor Bazarov wrote:
    > The Cool Giraffe wrote:
    >> I have a dynamically declared array of integers and i'd
    >> like to check the number of elements. For some reason,
    >> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
    >> work. I've been googling and i got a head ache more
    >> than understanding.
    >>
    >> Let me put my question this way. In Java, i'd go as
    >> mtx.length to learn the size. How can it be done
    >> _easily_ in C++? The shorter the better. :)

    >
    > There is no [portable] way.


    I'm curious, what is a non-portable one? I imagine if you knew how the
    heap was tracked and had access to the memory regions containing the
    information you want, you could read it. It won't be the element count
    though, it will be the size of the allocated region, which may or may
    not coincide with how much was requested. So even if you had access,
    which in most cases I don't believe you do, the information you where
    able to read probably wouldn't be of much use to you and would be
    inaccurate for your purpose.
     
    Noah Roberts, Feb 15, 2007
    #7
  8. Noah Roberts wrote:
    > Victor Bazarov wrote:
    >> The Cool Giraffe wrote:
    >>> I have a dynamically declared array of integers and i'd
    >>> like to check the number of elements. For some reason,
    >>> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
    >>> work. I've been googling and i got a head ache more
    >>> than understanding.
    >>>
    >>> Let me put my question this way. In Java, i'd go as
    >>> mtx.length to learn the size. How can it be done
    >>> _easily_ in C++? The shorter the better. :)

    >>
    >> There is no [portable] way.

    >
    > I'm curious, what is a non-portable one? I imagine if you knew how
    > the heap was tracked and had access to the memory regions containing
    > the information you want, you could read it. It won't be the element
    > count though, it will be the size of the allocated region, which may
    > or may not coincide with how much was requested. So even if you had
    > access, which in most cases I don't believe you do, the information
    > you where able to read probably wouldn't be of much use to you and
    > would be inaccurate for your purpose.


    Divide the size [of the allocated region] by the sizeof(*ptr) and you
    get the size of the array, no? If it matters to know exactly how much
    was *requested*, it would be passed to the same function as a separate
    argument.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 15, 2007
    #8
  9. Noah Roberts

    Noah Roberts Guest

    Victor Bazarov wrote:
    > Noah Roberts wrote:
    >> Victor Bazarov wrote:
    >>> The Cool Giraffe wrote:
    >>>> I have a dynamically declared array of integers and i'd
    >>>> like to check the number of elements. For some reason,
    >>>> the expected sizeof(mtx)/sizeof(mtx[0]) doesn't
    >>>> work. I've been googling and i got a head ache more
    >>>> than understanding.
    >>>>
    >>>> Let me put my question this way. In Java, i'd go as
    >>>> mtx.length to learn the size. How can it be done
    >>>> _easily_ in C++? The shorter the better. :)
    >>> There is no [portable] way.

    >> I'm curious, what is a non-portable one? I imagine if you knew how
    >> the heap was tracked and had access to the memory regions containing
    >> the information you want, you could read it. It won't be the element
    >> count though, it will be the size of the allocated region, which may
    >> or may not coincide with how much was requested. So even if you had
    >> access, which in most cases I don't believe you do, the information
    >> you where able to read probably wouldn't be of much use to you and
    >> would be inaccurate for your purpose.

    >
    > Divide the size [of the allocated region] by the sizeof(*ptr) and you
    > get the size of the array, no? If it matters to know exactly how much
    > was *requested*, it would be passed to the same function as a separate
    > argument.


    Well, I think that is what is desired though. How much was actually
    allocated isn't of much use...the extra space is likely to not be
    initialized at all, much less filled with useful information.

    I'm pretty sure what the OP wants is something like the following:

    void f(char [] ptr)
    {
    for (int i = 0; i < ptr.length; ++i)
    putchar(ptr);
    }

    I don't think even your non-portable solution will do what they want
    much of the time. AFAIK it wouldn't even be dependable on a single
    given architecture with a particular compiler and version...or even
    between runs.

    Of course, they could write their own allocator and it could provide
    something similar to your proposal and it could even be implemented in a
    portable manner. Lots of work though.
     
    Noah Roberts, Feb 15, 2007
    #9
    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. Vicky

    Checking file upload size

    Vicky, Feb 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    360
    =?Utf-8?B?QXZuZWVzaA==?=
    Feb 11, 2004
  2. iris
    Replies:
    1
    Views:
    24,002
    Robert Olofsson
    Oct 30, 2003
  3. Replies:
    5
    Views:
    551
    Flash Gordon
    Apr 9, 2006
  4. Replies:
    8
    Views:
    506
    Bob Hairgrove
    Apr 10, 2006
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,587
    Michael Jung
    May 25, 2008
Loading...

Share This Page