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. zero-sized array in struct/union

zero-sized array in struct/union

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
25 Posts 7 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.
  • L Lost User

    That is not a struct or a union. Is there some other code that you need to show us?

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

    The original code is:

    typedef struct {
    ....
    ....
    		char\* file\_name\[0\];			/\* File name in Unicode. \*/
    };  // warning C4094: untagged 'struct' declared no symbols
    

    also, I get another warning here: warning C4094: untagged 'struct' declared no symbols I don't know how to get rid of this warnings ...

    L D 2 Replies Last reply
    0
    • _ _Flaviu

      Or

      char* file_name[1];

      ? anyway, is more than 0 :)

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #6

      Well, it all depends upon what you are going to achieve... :rolleyes:

      1 Reply Last reply
      0
      • _ _Flaviu

        Or

        char* file_name[1];

        ? anyway, is more than 0 :)

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

        _Flaviu wrote:

        Or

        char* file_name[1];

        Yes. The only reason someone would declare zero-length is to dynamic allocate the array. You should change the array length to [1]. If you change it to _MAX_PATH (260) then you will be wasting 1036 bytes on a 32 bit machine and wasting 2072 bytes on a 64 bit machine. Wasting bytes is punishable by death. Best Wishes, -David Delaune

        CPalliniC _ 3 Replies Last reply
        0
        • _ _Flaviu

          I have a legacy C code:

          char* file_name[0];

          which generate a warning: warning C4200: nonstandard extension used : zero-sized array in struct/union it is correct to write:

          char* file_name[_MAX_PATH];

          ? I don't know the impact of this modify...

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #8

          Read carefully the documentation[^] (see the sample code). Using _MAX_PATH (or whatever >0) is correct, the impact is in memory: each time the struct is allocated, _MAX_PATH character pointers are allocated too. You might instead choose to disable the warning, if it makes sense (e.g. there is an additional field in the struct specifying the actual size of the array).

          In testa che avete, signor di Ceprano?

          _ 2 Replies Last reply
          0
          • L Lost User

            _Flaviu wrote:

            Or

            char* file_name[1];

            Yes. The only reason someone would declare zero-length is to dynamic allocate the array. You should change the array length to [1]. If you change it to _MAX_PATH (260) then you will be wasting 1036 bytes on a 32 bit machine and wasting 2072 bytes on a 64 bit machine. Wasting bytes is punishable by death. Best Wishes, -David Delaune

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #9

            Quote:

            You should change the array length to [1]

            Quote:

            Wasting bytes is punishable by death

            Take your own conclusions. :-D

            In testa che avete, signor di Ceprano?

            L 1 Reply Last reply
            0
            • L Lost User

              _Flaviu wrote:

              Or

              char* file_name[1];

              Yes. The only reason someone would declare zero-length is to dynamic allocate the array. You should change the array length to [1]. If you change it to _MAX_PATH (260) then you will be wasting 1036 bytes on a 32 bit machine and wasting 2072 bytes on a 64 bit machine. Wasting bytes is punishable by death. Best Wishes, -David Delaune

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #10

              :laugh: Thank you !

              1 Reply Last reply
              0
              • CPalliniC CPallini

                Read carefully the documentation[^] (see the sample code). Using _MAX_PATH (or whatever >0) is correct, the impact is in memory: each time the struct is allocated, _MAX_PATH character pointers are allocated too. You might instead choose to disable the warning, if it makes sense (e.g. there is an additional field in the struct specifying the actual size of the array).

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #11

                I guess disabling this warning is best solution … how can I do that ? With pragma statement ? If yes, which version of pragma should I use ?

                L S 2 Replies Last reply
                0
                • CPalliniC CPallini

                  Quote:

                  You should change the array length to [1]

                  Quote:

                  Wasting bytes is punishable by death

                  Take your own conclusions. :-D

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

                  Hmmm, The law states that wasting bytes less or equal to 1 * sizeof(pointer) is allowed but only in the month of August. I guess he could remove the array qualifier but then that would probably break his compile. :) Best Wishes, -David Delaune

                  CPalliniC 1 Reply Last reply
                  0
                  • _ _Flaviu

                    The original code is:

                    typedef struct {
                    ....
                    ....
                    		char\* file\_name\[0\];			/\* File name in Unicode. \*/
                    };  // warning C4094: untagged 'struct' declared no symbols
                    

                    also, I get another warning here: warning C4094: untagged 'struct' declared no symbols I don't know how to get rid of this warnings ...

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

                    You are using a typedef but have not given it the name that you wish to use. It should be something like:

                    typedef struct {
                    ....
                    ....
                    char* file_name[0]; /* File name in Unicode. */
                    } myStruct;
                    // myStruct is now an alias for the above structure

                    Also the comment on the last line makes no sense; firstly it is declaring an aray of pointers rather than characters. And secondly, you should not store Unicode characters in a char type array. It will most likely cause problems at run time. The zero length array is possibly valid, but it depends on how the code uses the struct. It can be used as a placeholder name for space that will be allocated for a dynamic structure at run time. Something like:

                    struct foo
                    {
                    int i;
                    char text[0];
                    };

                    // ... other code

                    struct foo* myFoo = (struct foo*)malloc(sizeof(struct foo) + 20); // additional 20 bytes for the char data.

                    S 1 Reply Last reply
                    0
                    • _ _Flaviu

                      I guess disabling this warning is best solution … how can I do that ? With pragma statement ? If yes, which version of pragma should I use ?

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

                      Never disable warnings, they are there to help you.

                      1 Reply Last reply
                      0
                      • CPalliniC CPallini

                        Read carefully the documentation[^] (see the sample code). Using _MAX_PATH (or whatever >0) is correct, the impact is in memory: each time the struct is allocated, _MAX_PATH character pointers are allocated too. You might instead choose to disable the warning, if it makes sense (e.g. there is an additional field in the struct specifying the actual size of the array).

                        _ Offline
                        _ Offline
                        _Flaviu
                        wrote on last edited by
                        #15

                        Sorry, I didn't saw the link first time ...

                        1 Reply Last reply
                        0
                        • L Lost User

                          Hmmm, The law states that wasting bytes less or equal to 1 * sizeof(pointer) is allowed but only in the month of August. I guess he could remove the array qualifier but then that would probably break his compile. :) Best Wishes, -David Delaune

                          CPalliniC Offline
                          CPalliniC Offline
                          CPallini
                          wrote on last edited by
                          #16

                          Disabling the warning is an option.

                          In testa che avete, signor di Ceprano?

                          L 1 Reply Last reply
                          0
                          • L Lost User

                            _Flaviu wrote:

                            Or

                            char* file_name[1];

                            Yes. The only reason someone would declare zero-length is to dynamic allocate the array. You should change the array length to [1]. If you change it to _MAX_PATH (260) then you will be wasting 1036 bytes on a 32 bit machine and wasting 2072 bytes on a 64 bit machine. Wasting bytes is punishable by death. Best Wishes, -David Delaune

                            CPalliniC Offline
                            CPalliniC Offline
                            CPallini
                            wrote on last edited by
                            #17

                            Quote:

                            _MAX_PATH (260) then you will be wasting 236 bytes on a 32 bit machine and wasting 472 bytes on a 64 bit machine

                            Hey David, the math there is not clear to me. Do I need more caffeine this morning?

                            In testa che avete, signor di Ceprano?

                            L L 2 Replies Last reply
                            0
                            • CPalliniC CPallini

                              Disabling the warning is an option.

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

                              Yes, but that is punished by [tar and feathering](https://en.wikipedia.org/wiki/Tarring\_and\_feathering) :laugh: Best Wishes, -David Delaune

                              1 Reply Last reply
                              0
                              • CPalliniC CPallini

                                Quote:

                                _MAX_PATH (260) then you will be wasting 236 bytes on a 32 bit machine and wasting 472 bytes on a 64 bit machine

                                Hey David, the math there is not clear to me. Do I need more caffeine this morning?

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

                                CPallini wrote:

                                Do I need more caffeine this morning?

                                No, but I do... Best Wishes, -David Delaune

                                1 Reply Last reply
                                0
                                • CPalliniC CPallini

                                  Quote:

                                  _MAX_PATH (260) then you will be wasting 236 bytes on a 32 bit machine and wasting 472 bytes on a 64 bit machine

                                  Hey David, the math there is not clear to me. Do I need more caffeine this morning?

                                  L Offline
                                  L Offline
                                  leon de boer
                                  wrote on last edited by
                                  #20

                                  If you are on C11 ... C11 6.7.9/14 allows the option

                                  char file_name[];

                                  It was addedd for exactly that reason

                                  In vino veritas

                                  CPalliniC 1 Reply Last reply
                                  0
                                  • L leon de boer

                                    If you are on C11 ... C11 6.7.9/14 allows the option

                                    char file_name[];

                                    It was addedd for exactly that reason

                                    In vino veritas

                                    CPalliniC Offline
                                    CPalliniC Offline
                                    CPallini
                                    wrote on last edited by
                                    #21

                                    Nice to know.

                                    In testa che avete, signor di Ceprano?

                                    1 Reply Last reply
                                    0
                                    • _ _Flaviu

                                      The original code is:

                                      typedef struct {
                                      ....
                                      ....
                                      		char\* file\_name\[0\];			/\* File name in Unicode. \*/
                                      };  // warning C4094: untagged 'struct' declared no symbols
                                      

                                      also, I get another warning here: warning C4094: untagged 'struct' declared no symbols I don't know how to get rid of this warnings ...

                                      D Offline
                                      D Offline
                                      Davie21240
                                      wrote on last edited by
                                      #22

                                      pretty sure you could just use char* name; since the array size is 0.

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        You are using a typedef but have not given it the name that you wish to use. It should be something like:

                                        typedef struct {
                                        ....
                                        ....
                                        char* file_name[0]; /* File name in Unicode. */
                                        } myStruct;
                                        // myStruct is now an alias for the above structure

                                        Also the comment on the last line makes no sense; firstly it is declaring an aray of pointers rather than characters. And secondly, you should not store Unicode characters in a char type array. It will most likely cause problems at run time. The zero length array is possibly valid, but it depends on how the code uses the struct. It can be used as a placeholder name for space that will be allocated for a dynamic structure at run time. Something like:

                                        struct foo
                                        {
                                        int i;
                                        char text[0];
                                        };

                                        // ... other code

                                        struct foo* myFoo = (struct foo*)malloc(sizeof(struct foo) + 20); // additional 20 bytes for the char data.

                                        S Offline
                                        S Offline
                                        Stefan_Lang
                                        wrote on last edited by
                                        #23

                                        I was about to write an answer when i saw this. Yes, zero length char arrays at the end of a struct appeared to be quite common in C programming some years (or decades, rather) ago. I haven't seen it in any C**++** code ever, although it probably works the same. Whatever you wish to achieve, there's probably a better solution available in C++ syntax. Usually, std::string is the go to solution here. That said, yes, it must be char [], not char* [], otherwise it doesn't make any sense at all.

                                        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                                        L 1 Reply Last reply
                                        0
                                        • _ _Flaviu

                                          I guess disabling this warning is best solution … how can I do that ? With pragma statement ? If yes, which version of pragma should I use ?

                                          S Offline
                                          S Offline
                                          Stefan_Lang
                                          wrote on last edited by
                                          #24

                                          Don't disable warnings unless you are 100% sure what they're telling you, 100% sure that this is not a problem for the syntactic and semantic functionality of your code, and at least 90% sure there's no reasonable way to avoid them. Under these conditions, the best way is to use #pragma push immediately before the disable command and #pragma pop after the code that causes the warning. That way you can be sure that the remainder of the code will use the same warning settings as defined in the compiler options.

                                          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                                          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