What's wrong with this code?

C

cpphero

I'm looking at something a colleague has written and the function
looks like this:

void func(char buffer[ 80 ])
{
...
}

where the function will fill in the passed in buffer with some
meaningful data. Something about it doesn't seem quite right; I've
seen plenty of functions take a char* or char[] as an arg and assume
it is a null terminated string, or have a size param passed along, but
I've never encountered a char array of fixed length as an argument.
It compiles and seems to work, are there any pitfalls or reasons why
this shouldn't be done? Or am I just being paranoid?
 
M

Marcel Müller

cpphero said:
I'm looking at something a colleague has written and the function
looks like this:

void func(char buffer[ 80 ])
{
...
}

where the function will fill in the passed in buffer with some
meaningful data. Something about it doesn't seem quite right; I've
seen plenty of functions take a char* or char[] as an arg and assume
it is a null terminated string, or have a size param passed along, but
I've never encountered a char array of fixed length as an argument.
It compiles and seems to work, are there any pitfalls or reasons why
this shouldn't be done? Or am I just being paranoid?

It is exactly the same as char[] except that length is checked at
compile time. Well, and the pitfalls... non constant char pointers are
unsafe anyway and should be avoided in C++ code. The fixed size array
might be safe to some degree, but the implicit conversion to char* is
not. I would recommend std::string instead.


Marcel
 
V

Victor Bazarov

Marcel said:
cpphero said:
I'm looking at something a colleague has written and the function
looks like this:

void func(char buffer[ 80 ])
{
...
}

where the function will fill in the passed in buffer with some
meaningful data. Something about it doesn't seem quite right; I've
seen plenty of functions take a char* or char[] as an arg and assume
it is a null terminated string, or have a size param passed along, but
I've never encountered a char array of fixed length as an argument.
It compiles and seems to work, are there any pitfalls or reasons why
this shouldn't be done? Or am I just being paranoid?

It is exactly the same as char[] except that length is checked at
compile time.

Checked against what?
> Well, and the pitfalls... non constant char pointers are
unsafe anyway and should be avoided in C++ code. The fixed size array
might be safe to some degree, but the implicit conversion to char* is
not. I would recommend std::string instead.


Marcel

V
 
A

Alf P. Steinbach

* Marcel Müller:
cpphero said:
I'm looking at something a colleague has written and the function
looks like this:

void func(char buffer[ 80 ])
{
...
}

where the function will fill in the passed in buffer with some
meaningful data. Something about it doesn't seem quite right; I've
seen plenty of functions take a char* or char[] as an arg and assume
it is a null terminated string, or have a size param passed along, but
I've never encountered a char array of fixed length as an argument.
It compiles and seems to work, are there any pitfalls or reasons why
this shouldn't be done? Or am I just being paranoid?

It is exactly the same as char[] except that length is checked at
compile time.

Sorry, no, there's no such service.

The specified length is just ignored by the compiler.

Specifying a length in the declaration is just a way to inform programmers about
an expected (probably minimum) length, like a comment, unless the array is
passed by reference, which it isn't here.

Well, and the pitfalls... non constant char pointers are
unsafe anyway and should be avoided in C++ code. The fixed size array
might be safe to some degree

There is no fixed size array.

, but the implicit conversion to char* is
not. I would recommend std::string instead.

Yeah.

The main pitfall is that using more or less obscure language features (not
talking about dark corners of language) can confuse programmers.

On the other hand, the only reason that this particular feature can seem obscure
is when the programmers are mainly using other languages. After all, it's been
there since Kernighan & Ritchie C, as I recall. And although I've done it,
"dumbing down" code for others is to plan for everlasting incompetence, which is
sort of defaitist and besides, even if intended to help others, might hit back
like a boomerang; I now believe that one shouldn't ever do that.


Cheers & hth.,

- Alf
 
C

cpphero

cpphero said:
I'm looking at something a colleague has written and the function
looks like this:
void func(char buffer[ 80 ])
{
  ...
}
where the function will fill in the passed in buffer with some
meaningful data.  Something about it doesn't seem quite right; I've
seen plenty of functions take a char* or char[] as an arg and assume
it is a null terminated string, or have a size param passed along, but
I've never encountered a char array of fixed length as an argument.
It compiles and seems to work, are there any pitfalls or reasons why
this shouldn't be done? Or am I just being paranoid?

It is exactly the same as char[] except that length is checked at
compile time. Well, and the pitfalls... non constant char pointers are
unsafe anyway and should be avoided in C++ code. The fixed size array
might be safe to some degree, but the implicit conversion to char* is
not. I would recommend std::string instead.

Marcel

I tried playing around with this method of passing a char pointer to a
function in mingw and it compiles without error even if you pass the
function an array with a different length! (This is with the flags -
Wall -pedantic). The function silently overwrites memory which
shouldn't be altered by the function. So I suppose it is essentially
identical to char* and char[]. I tried:

void func(char buf[8])
{
buf[0] = 'h';
buf[1] = 'e';
buf[2] = 'l';
buf[3] = 'l';
buf[4] = 'o';
buf[5] = '!';
buf[6] = '!';
buf[7] = '\0';
}

int main(int argc, char** argv)
{
char buf1[2] = {'A', '\0' }
char buf2[2] = {'B', '\0' }

func(buf2);

return 0;
}
 
W

White Wolf

Marcel said:
cpphero said:
I'm looking at something a colleague has written and the function
looks like this:

void func(char buffer[ 80 ])
{
...
}

where the function will fill in the passed in buffer with some
meaningful data. Something about it doesn't seem quite right; I've
seen plenty of functions take a char* or char[] as an arg and assume
it is a null terminated string, or have a size param passed along, but
I've never encountered a char array of fixed length as an argument.
It compiles and seems to work, are there any pitfalls or reasons why
this shouldn't be done? Or am I just being paranoid?

It is exactly the same as char[] except that length is checked at
compile time. Well, and the pitfalls... non constant char pointers are
unsafe anyway and should be avoided in C++ code. The fixed size array
might be safe to some degree, but the implicit conversion to char* is
not. I would recommend std::string instead.

*Nope*. The 80 is ignored. It is exactly the same as char[], so check
for length compile time. Unless you write void func(char (&buffer)[80])
or the const ref variant.

BR, WW
 

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
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top