Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Array of size ZERO

Array of size ZERO

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structureshelp
8 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?

    J M L _ S 6 Replies Last reply
    0
    • A AmbiguousName

      hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?

      J Offline
      J Offline
      Jalapeno Bob
      wrote on last edited by
      #2

      Allowing an array size of zero is a good thing. The array still exists, even if it is empty. We can pass it, process it and otherwise use it without having to write special code. Often, database and table queries return zero rows. Since the rows returned can be viewed as an array of rows, the number of rows is the size of the array.

      E 1 Reply Last reply
      0
      • A AmbiguousName

        hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        yeah, so ? Well, you need an empty array to be able to fill it.

        Watched code never compiles.

        1 Reply Last reply
        0
        • A AmbiguousName

          hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          In my old "C" days I used zero sized arrays as placeholders for dynamic sized structs. Something like this:

          typedef struct _VARIABLE_SIZED_STRUCT {
          	//Other members
          	BYTE data[0];
          } VARIABLE_SIZED_STRUCT, *PVARIABLE_SIZED_STRUCT;
          
          PVARIABLE_SIZED_STRUCT pDynamic = (PVARIABLE_SIZED_STRUCT)malloc(FIELD_OFFSET(VARIABLE_SIZED_STRUCT,data[iSize]));
          

          The zero sized placeholder is antiquated and no longer needed with the C++ language. In fact I believe support for zero sized array was removed from recent MSVC compilers. Best Wishes, -David Delaune

          1 Reply Last reply
          0
          • A AmbiguousName

            hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            Visual Studio 2010 will flag an error for int i[0]; You can however create it dynamically - int* i = new i[0];

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++)

            Polymorphism in C

            1 Reply Last reply
            0
            • A AmbiguousName

              hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?

              S Offline
              S Offline
              Sauro Viti
              wrote on last edited by
              #6

              As Superman already wrote, declaring an array with zero size is not allowed, at least on Microsoft Visual C++. Something interesting is that, on versions prior to Visual Studio 2010, which is the first one that implements static_assert, such functionality was realized using the _STATIC_ASSERT Macro[^]. That macro tests at compile time for the given condition and give the Compiler Error C2466[^] if not satisfied. Ops! C2466 means cannot allocate an array of constant size 0. By going deeper, we can search the macro on the crtdbg.h header:

              #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ]

              Now, let's try to imagine how it works...

              1 Reply Last reply
              0
              • A AmbiguousName

                hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?

                A Offline
                A Offline
                Aescleal
                wrote on last edited by
                #7

                You can in C99 (I think VC++ supports this syntax in structures if you use /Ze but it's not a full C99 flexible array thing) but you can't in C90 or C++. The sort of thing it's useful for in C99 is for arrays with variable sized structures. You can write:

                struct foo
                {
                int number_of_elements_;
                int elements_[];
                };

                and then have the constructor sort out sizing the array. In C90 and C++ you'd have to have one element and manually size everything. Cheers, Ash

                1 Reply Last reply
                0
                • J Jalapeno Bob

                  Allowing an array size of zero is a good thing. The array still exists, even if it is empty. We can pass it, process it and otherwise use it without having to write special code. Often, database and table queries return zero rows. Since the rows returned can be viewed as an array of rows, the number of rows is the size of the array.

                  E Offline
                  E Offline
                  Eugen Podsypalnikov
                  wrote on last edited by
                  #8

                  Jalapeno Bob wrote:

                  Allowing an array size of zero is a good thing.

                  Here would be a possible initial form of the thing :) :

                  template <typename T>
                  class SimpleArray
                  {
                  T* m_pData;
                  int m_iLength;

                  public:
                  SimpleArray() : m_pData(NULL), m_iLength(0) {};
                  ...
                  };

                  They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups