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; Originally Posted by Elysia But if you have a strongly typed function that only accepts arrays, you will get a ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,772
    Quote Originally Posted by Elysia
    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.
    Or you could have the function's (or function template's) parameters be an iterator pair instead, then overload with convenience functions for various containers as desired.
    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

  2. #17
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    But this may be a good thing.
    This concept is utterly destroyed because of the idiotic requirement. Yes, it is an idiotic requirement; protecting clients from themselves isn't a sufficient reason to even try to enforce it. Trying to do this, as you are pretending to want, requires that every component of the system that uses an array in almost any capacity be a template. At that point the complain that templates bloat code will be true, but no one, including you remember (earlier thread), will agree to follow such a broken requirement.

    If a developer can't or refuses to use the alternatives--to arrays--starting with such a template is simply wrong.

    Soma

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    Oh, come on. Who says it must be the only function?
    You can have more overloads for other types such as containers and iterators, all which just call a generic function in a safe way.
    Limited, well-defined interfaces allow for less bugs at runtime AFAIK, which is a good thing™. I'm not suggesting you should go overboard with it, as it is pointless if it never used and code duplication is evil™. There is a tradeoff, but I'm just throwing some few nudgets here.
    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.

  4. #19
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Oh, come on. Who says it must be the only function?
    You can have more overloads for other types such as containers and iterators, all which just call a generic function in a safe way.
    No one said that. But I am not going to create tons of overloads for differently sized arrays. Templates ease the pains of doing that, but then we come back to the problems noted in the previous replies. Templates might as well be a requirement for functions that use arrays like this in any capacity.

  5. #20
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    You can have more overloads for other types such as containers and iterators, all which just call a generic function in a safe way.
    Those overloads for other types necessarily involve separating a normal array target from its size and allowing it to decay into a pointer.

    You and what you are forwarding are trying to avoid exactly that.

    Soma

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    I mean, C arrays should just be used as a last resort. std::array or std::vector is far better, and 99% of the time, you will be using them, and we agree that when using them, there are no problems, right?
    You can catch the rest of the C arrays with few a few overloads.
    I'm losing track of what exactly people are arguing about here :/
    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.

  7. #22
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    >> I mean, C arrays should just be used as a last resort.
    Whatever you say, I mean this is a new one in a never ending list of comebacks you have.

    >> You can catch the rest of the C arrays with few a few overloads.
    >> I'm losing track of what exactly people are arguing about here :/
    What argument
    Last edited by whiteflags; 09-01-2012 at 07:05 AM.

Page 2 of 2 FirstFirst 12
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, 03:37 AM
  2. Determining file size
    By waterborne in forum C Programming
    Replies: 5
    Last Post: 12-15-2009, 07:56 AM
  3. determining size of struct
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-09-2008, 07:10 AM
  4. sizeof: determining the size of char array problems...
    By what3v3r in forum C++ Programming
    Replies: 17
    Last Post: 02-09-2006, 01:40 AM
  5. determining size
    By gooddevil in forum C Programming
    Replies: 5
    Last Post: 05-22-2004, 11:10 AM

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