Like Tree1Likes

determining the size of an array when passed by ref

This is a discussion on determining the size of an array when passed by ref within the C++ Programming forums, part of the General Programming Boards category; Is there a reason why I get the correct result when I use this approach within the function that I ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Chicago
    Posts
    1

    determining the size of an array when passed by ref

    Is there a reason why I get the correct result when I use this approach within the function that I instantiated my class as an array but when I do the same thing in a function where I pass the array class (byref) I get 1?
    Code:
     sizeof array / sizeof array[0]
    The suggestion was made that I should have used the "vector" class as an alternative approach to create my class array instead of using native arrays. As this would provide some built-in features for the array, such as storing the size and allowing dynamic sizing, while also providing some safety, such as range-checking.
    Last edited by mdehnel; 08-30-2012 at 09:37 AM. Reason: Adding outside help

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    When an array is passed to a function, it is converted to a pointer. The called function only ever receives a pointer. A pointer is not associated with any information related to the size of what it points at.

    sizeof(array) will therefore be the sizeof(pointer), not the size of the array passed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When you pass the array by ref then actually you are passing a pointer.That's why this i is a good approach.If you pass the whole object then the copy constructor will be invoked,and a big(maybe) object has to be copied and passed as an argument and then be destroyed!

    However i think that it would be helpful to post your code,in order to exactly see what you are doing.Or just wait for a user that is very good to answer(there are many here)

    EDIT->one already did

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,539
    you can pass an array by reference with templates:

    Code:
    template<int N>
    void SomeFunction(char (&c)[N]);
    then sizeof(c) will actually be the length of the array in bytes, but in this situation, you then can use N instead of sizeof(c) / sizeof(char).
    Elysia likes this.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Quote Originally Posted by std10093 View Post
    When you pass the array by ref then actually you are passing a pointer.That's why this i is a good approach.If you pass the whole object then the copy constructor will be invoked,and a big(maybe) object has to be copied and passed as an argument and then be destroyed!
    No, that is not correct.
    There are two ways to pass arrays: by address (or pointer, if you will; which is presumably what the OP did since sizeof won't work then), and by reference (which Elkvis covered).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    It boggles my mind that people find this so hard.

    Compute the size before the call.

    Code:
    void foo( type array[], size_t nelem );
    
    foo( array, sizeof(array) / sizeof(array[0]) );

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Or just stop poisoning function call parameter lists with unnecessary parameters that the compiler can deduce itself (and lessen the amount of mistakes and errors).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,118
    Quote Originally Posted by mdehnel
    The suggestion was made that I should have used the "vector" class as an alternative approach to create my class array instead of using native arrays. As this would provide some built-in features for the array, such as storing the size and allowing dynamic sizing, while also providing some safety, such as range-checking.
    If you specifically want a fixed size array, then consider using std::array instead of std::vector.

    Quote Originally Posted by Elysia
    Or just stop poisoning function call parameter lists with unnecessary parameters that the compiler can deduce itself (and lessen the amount of mistakes and errors).
    If you are talking with respect to whiteflags' post #6, then I note that the size parameter is not necessarily unnecessary since there may not be a guarantee that the argument will be a fixed size array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,539
    Quote Originally Posted by laserlight View Post
    since there may not be a guarantee that the argument will be a fixed size array.
    in that case you'd have an overloaded function to handle it.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Quote Originally Posted by laserlight View Post
    If you are talking with respect to whiteflags' post #6, then I note that the size parameter is not necessarily unnecessary since there may not be a guarantee that the argument will be a fixed size array.
    Yes, there are cases where it might be necessary, but I really want to get across the message: Don't use a size parameter unless absolutely necessary.
    If it looks like you need one, seriously consider your design. Can you use a container (eg std::vector) instead?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    If it looks like you need one, seriously consider your design. Can you use a container (eg std::vector) instead?
    I actually agree, by the way. I've just never thought of a valid reason to pass an array by reference. It's easy to say it prevents errors, but I'd prefer like two or three other ideas that just work better because of convention, IMO. It's just not worth knowing.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,359
    in that case you'd have an overloaded function to handle it.
    If you start with the interface you posted, you would have to duplicate functionality (implementation); you couldn't just call that function from an overloaded interface.

    You need to start with a robust general interface; you can add convenience wrappers at your leisure.

    Or just stop poisoning function call parameter lists with unnecessary parameters that the compiler can deduce itself (and lessen the amount of mistakes and errors).
    The parameter is obviously necessary; you've moved the information about in one way or the other, but the information is still a requirement.

    Don't use a size parameter unless absolutely necessary.
    The code you forward (as Elkvis posted) does not eliminate the size parameter; it moves the size parameter into a template.

    That's useful for a convenience function, but it doesn't improve the implementation.

    Soma

    trouble with hash function implementing

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    I suspect the OP's interpretation of "pass by reference" was passing a pointer, not the trickery shown by Elkvis (neat as it is, when used right).

    Quote Originally Posted by phantomotap View Post
    The code you forward (as Elkvis posted) does not eliminate the size parameter; it moves the size parameter into a template.

    That's useful for a convenience function, but it doesn't improve the implementation.
    Indeed.

    It is also a function that will not work in this example.
    Code:
    template<int N>   void SomeFunction(char (&c)[N])     // Elkvis's function
    {
         // something with elements of c, cognisant of the value of N
    }
    
    void intermediary(char a[])
    {
         SomeFunction(a);     //   This will not compile, fortunately
    }
    
    int main()
    {
         char x[2];
    
         SomeFunction(x);     //   will call Elkvis's template function correctly
    
         intermediary(x);       //   attempt to call Elkvis's template function
    }
    Fortunately, a C++ compiler will reward you with a compilation error in the body of intermediary() - unless the SomeFunction() template has been specialised for a char pointer. This is because the argument a inside intermediary() is actually a pointer, lacking size information. Which brings us back to the OP's original problem ....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Quote Originally Posted by phantomotap View Post
    The parameter is obviously necessary; you've moved the information about in one way or the other, but the information is still a requirement.
    Oh sure. I don't think anyone disagrees with that. The disagreement is more of how to pass along that information (ie via the compiler implicitly deducing it, some standard library keeping track of it or passing it explicitly).

    Quote Originally Posted by grumpy View Post
    It is also a function that will not work in this example.
    But this may be a good thing.
    There are some valid differences about pointers vs arrays (such as sizeof), which may be critical for safe code.
    Imagine someone having a function taking an explicit size, then passing an array with sizeof to that function. So far, so good, right?
    Then imagine someone suddenly changing the array to a pointer and forgetting to change the sizeof. Boom! A bug right there, and you won't realize it until runtime.
    But if you have a strongly typed function that only accepts arrays, you will get a compile error and can fix it before it becomes a bug.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    Quote Originally Posted by Elysia View Post
    But this may be a good thing.
    There are some valid differences about pointers vs arrays (such as sizeof), which may be critical for safe code.
    Imagine someone having a function taking an explicit size, then passing an array with sizeof to that function. So far, so good, right?
    Then imagine someone suddenly changing the array to a pointer and forgetting to change the sizeof. Boom! A bug right there, and you won't realize it until runtime.
    But if you have a strongly typed function that only accepts arrays, you will get a compile error and can fix it before it becomes a bug.
    Sure. I agree with you on that. My post wasn't a criticism of your technique. It's just that I've seen a few too many people make the unjustified leap to believing that your technique can be used to obtain a size of an array from a pointer. The compiler complains, which it should, they ask why, and the discussion becomes circular.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining size of array returning strange values?
    By edddo in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2011, 04:37 AM
  2. Determining file size
    By waterborne in forum C Programming
    Replies: 5
    Last Post: 12-15-2009, 08:56 AM
  3. determining size of struct
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-09-2008, 08:10 AM
  4. sizeof: determining the size of char array problems...
    By what3v3r in forum C++ Programming
    Replies: 17
    Last Post: 02-09-2006, 02:40 AM
  5. determining size
    By gooddevil in forum C Programming
    Replies: 5
    Last Post: 05-22-2004, 12:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21