Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I learned to program in C many years ago, and used C steadily for about 10 years.

These days, I occasionally have to look at C code, because our Informix 4GL RDS customized runners also contain C code, but it is fairly simplistic code.

When I was using C a lot, I never remember hearing about of flexible arrays. I am asking this question, because I came across this SO question. Thanks.

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

It is defined in ISO C99; I don't really know if it has been defined before, but I doubt it.

Traditionally, structures with now-specified flexible arrays were declared with arrays of size 1 at the end, and then allocated according to the desired size, a trick known as the "struct hack". Examples of this may be seen eg. in the Windows API.

share|improve this answer
    
Flexible array members definitely were not standardized before C99; they didn't appear in the 1995 revision of the 1990 ISO C standard. –  Keith Thompson Jun 24 '12 at 20:03
add comment

C99 standardised them. However, they were used long prior with 1-element arrays, a trick known as the struct hack.

It's not clear if it's legal or portable, but it is rather popular. An implementation of the technique might look something like this:

    #include <stdlib.h>
    #include <string.h>

    struct name *makename(char *newname)
    {
        struct name *ret =
            malloc(sizeof(struct name)-1 + strlen(newname)+1);
                    /* -1 for initial [1]; +1 for \0 */
        if(ret != NULL) {
            ret->namelen = strlen(newname);
            strcpy(ret->namestr, newname);
        }

        return ret;
    }

This function allocates an instance of the name structure with the size adjusted so that the namestr field can hold the requested name (not just one character, as the structure declaration would suggest).

Despite its popularity, the technique is also somewhat notorious: Dennis Ritchie has called it ``unwarranted chumminess with the C implementation,'' and an official interpretation has deemed that it is not strictly conforming with the C Standard, although it does seem to work under all known implementations. (Compilers which check array bounds carefully might issue warnings.)

Another possibility is to declare the variable-size element very large, rather than very small...

share|improve this answer
    
AFAIK you're confusing flexible arrays (struct X { int len; double data[]; } and variable-length arrays (int n = get_the_value_somewhere(); int array[n];). They are different. –  jpalecek Jun 24 '12 at 19:31
    
@jpalecek: I just edited that. Hadn't heard them termed flexible arrays before. –  DeadMG Jun 24 '12 at 19:32
    
The term flexible arrays, is why I included the link to the post. –  octopusgrabbus Jun 24 '12 at 20:21
    
@octopusgrabbus: You should not have a question where every answer must click a link. –  DeadMG Jun 24 '12 at 20:23
    
I'm confused. I put the link in my original question, because I had never heard of flexible arrays, and, instead of copying the code here, I thought if anyone wanted to see how that term was used, they could look. Sorry for any protocol misstep. –  octopusgrabbus Jun 24 '12 at 20:27
show 2 more comments

There are two different things that the C99 standard added and they are easy to mix up by mistake:

Flexible array members. This means that a struct/union can have a member of unknown size at the end. Example from the C standard:

struct s { int n; double d[]; };

int m = /* some value */;
struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));

This was used before C99 as well, but it was then undefined behavior, known as the "struct hack" referred to in another answer. Before C90, there could be unexpected padding bytes at the end of the struct, leading to bugs.

Variable length arrays (VLA). These are arrays with their size set in runtime. They are most likely implemented by the compiler by using dynamic memory allocation. Example:

void func (int n)
{
  int array[n];
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.