zero-sized array in struct/union
-
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...
_Flaviu wrote:
it is correct to write:
char* file_name[_MAX_PATH];
Yes, it is correct presuming you are going to define the array of pointers
-
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...
-
_Flaviu wrote:
it is correct to write:
char* file_name[_MAX_PATH];
Yes, it is correct presuming you are going to define the array of pointers
-
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 ... -
Well, it all depends upon what you are going to achieve... :rolleyes:
-
_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
-
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...
Read carefully the documentation[^] (see the sample code). Using
_MAX_PATH
(or whatever>0
) is correct, the impact is in memory: each time thestruct
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). -
_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
-
_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
-
Read carefully the documentation[^] (see the sample code). Using
_MAX_PATH
(or whatever>0
) is correct, the impact is in memory: each time thestruct
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). -
Quote:
You should change the array length to [1]
Quote:
Wasting bytes is punishable by death
Take your own conclusions. :-D
-
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 ...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 structureAlso 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.
-
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 ?
-
Read carefully the documentation[^] (see the sample code). Using
_MAX_PATH
(or whatever>0
) is correct, the impact is in memory: each time thestruct
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). -
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
-
_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
-
Yes, but that is punished by [tar and feathering](https://en.wikipedia.org/wiki/Tarring\_and\_feathering) :laugh: Best Wishes, -David Delaune
-
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?
-
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?
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
-
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