question about 2.6 in the faq

M

myheartinamerica

I was confused about part 2.6 in the comp.lang.c. FAQ.

2.6 ... <cut>
Another possibility is to declare the variable-size element very
large, rather than very small; in the case of the above example:

...
char namestr[MAXSIZE];

where MAXSIZE is larger than any name which will be stored.
However, it looks like this technique is disallowed by a strict
interpretation of the Standard as well. Furthermore, either of
these "chummy" structures must be used with care, since the
programmer knows more about their size than the compiler does.


I thought it was valid to have arrays of characters in a struct. Am I
missing
something? I realize the intent of the FAQ was to explain how not to
simulate
variable-length arrays.

Thanks for any explanation,
Mick
 
V

vippstar

I was confused about part 2.6 in the comp.lang.c. FAQ.

2.6 ... <cut>
Another possibility is to declare the variable-size element very
large, rather than very small; in the case of the above example:

...
char namestr[MAXSIZE];

where MAXSIZE is larger than any name which will be stored.
However, it looks like this technique is disallowed by a strict
interpretation of the Standard as well. Furthermore, either of
these "chummy" structures must be used with care, since the
programmer knows more about their size than the compiler does.

I thought it was valid to have arrays of characters in a struct. Am I
missing something?

You can have arrays in structs.
The reason the FAQ says this is not valid is because the context is
close to this

char (*p)[42] = malloc(30);
if(p == NULL) return 0;
strcpy(p[0], "hello world");
puts(p[0]);

This will probably compile and "work" as expected by any C
implementation (including the right headers, main, et cetera).
However, p[0] is a char[42]; we've only allocated for a char[30].
Strictly speaking, using p[0] in any way invokes undefined behavior,
just like using a pointer to any object where not enough memory for
that object is allocated is UB.
 
K

Keith Thompson

myheartinamerica said:
I was confused about part 2.6 in the comp.lang.c. FAQ.

2.6 ... <cut>
Another possibility is to declare the variable-size element very
large, rather than very small; in the case of the above example:

...
char namestr[MAXSIZE];

where MAXSIZE is larger than any name which will be stored.
However, it looks like this technique is disallowed by a strict
interpretation of the Standard as well. Furthermore, either of
these "chummy" structures must be used with care, since the
programmer knows more about their size than the compiler does.


I thought it was valid to have arrays of characters in a struct. Am I
missing
something? I realize the intent of the FAQ was to explain how not to
simulate
variable-length arrays.

If you'll look at the immediately following code, the idea is to
allocate *less* than the size of the struct, on the assumption that
you'll be ok if you don't use array elements beyond the allocated
size.

Speaking of variable length, it looks like you're writing long lines
and then something is wrapping them; the result is alternating long
and short lines, which can be difficult to read. If you'll keep your
lines below about 70 columns in the first place, this shouldn't
happen.

Compare:
 
M

myheartinamerica

Speaking of variable length, it looks like you're writing long lines
and then something is wrapping them; the result is alternating long
and short lines, which can be difficult to read.  If you'll keep your
lines below about 70 columns in the first place, this shouldn't
happen.

Compare:

I wholeheartedly agree that it was unreadable. I wrote my response in
Vim wrapped at 80 columns. Then I pasted it into the web interface of
Google Groups, which doesn't seem to show you where it will wrap.
Google Groups lets you compose lines that are wrapped by the size of
the input box. If I resize my browser window, it resizes where the
lines are wrapped.

While I appreciate the convenience of Google Groups, I question its
impact on Usenet readability and quality.

Anyways, I intend this to be the last message I write from the web
interface.

Thanks for your response,
Mick Beaver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top