473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

why arrary with size of zero?

I saw some library code wrote like this:

struct X{
int x;
char y[0]; /* Note: here array size is ZERO, why? */
};


Why use array size zero. I heard about the C standard
not allow array with size zero, but my gcc tell me I can
compile this code without error or warning.

One give me explaination this trick can used for dynamic
memory, for example:
size_t bufsize;
...
struct X* px = NULL;
px = (struct X*)malloc(sizeof(struct X) + bufsize);
in this way, we can gain a X and additional bufsize in once
malloc invoke. Does this correct?

Another question is whether this code portable?

Any help is appreciated.

--
Regards.
Mockey Chen
Email: mo*********@gmail.com
Nov 21 '05 #1
2 1323
Mockey Chen <mo*********@google.com> wrote:
struct X{
int x;
char y[0]; /* Note: here array size is ZERO, why? */
};
It's a nonportable hack, at best. It is almost, but not quite, the
"struct hack" that is also nonportable but nevertheless widely used.

http://www.eskimo.com/~scs/C-faq/q2.6.html
Why use array size zero. I heard about the C standard
not allow array with size zero, but my gcc tell me I can
compile this code without error or warning.


You're not invoking it in strictly conforming mode. Use gcc -Wall
-ansi -pedantic and you will see that it does, indeed, warn you that
it is a nonstandard construct.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 21 '05 #2
On 2005-11-21 09:43:29 -0500, Christopher Benson-Manica
<at***@nospam.cyberspace.org> said:
Mockey Chen <mo*********@google.com> wrote:
struct X{
int x;
char y[0]; /* Note: here array size is ZERO, why? */
};


It's a nonportable hack, at best. It is almost, but not quite, the
"struct hack" that is also nonportable but nevertheless widely used.

http://www.eskimo.com/~scs/C-faq/q2.6.html


Though, note that with C99, the following is legal (basically a
legalization of the struct hack):

#include <stdlib.h>

struct X {
int x;
char y[]; //Note empty brackets
};

struct X *allocateX(int count)
{
struct X *result = malloc(count + sizeof *result);
if(result)
{
result->x = count;
}
return result;
}

int main()
{
struct X *x_ptr;

if(x_ptr = allocateX(20))
{
//I can treat x_ptr->y basically as if it were an array of 20 char's
x_ptr->y[19] = 'a';
}

return 0;
}

Why use array size zero. I heard about the C standard not allow array
with size zero, but my gcc tell me I can compile this code without
error or warning.


You're not invoking it in strictly conforming mode. Use gcc -Wall
-ansi -pedantic and you will see that it does, indeed, warn you that
it is a nonstandard construct.

--
Clark S. Cox, III
cl*******@gmail.com

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
3
by: Nan Li | last post by:
Please look at the code and run below. My question is why the compiler cannot figure out the size of the array in function foo? Is this a bug or expected? Thanks a lot. #include <iostream> ...
54
by: Neo | last post by:
Hi Folks, I've a simple qestion related to dynamic memory allocation in C here is the code: #include <stdio.h> int main() {
3
by: Marauderz | last post by:
Hi there, Got a quick question, I have an app that I'm compiling with /clr and I have a native function that passes in a pointer, and I need to copy from that unmanaged memory into a managed...
27
by: Deephay | last post by:
Greetings all, I have a program that used the realloc() function to change the allocated size of a buffer, the program works with some arguments, but with some other arguments, it will show me...
7
by: ottawajn | last post by:
Dear there, I want to do the follows. (1)initial an array, say myarray={1 2 3.1 4}; (2)insert zeros in myarray, like {1,0,2,0,3.1,0,4,0}; (3)let myarray={1,0,2,0,3.1,0,4,0}; (I have not...
0
by: alanwo | last post by:
Hi Experts, Interesting finding, when comparing two dictionary of byte(), KeyNotFoundException throwed but, that byte() key is present in another dictionary. Is that the limitation of comparison...
1
by: Abdul Samad | last post by:
Assalam mu Alikum, i understand the size of structure with no data types is zero. but why the size of structure variable is 1 Byte when the size of that structure is zeor? for example ...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.