Why the simple code can not compile?

J

Jack

#include "string.h"
#include <iostream>
using namespace std;
int main()
{
char buf[1024] = "This is a test.";
int len = 1024;
bzero(buf, len);
cout<<"buf::"<<buf<<endl;
}

The error is:
line 8: Error: The function "bzero" must have a prototype.
1 Error(s) detected.

I tried to compile it on Solaris SUN.

Thanks.
 
B

Barry

Jack said:
#include "string.h"
#include <iostream>
using namespace std;

suppose the function is in some cxx file

void bzero(char*, size_t);
int main()
{
char buf[1024] = "This is a test.";
int len = 1024;
bzero(buf, len);
cout<<"buf::"<<buf<<endl;
}

The error is:
line 8: Error: The function "bzero" must have a prototype.
1 Error(s) detected.

I tried to compile it on Solaris SUN.

Thanks.
 
I

Ian Collins

Jack said:
#include "string.h"
#include <iostream>
using namespace std;
int main()
{
char buf[1024] = "This is a test.";
int len = 1024;
bzero(buf, len);
cout<<"buf::"<<buf<<endl;
}

The error is:
line 8: Error: The function "bzero" must have a prototype.
1 Error(s) detected.
man bzero (and check the include file).
 
N

Neelesh Bodas

#include "string.h"
#include <iostream>
using namespace std;
int main()
{
char buf[1024] = "This is a test.";
int len = 1024;
bzero(buf, len);
cout<<"buf::"<<buf<<endl;

}

The error is:
line 8: Error: The function "bzero" must have a prototype.
1 Error(s) detected.

I tried to compile it on Solaris SUN.

bzero was declared in C header 'strings.h'. I am not sure if C++
header strings.h declares it, but othe point to note is that this
function is deprecated, and it is suggested (by 'man bzero') that
memset be used instead.

Of course I am assuming that you are talking about the bzero function
defined in the (c-standard) library. If you are talking about your
self-defined bzero function, make sure that its prototype is made
available.

Neelesh

-Neelesh
 
R

robertwessel2

bzero was declared in C header 'strings.h'. I am not sure if C++
header strings.h declares it, but othe point to note is that this
function is deprecated, and it is suggested (by 'man bzero') that
memset be used instead.

Of course I am assuming that you are talking about the bzero function
defined in the (c-standard) library. If you are talking about your
self-defined bzero function, make sure that its prototype is made
available.


There is no bzero in the standard C (or C++) library, but it's a
fairly common extension in *nix implementations.
 
J

James Kanze

#include "string.h"
#include <iostream>
using namespace std;
int main()
{
char buf[1024] = "This is a test.";
int len = 1024;
bzero(buf, len);
cout<<"buf::"<<buf<<endl;
}
The error is:
line 8: Error: The function "bzero" must have a prototype.
1 Error(s) detected.
I tried to compile it on Solaris SUN.
bzero was declared in C header 'strings.h'.

Only in certain versions of Unix, pre-1990.
I am not sure if C++
header strings.h declares it, but othe point to note is that this
function is deprecated, and it is suggested (by 'man bzero') that
memset be used instead.

Not deprecated. It never officially existed, at least in
standard C. It was present in the Berkley distributions of
Unix, and is part of Posix, but in the supplementary header
<strings.h> (not <string.h>). Given that it's deprecated by
Posix, however, and was never part of standard C, I'd definitely
replace it by memset (which is standard, and declared in
<string.h>---or you can use std::memset, and <cstring>).
 
J

Jack

#include "string.h"
#include <iostream>
using namespace std;
int main()
{
char buf[1024] = "This is a test.";
int len = 1024;
bzero(buf, len);
cout<<"buf::"<<buf<<endl;

The error is:
line 8: Error: The function "bzero" must have a prototype.
1 Error(s) detected.
I tried to compile it on Solaris SUN.

bzero was declared in C header 'strings.h'. I am not sure if C++
header strings.h declares it, but othe point to note is that this
function is deprecated, and it is suggested (by 'man bzero') that
memset be used instead.

Of course I am assuming that you are talking about the bzero function
defined in the (c-standard) library. If you are talking about your
self-defined bzero function, make sure that its prototype is made
available.

Neelesh

-Neelesh

Thanks. But it works fine on Linux when I used g++. And it is defined
in <string.h> on Linux.
 
W

werasm

James Kanze wrote:

Not deprecated. It never officially existed, at least in
standard C. It was present in the Berkley distributions of
Unix, and is part of Posix, but in the supplementary header
<strings.h> (not <string.h>). Given that it's deprecated by
Posix, however, and was never part of standard C, I'd definitely
replace it by memset (which is standard, and declared in
<string.h>---or you can use std::memset, and <cstring>).

Maybe I'm slightly paranoid, but I would rather use something
like this...

#include <cstddef>
#include <algorithm>

template <class T, std::size_t N>
void zero( T(&array)[N] )
{
std::fill( array, array+N, T() );
}

int main()
{
char array[] = "Hallo";
zero( array );
}

.... as I've seen memset being used erroneously by mistake
one to many times. OTOH I think you would probably too.

Regards,

Werner
 
J

James Kanze

Maybe I'm slightly paranoid, but I would rather use something
like this...

Not paranoid, just good programming practice.
#include <cstddef>
#include <algorithm>
template <class T, std::size_t N>
void zero( T(&array)[N] )
{
std::fill( array, array+N, T() );
}
int main()
{
char array[] = "Hallo";
zero( array );
}
... as I've seen memset being used erroneously by mistake
one to many times. OTOH I think you would probably too.

Now that you mention it, yes. Memset only works for C style
arrays of integers. std::fill (and std::fill_n) works for
everything. (And in C, it was a common error to see memset used
for arrays of double or of pointer.)
 
J

James Kanze

#include "string.h"
#include <iostream>
using namespace std;
int main()
{
char buf[1024] = "This is a test.";
int len = 1024;
bzero(buf, len);
cout<<"buf::"<<buf<<endl;
}
The error is:
line 8: Error: The function "bzero" must have a prototype.
1 Error(s) detected.
I tried to compile it on Solaris SUN.
bzero was declared in C header 'strings.h'. I am not sure if C++
header strings.h declares it, but othe point to note is that this
function is deprecated, and it is suggested (by 'man bzero') that
memset be used instead.

[...]
Thanks. But it works fine on Linux when I used g++. And it is
defined in <string.h> on Linux.

Which is an error. Neither C nor Posix allow <string.h> to
contain the symbol bzero in any form; according to the
standards, the following program should compile and run
everywhere, both as C and as C++:

#define bzero "Hello, World!"

#include <string.h>
#include <stdio.h>

int
main()
{
printf( "%s\n", bzero ) ;
return 0 ;
}

(Curiously, on my Linux, it compiles as C, but not as C++. So
presumably, there are some #ifdef's somewhere which mean that
the symbol is not defined if you invoke the standard compliant C
compiler. And that something isn't being defined correctly when
you invoke the standard compliant C++ compiler; a bug report is
probably in order.)
 
J

Jerry Coffin

[ ... ]
Now that you mention it, yes. Memset only works for C style
arrays of integers.

It works for arrays of char. For arrays of integers, it works only for a
rather loose definition of "works". Generally speaking, the only value
that's useful for non-char types is zero. For example:

int x;

memset(&x, 1, sizeof(x));

std::cout << x;

One likely result:

16843009
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top