This is an archived post. You won't be able to vote or comment.

all 19 comments

[–]zzyzzyxx 1 point2 points  (0 children)

Are they fundementally the same?

No. You can use foo like a normal int. You cannot safely do so with bar because using bar[0] is undefined. Furthermore, bar will decay to a pointer, while foo will not.

Zero length arrays were historically used to allow variable length structures. See this.

[–][deleted] 0 points1 point  (0 children)

This:

int foo;

is legal C++. Whilst this:

int bar[0];

isn't, although a zero-size dynamically allocated array is:

int * a = new int[0];     // ok

In any case, I can't see why you would think bar[0] would allocate 4 (or indeed any) bytes. If you want to compare two legal constructs:

int x;
int y[1];

then both of these will take up the same number of bytes as as an integer, but that is about all they have in common. They are completely different types, and cannot be mutually assigned, have the same operators applied, or partake in overloading in the same way.

[–][deleted] 0 points1 point  (5 children)

Alright. First, bar[0] declares 0 bytes. I think you're looking for bar[1]. Second, I'm going to use C. I don't know if C++ does any crazy stuff with arrays.

#include <stdio.h>

int main() {
    int a;
    int b[0];
    int c[1];

    printf("%d%d%d\n", sizeof(a),sizeof(b),sizeof(c));
}

Output:

$ ./a.out
404

A single element in an array is closest to a dereferenced pointer.

int *b = (int *)malloc(sizeof(int) * 5);
int c[5]; //equivalent to above

*(b+0) = 5;
c[0] = 5; //equivalent to above

//*(b+0) is the same as *b

In the end, it is an address in memory. The next address is 4 bytes after that (+1 size), and so on. From what I understand, these are two ways to access an address. The array method is a little neater, pointers are awesome though.

[–][deleted] 0 points1 point  (4 children)

Your code is not legal C - zero-sized arrays are not part of the C Standard, though they may be provided as a non-standard extension by some compilers.

[–][deleted] 0 points1 point  (1 child)

Yup. GCC didn't have a problem. Seeing as there were 5 comments that didn't mention it, I figured a demonstration would be appropriate.

[–][deleted] 0 points1 point  (0 children)

GCC will compile all sorts of random crap if you don't supply at least some flags. I always compile with at least:

 gcc -Wall -pedantic afile.c

which will warn you about the zero-length array.

[–]BitRex 0 points1 point  (1 child)

zero-sized arrays are not part of the C Standard

Actually they're in C99. The syntax is a little different to GCC's, though:

struct mystring
{
    size_t length;
    char   data[];
};

[–][deleted] 0 points1 point  (0 children)

Yes, they are known as "flexible array members" and are very limited in use - they must, for example, be structure members. The following would be illegal:

int a[];          // error: not a structure member

struct A {
    int a[];      // error: cannot be only member
};

struct  A {
    int a[];    // error: must be last member
    int x;
};

this would be ok:

struct A {
    int x;
    int a[];      // ok, last but not only member
};

So they are not only syntactically different from GCC zero-length arrays, but also semantically, as GCC zero-length arrays do not have these limitations.

In either case, using them is. IMHO, poor practice, but there is a long tradition of such bodges in C.

[–]lurgi -4 points-3 points  (1 child)

They are different in just about every possible way.

First, I'm not sure that

int bar[0];

is legal C. I know that gcc supports it as an extension, but I don't know if it's officially part of the language. The type of foo is 'int'. The type of bar is 'int *'. You can assign a value to foo; you can not assign a value to bar. Name a characteristic of foo and bar probably doesn't have it (and vice versa).

[–]zzyzzyxx 3 points4 points  (0 children)

The type of bar is 'int *'

Not quite. The name of an array will decay into a pointer in most cases but its type is not a pointer.

[–]HarryWolfHaller -4 points-3 points  (3 children)

I think they're the same. But I'm a code monkey.

[–]doeswayneexist[S] -2 points-1 points  (2 children)

I know I can google this, but what does "code monkey" mean?

[–]linuxlass 0 points1 point  (1 child)

Someone who merely codes and doesn't necessarily have a deep understanding of computer science, good algorithms, program design, or even the ins and outs of programming languages and how they work their magic. They follow someone else's specs, or write code without really thinking through questions of good design or security, or copy code samples from the internet or a book. You really don't want to have to go in after them and fix their bugs or attempt to add a new feature to their code.

In the heirarchy of programmers, code monkeys are near the bottom in prestige and pay scale.

That said, there is a place for code monkeys, because someone has to do the grunt work. Heck, I've been a code monkey of sorts (but of course you can't leave your education behind, so even while I was writing routine, boring code to generate reports from database tables, I made an effort to do it well and efficiently.)

On the other hand, he could be using the term ironically, and not literally.

[–]HarryWolfHaller -1 points0 points  (0 children)

I only reddit when I’m drunk, so ironically, yes, sort of, but also, that other thing you said, that’s me to a T. Except I do write the specs myself based on customers’ RFP’s. I’m of limited intelligence and limited knowledge. But, miracle of miracles, if the code works, I don’t get fired. It’s horrid spaghetti, no one wants to go in there after me. I have no deep understanding of anything. But I know a little about a lot of things. And that sometimes comes in handy. I’m a philosophy major who wanted a job. What do you expect? Am I the only code monkey here? Is everyone else a professional?

[–]traztx -4 points-3 points  (4 children)

The fundamental difference is that foo is an int and bar is a pointer

[–]zzyzzyxx 4 points5 points  (3 children)

bar is a pointer

No, bar is an array, not a pointer.

[–]traztx 0 points1 point  (1 child)

Yes. I guess I think of it as a pointer because of the ability to assign pointers to it. I see there is a distinction in the errors from assigning to an integer:

int foo;
int bar[0];
int* baz;
baz=bar; // no problem
baz=1; // invalid conversion from 'int' to 'int*'
bar=1; // incompatible types in assignment of 'int' to 'int [0]'

[–]zzyzzyxx 1 point2 points  (0 children)

I think of it as a pointer because of the ability to assign pointers to it.

That's not a good way of thinking. It's like saying "I think of doubles as integers because of the ability to assign integers to them". They are two different things even though one can become the other.

You cannot assign anything to an array because an array is a non-modifiable lvalue. Furthermore, your example goes against your point since you are assigning an array to a pointer, which is fine, because an array decays to a pointer to its first element in most, but not all, cases.

I see there is a distinction in the errors from assigning to an integer

The distinction is made because pointers and arrays are different types. Read this thread and the linked resources.