473,834 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char data[0]

Hi all,

this is a question which i saw in a book

typedef struct mall_li_header_ {
int refcnt;
uchar pool;
uchar flag;
ushort magic_no;
char data[0];

} mall_li_header_ t;

What is the use of data[0] here ?
to which answer i found from my colleagues as

this is a very old (and common) idea used by a lot of game programmers
from yesteryears.
The answer for completeness sake - common use is : allocate arbitrary
size - anything above the size of the struct can be referenced as
"data".

to what extent this is true?????

Oct 10 '06 #1
31 7769
aa*****@gmail.c om wrote:
this is a question which i saw in a book
(snip struct hack)
What is the use of data[0] here ?
to which answer i found from my colleagues as
(snip)

What your colleagues said is similar to the FAQ's content:
http://c-faq.com/struct/structhack.html

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Oct 10 '06 #2
On 10 Oct 2006 11:56:12 -0700, aa*****@gmail.c om wrote in comp.lang.c:
Hi all,

this is a question which i saw in a book

typedef struct mall_li_header_ {
int refcnt;
uchar pool;
uchar flag;
ushort magic_no;
char data[0];

} mall_li_header_ t;

What is the use of data[0] here ?
to which answer i found from my colleagues as

this is a very old (and common) idea used by a lot of game programmers
from yesteryears.
The answer for completeness sake - common use is : allocate arbitrary
size - anything above the size of the struct can be referenced as
"data".

to what extent this is true?????
What it is actually for is testing for broken compilers, since an
array declaration with a size of 0 has never been allowed in any
version of standard C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 11 '06 #3
Jack Klein wrote:
aa*****@gmail.c om wrote:

typedef struct mall_li_header_ {
char data[0];

What it is actually for is testing for broken compilers, since an
array declaration with a size of 0 has never been allowed in any
version of standard C.
Compilers are allowed to offer extensions that don't alter the
behaviour of any conforming program.

Oct 11 '06 #4
aa*****@gmail.c om wrote:
to what extent this is true?????
"none."
Oct 11 '06 #5
aa*****@gmail.c om wrote:
this is a very old (and common) idea used by a lot of game programmers
from yesteryears.
I remember a lot of yesteryears' games failing with bizarre memory
problems, especially when running them on later OS versions, with
different memory managers, etc.

If you want an incomplete type, just put a void * in your struct and
allocate that properly. Would that kill you?
Oct 11 '06 #6
"Old Wolf" <ol*****@inspir e.net.nzwrites:
Jack Klein wrote:
>aa*****@gmail.c om wrote:
>
typedef struct mall_li_header_ {
char data[0];

What it is actually for is testing for broken compilers, since an
array declaration with a size of 0 has never been allowed in any
version of standard C.

Compilers are allowed to offer extensions that don't alter the
behaviour of any conforming program.
Yes, but if the expression is constant and doesn't have a value
greater than zero, it's a constraint violation requiring a diagnostic.
A compiler is free to do what it likes after issuing the diagnostic.
(And of course it's free not to issue the diagnostic in non-conforming
mode; the standard by definition cannot constrain implementations that
don't conform to it.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 11 '06 #7
jmcgill <jm*****@email. arizona.eduwrit es:
aa*****@gmail.c om wrote:
>this is a very old (and common) idea used by a lot of game programmers
from yesteryears.

I remember a lot of yesteryears' games failing with bizarre memory
problems, especially when running them on later OS versions, with
different memory managers, etc.

If you want an incomplete type, just put a void * in your struct and
allocate that properly. Would that kill you?
The struct hack has the advantage that the structure and the array can
be allocated contiguously. It's certainly not legal if you declare
the array with [0], but with [1] it's arguably legal and de facto
portable. It's sufficiently popular that C99 provided a well-defined
version, flexible array members.

See the comp.lang.c FAQ, <http://www.c-faq.com/>, question 2.6.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 11 '06 #8

Keith Thompson <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
jmcgill <jm*****@email. arizona.eduwrit es:
aa*****@gmail.c om wrote:
this is a very old (and common) idea used by a lot of game programmers
from yesteryears.
I remember a lot of yesteryears' games failing with bizarre memory
problems, especially when running them on later OS versions, with
different memory managers, etc.

If you want an incomplete type, just put a void * in your struct and
allocate that properly. Would that kill you?

The struct hack has the advantage that the structure and the array can
be allocated contiguously.
Of course, you can allocate a "data" pointer contiguously in a struct
in any event, right?

typedef struct {
unsigned data_type;
unsigned data_size;
unsigned *data;
} contiguous_data _struct;

contiguous_data _struct *my_contiguous_ data_struct;

void create_struct(u nsigned data_type,unsig ned data_size) {
my_contiguous_d ata_struct=
malloc(sizeof(c ontiguous_data_ struct)+(data_s ize*sizeof(unsi gned));
my_contiguous_d ata_struct->data_type=data _type;
my_contiguous_d ata_struct->data_size=data _size;
my_contiguous_d ata_struct->data=
my_contiguous_d ata_struct+size of(contiguous_d ata_struct);
}

And now you may write the data to my_contiguous_d ata_struct->data(++).

Of course, you wouldn't want to do this for certain types of struct
uses, like an array of structs that you intend to search for data of a
certain type quickly using pointer arithmetic. However, it does allow
you to free the entire schmear using one free().

Right?

---
William Ernest Reid

Oct 11 '06 #9
>Keith Thompson <ks***@mib.orgw rote in message
>news:ln******* *****@nuthaus.m ib.org...
>The struct hack has the advantage that the structure and the array can
be allocated contiguously.
In article <zY************ *******@bgtnsc0 4-news.ops.worldn et.att.net>
Bill Reid <ho********@hap pyhealthy.netwr ote:
>Of course, you can allocate a "data" pointer contiguously in a struct
in any event, right?
Yes; but one crucial fix (and one style nit):
>typedef struct {
unsigned data_type;
unsigned data_size;
unsigned *data;
} contiguous_data _struct;

contiguous_dat a_struct *my_contiguous_ data_struct;

void create_struct(u nsigned data_type,unsig ned data_size) {
my_contiguous_d ata_struct=
malloc(sizeof(c ontiguous_data_ struct)+(data_s ize*sizeof(unsi gned));
It is "better style" (at least according to those who think it is
better :-) ) to write this as:

my_contiguous_d ata_struct =
malloc(sizeof *my_contiguous_ data_struct +
data_size * sizeof *my_contiguous_ data_struct->data);

That is, apply sizeof to the object(s) being allocated, rather
than their types. That way, if you make a small change to the
data types -- e.g., if you decide to save space by using
"unsigned short" instead of "unsigned int" for the data --
the code automatically follows the change.
my_contiguous_d ata_struct->data_type=data _type;
my_contiguous_d ata_struct->data_size=data _size;
my_contiguous_d ata_struct->data=
my_contiguous_d ata_struct+size of(contiguous_d ata_struct);
This last line needs to add 1, not "sizeof(contigu ous_data_struct )"
(nor sizeof *my_contiguous_ data_struct). The result has type
"pointer to contiguous_data _struct" -- the same as the type of
the left-hand operand -- so a cast is also required (to
"unsigned *", or perhaps "void *").

Note that, if the compiler is not able to "see" that the
"data" pointer points just past the structure itself, on most
machines you will get some extra (and slightly slower) code
to access the data elements. That is, without the "struct hack":

my_contiguous_d ata_struct->data[i]

tends to compile into:

- obtain value of my_contiguous_d ata_struct pointer
- use that value to obtain pointer value my_contiguous_d ata_struct->data
- obtain value of i, scale if needed, and add to previous
- follow this last pointer to the data

or in machine-code terms:

# assumes "i" is in register r2 at this point
load r1, my_contiguous_d ata_struct # first step above
load r1, 12(r1) # 2nd step, assuming 4-byte "int"
sll r3, r2, 2 # r3 = r2 * 4 (scale)
add r3, r1, r3 # r3 = &...->data[i]
# now r3 points to my_contiguous_d ata_struct->data[i]

load r4, (r3) # (if we want to read it)

Using the "struct hack" (or C99's flexible members), on the other
hand, we get to skip one "load" step, perhaps at the cost of an
extra "add", and the machine code becomes more like:

load r1, my_contiguous_d ata_struct
sll r3, r2, 2
add r3, r1, r3
# now r3 points 12 bytes below my_contiguous_d ata_struct

load r4, 12(r3)

(this assumes there is a "constant(r eg)" offset addressing mode,
but no reg(reg) mode; many machines have both; some even offer
scaling for one of the "reg"s, which can eliminate the shift
instruction).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Oct 11 '06 #10

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

Similar topics

7
4837
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am ordered by my instructor and she does have her reasons so I have to use char*. So there is alot of c in the code as well Anyways, I have a linked list of linked lists of a class we defined, I need to make all this into a char*, I know that I...
74
43288
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
9
2212
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my attempt at it, but it seems to be lacking... I'm aware that strdup() is nonstandard (and a bad idea for C++ code) - please just bear with me: /* Assume relevant headers are included */ class char_ptr {
20
3087
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code: --cut here-- typedef struct pkg_ { short int info; char* data;
7
5593
by: techno | last post by:
Dear all, Our bitmap has some x00 values ( '\0' ) and i am storing it in char* array. the problem is that the '\0' is treated as eos character in c and it is truncating it so the characters after it are not pass to the function. here is the code snippet /* DATA8583.data = ( char ) ( unsigned int ) 0xF0;
6
2315
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why? The compiler i'm using is Dev c++. #include <stdio.h> #include <stdlib.h>
16
10134
by: Dave | last post by:
Hi all, I have a 4 byte char array with the binary data for two 16-bit signed integers in it like this: Index 3 2 1 0 Data Bh Bl Ah Al Where Bh is the high byte of signed 16-bit integer B and so on.
5
2854
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is from when I declare some of my char variables
0
27267
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications are constrained by the technology available at the time of their development and hence aren’t optimum as per current scenario. One of such cases is the extensive usage of CHAR fields, which aren’t optimum solution for space storage now. This paper...
4
3228
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
0
9796
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9643
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10789
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10504
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3079
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.