C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Confused in function: close() and bzero()

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Charles
Guest





PostPosted: Mon Sep 20, 2004 3:34 pm    Post subject: Confused in function: close() and bzero() Reply with quote



I am a newbie recently starting some network programming practices
using GCC.

I noticed some strange things while compiling the code below.


****************************************
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

char * host_name = "127.0.0.1";
int port = 8000;

void main(int argc, char *argv[]) {

int socket_descriptor;
struct sockaddr_in pin;

....

bzero(&pin, sizeof(pin));
....

socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)

....

close(socket_descriptor);

}

************************


It's quoted from a sample client program. When I compiled it using gcc
(which is supposed to be C complier, is it right?), it's OK. But when
I try using g++ to compile it, it always reports that close() and
bzero() are not declared.

Also after researching "man close", I found close() is included in
"unistd.h" and by "man bzero", I found bzero() is included in
"string.h". But both "string.h" and "unistd.h" are not included in the
above code. How can it pass the compiling?

My machine is redhat 8.0 kernel 2.4, gcc version is 3.2.

Could somebody explain it to me? Thanks a lot.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Felipe Magno de Almeida
Guest





PostPosted: Tue Sep 21, 2004 11:26 am    Post subject: Re: Confused in function: close() and bzero() Reply with quote



Charles wrote:

Quote:
I am a newbie recently starting some network programming practices
using GCC.

I noticed some strange things while compiling the code below.


****************************************
#include <stdio.h
#include #include #include #include
char * host_name = "127.0.0.1";
int port = 8000;

void main(int argc, char *argv[]) {

int socket_descriptor;
struct sockaddr_in pin;

...

bzero(&pin, sizeof(pin));
...

socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)

...

close(socket_descriptor);

}

************************


It's quoted from a sample client program. When I compiled it using gcc
(which is supposed to be C complier, is it right?), it's OK. But when
I try using g++ to compile it, it always reports that close() and
bzero() are not declared.

Also after researching "man close", I found close() is included in
"unistd.h" and by "man bzero", I found bzero() is included in
"string.h". But both "string.h" and "unistd.h" are not included in the
above code. How can it pass the compiling?
include them

#include #include <unistd.h>
in C, you can use the function without defining it anywhere, but in C++
it is completly prohibited.



--
Felipe Magno de Almeida
UIN: 2113442
email: felipe.almeida@ic unicamp br, felipe.m.almeida@gmail com
I am a C, modern C++, MFC, ODBC, Windows Services, MAPI developer
from synergy, and Computer Science student from State
University of Campinas(UNICAMP).
To know more about:
Unicamp: http://www.ic.unicamp.br
Synergy: http://www.synergy.com.br
current work: http://www.mintercept.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Nicolas Pavlidis
Guest





PostPosted: Tue Sep 21, 2004 11:29 am    Post subject: Re: Confused in function: close() and bzero() Reply with quote



[email]charling_us (AT) hotmail (DOT) com[/email] (Charles) writes:

Quote:
I am a newbie recently starting some network programming practices
using GCC.

I noticed some strange things while compiling the code below.


****************************************
#include #include #include #include #include
char * host_name = "127.0.0.1";
int port = 8000;

void main(int argc, char *argv[]) {

int main(..) please ... :-)

[snipp]

Quote:


It's quoted from a sample client program. When I compiled it using gcc
(which is supposed to be C complier, is it right?), it's OK. But when
I try using g++ to compile it, it always reports that close() and
bzero() are not declared.

Also after researching "man close", I found close() is included in
"unistd.h" and by "man bzero", I found bzero() is included in
"string.h". But both "string.h" and "unistd.h" are not included in the
above code. How can it pass the compiling?

That's because gcc links automatically the hole C - Library, including
string.h and so on, but g++ (the C++ - frontend) doesn't do this.

Trye to compile the code with gcc withOption -Wall and you'll get some
warnings that some function aren't declared.

HTH && Kind regards,
Nicolas

--
Quote:
Nicolas Pavlidis | Elvis Presly: | |__ |
Student of SE & KM | "Into the goto" | |__| |
[email]pavnic (AT) sbox (DOT) tugraz.at[/email] | ICQ #320057056 | |
-------------------University of Technology, Graz----------------|

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Tue Sep 21, 2004 11:33 am    Post subject: Re: Confused in function: close() and bzero() Reply with quote

Charles wrote:
Quote:
I am a newbie recently starting some network programming practices
using GCC.

I noticed some strange things while compiling the code below.


****************************************
#include <stdio.h
#include #include #include #include
char * host_name = "127.0.0.1";
int port = 8000;

void main(int argc, char *argv[]) {

int socket_descriptor;
struct sockaddr_in pin;

...

bzero(&pin, sizeof(pin));
...

socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)

...

close(socket_descriptor);

}

************************


It's quoted from a sample client program. When I compiled it using gcc
(which is supposed to be C complier, is it right?), it's OK. But when
I try using g++ to compile it, it always reports that close() and
bzero() are not declared.

Also after researching "man close", I found close() is included in
"unistd.h" and by "man bzero", I found bzero() is included in
"string.h". But both "string.h" and "unistd.h" are not included in the
above code. How can it pass the compiling?

My machine is redhat 8.0 kernel 2.4, gcc version is 3.2.

Could somebody explain it to me? Thanks a lot.

Setting aside the fact that 'bzero' and 'close' are not standard C
functions, your question can still be answered with, "probably because
those headers [that are not directly included in the given source] do
nonetheless get included [indirectly, by other headers used here]".
Since four out of five headers in the posted code are non-standard,
there is nothing that can be said about what they themselves include
when preprocessed.

Try outputting the result of preprocessor work, and you can see for
yourself who includes the "missing" headers,
Oh, BTW, 'main' is supposed to return 'int'.

Victor

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jack Klein
Guest





PostPosted: Tue Sep 21, 2004 11:34 am    Post subject: Re: Confused in function: close() and bzero() Reply with quote

On 20 Sep 2004 11:34:32 -0400, [email]charling_us (AT) hotmail (DOT) com[/email] (Charles) wrote
in comp.lang.c++.moderated:

Quote:
I am a newbie recently starting some network programming practices
using GCC.

Neither network programming nor GCC are appropriate here. The C++
language standard does not define any networking support at all, nor
does it define the specifics of using specific compilers and linkers.

Quote:
I noticed some strange things while compiling the code below.


****************************************
#include <stdio.h
#include #include #include #include
char * host_name = "127.0.0.1";
int port = 8000;

void main(int argc, char *argv[]) {

Both C and C++ require that main() be defined with a return type of
int. Neither language takes any responsibility for the results of a
program that begins with "void main".

Quote:
int socket_descriptor;
struct sockaddr_in pin;

...

bzero(&pin, sizeof(pin));
...

socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)

...

close(socket_descriptor);

}

************************


It's quoted from a sample client program. When I compiled it using gcc
(which is supposed to be C complier, is it right?), it's OK. But when
I try using g++ to compile it, it always reports that close() and
bzero() are not declared.

Also after researching "man close", I found close() is included in
"unistd.h" and by "man bzero", I found bzero() is included in
"string.h". But both "string.h" and "unistd.h" are not included in the
above code. How can it pass the compiling?

My machine is redhat 8.0 kernel 2.4, gcc version is 3.2.

Could somebody explain it to me? Thanks a lot.

Neither 'close' nor 'bzero' is a standard C++ library function.
These, along all of the headers except for extensions provided by your compiler/operating system combination.

You need to ask this question in a one of the GCC support groups, or a
Linux programming group like news:comp.os.linux.development.apps. It
is not a C++ language issue at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Tue Sep 21, 2004 11:35 am    Post subject: Re: Confused in function: close() and bzero() Reply with quote

Charles wrote:
Quote:
It's quoted from a sample client program. When I compiled it using gcc
(which is supposed to be C complier, is it right?), it's OK. But when
I try using g++ to compile it, it always reports that close() and
bzero() are not declared.

The language C allows functions to be used without them being declared, in
C++ that constitutes an error.

Nonetheless, it can still invoke undefined behaviour in C if the assumed
prototype doesn't match the actual function's declaration, which is why
even in C it is frowned upon.

Specific to GCC's C and C++ compilers is the commandline flag '-Wall' which
you should always use. It would have warned yuo about the dubious use in
the C case. Use of gcc or g++ is rather off-topic here though, for
questions consult the gnu.*.help groups.

cheers

Uli

--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue Sep 21, 2004 11:48 am    Post subject: Re: Confused in function: close() and bzero() Reply with quote

[email]charling_us (AT) hotmail (DOT) com[/email] (Charles) wrote in message
news:<4d10c1c8.0409192202.1c061282 (AT) posting (DOT) google.com>...

Quote:
I am a newbie recently starting some network programming practices
using GCC.

I noticed some strange things while compiling the code below.

****************************************
#include <stdio.h
#include #include #include #include
char * host_name = "127.0.0.1";

In C++, we'd almost certainly write "char const*', rather than "char*".
In both C and C++, I'd use an array, rather than a pointer. In C++, of
course, I'd almost certainly use std::string.

And of course, in a real program, this sort of thing wouldn't be a
global.

Quote:
int port = 8000;

void main(int argc, char *argv[]) {

Technically, "void main" is illegal in both C and C++. Practically, it
is widely used under Windows, but some Unix compilers (including up to
date versions of gcc) correctly reject it.

Quote:
int socket_descriptor;
struct sockaddr_in pin;

...

bzero(&pin, sizeof(pin));

This is NOT a standard C/C++ function. Use memset.
Quote:
...

socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)

...

close(socket_descriptor);

}

************************

It's quoted from a sample client program.

I don't know where you got the sample, but it isn't what I would call a
good example.

Quote:
When I compiled it using gcc (which is supposed to be C complier, is
it right?), it's OK. But when I try using g++ to compile it, it
always reports that close() and bzero() are not declared.

First, whether gcc compiles code as C or as C++ depends on the source
filename, not whether it is invoked as gcc or as g++. The invocation
controls which libraries are implicitly linked (so linking C++ code with
gcc will generally result in undefined externals), and possibly where it
looks for headers.

Secondly, there is NO standard function, either in C or in C++, by the
name of bzero. In is present in Posix, but it is documented there as a
legacy function, which should not be used in new code. According to
Posix, the header which contains it is You should replace it with memset (in <string.h>).

With regards to close, it is also not a C or a C++ function. It is a
Posix function, however, and it should be defined in <unistd.h>. Which
you don't include.

Quote:
Also after researching "man close", I found close() is included in
"unistd.h" and by "man bzero", I found bzero() is included in
"string.h". But both "string.h" and "unistd.h" are not included in the
above code. How can it pass the compiling?

In C, functions may be declared implicitly. This is a deprecated
feature in the latest version of the C standard, so ideally, you should
get a warning. Try compiling with -Wmissing-prototypes (and don't ask
me why this one isn't in -Wall).

In general, I've yet to find a compiler whose default options are
useful. Gcc is no exception. Read the documentation, and define the
options you need. (The exact list of options will depend on the
application. But -Wmissing-prototypes should definitly be in there, at
least when compiling C.)

Quote:
My machine is redhat 8.0 kernel 2.4, gcc version is 3.2.

And it accepts "void main"?

Quote:
Could somebody explain it to me? Thanks a lot.

Just one of the differences between C and C++. And a good reason why
you should probable steer clear of C, even if you don't want all of the
features of C++.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Sep 22, 2004 9:54 am    Post subject: Re: Confused in function: close() and bzero() Reply with quote

Jack Klein <jackklein (AT) spamcop (DOT) net> wrote


Quote:
On 20 Sep 2004 11:34:32 -0400, [email]charling_us (AT) hotmail (DOT) com[/email] (Charles) wrote
in comp.lang.c++.moderated:

I am a newbie recently starting some network programming practices
using GCC.

Neither network programming nor GCC are appropriate here. The C++
language standard does not define any networking support at all, nor
does it define the specifics of using specific compilers and linkers.

but his problem has nothing to do with either.

Quote:
I noticed some strange things while compiling the code below.

****************************************
#include <stdio.h
#include #include #include #include
char * host_name = "127.0.0.1";
int port = 8000;

void main(int argc, char *argv[]) {

Both C and C++ require that main() be defined with a return type of
int. Neither language takes any responsibility for the results of a
program that begins with "void main".

C++ does: it says that the compiler must emit a diagnostic.

Another poster said simply "int main(..) please". In this particular
case, it is more than please. The poster is compiling with g++, and
current versions of g++ do conform to the standard in this regard. The
code won't compile with void main.

[...]
Quote:
Neither 'close' nor 'bzero' is a standard C++ library function.

But his question didn't concern anything specific to close or bzero.
His question was basically, why the code compiled in C and not in C++.

Quote:
These, along all of the headers except for extensions provided by your compiler/operating system combination.

You need to ask this question in a one of the GCC support groups, or a
Linux programming group like news:comp.os.linux.development.apps. It
is not a C++ language issue at all.

It actually is, more or less. It concerns a major difference between C
and C++: the fact that functions may be implicitly declared in C, but
not in C++.

Most C compilers warn when they encounter an implicitly declared
function. Of course, you do have to turn on warnings (-Wall for
gcc/g++).

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sebastian Kapfer
Guest





PostPosted: Thu Sep 23, 2004 8:18 am    Post subject: Re: Confused in function: close() and bzero() Reply with quote

On Tue, 21 Sep 2004 07:29:36 -0400, Nicolas Pavlidis wrote:

Quote:
charling_us (AT) hotmail (DOT) com (Charles) writes:

Also after researching "man close", I found close() is included in
"unistd.h" and by "man bzero", I found bzero() is included in
"string.h". But both "string.h" and "unistd.h" are not included in
the above code. How can it pass the compiling?

That's because gcc links automatically the hole C - Library, including
string.h and so on, but g++ (the C++ - frontend) doesn't do this.

g++ also links the whole C library. The important difference is that g++
is a C++ compiler and thus rejects calls to functions without prototypes.
That's basically what an #include does, it declares function prototypes
(and other stuff). #include has nothing to do with linking.

--
Best Regards, | I couldn't afford a cool signature,
Sebastian | so I just got this one.
Quote:
--------------------------------------------------------
mailbox in "From" silently drops any mail > 20k

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Old Wolf
Guest





PostPosted: Sat Sep 25, 2004 5:36 pm    Post subject: Re: Confused in function: close() and bzero() Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
Quote:
charling_us (AT) hotmail (DOT) com (Charles) wrote:
char * host_name = "127.0.0.1";

In C++, we'd almost certainly write "char const*', rather
than "char*". In both C and C++, I'd use an array, rather
than a pointer.

Why?
In practical terms, I would prefer the pointer, based on the
following logic: "127.0.0.1" exists in the string table. Pointing
to it requires 4 bytes, whereas declaring an automatic array
involves copying 10 bytes (ie. slower). I would only be keen
on copying an object if one of the copies had to be modified.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Sep 27, 2004 8:15 pm    Post subject: Re: Confused in function: close() and bzero() Reply with quote

[email]oldwolf (AT) inspire (DOT) net.nz[/email] (Old Wolf) wrote in message
news:<843a4f78.0409231818.34d015db (AT) posting (DOT) google.com>...
Quote:
kanze (AT) gabi-soft (DOT) fr wrote:
[email]charling_us (AT) hotmail (DOT) com[/email] (Charles) wrote:
char * host_name = "127.0.0.1";

In C++, we'd almost certainly write "char const*', rather
than "char*". In both C and C++, I'd use an array, rather
than a pointer.

Why?

Habit. In C, the array could be const char[], where as a string literal
is char[], so a possible source of errors is eliminated.

Quote:
In practical terms, I would prefer the pointer, based on the following
logic: "127.0.0.1" exists in the string table.

What string table?

Quote:
Pointing to it requires 4 bytes, whereas declaring an automatic array
involves copying 10 bytes (ie. slower). I would only be keen on
copying an object if one of the copies had to be modified.

I very much doubt that the array would be automatic. When I declare a
const object in block scope, it is almost an automatism to declare it
static.

If the object has static lifetime, then declaring a pointer takes four
extra bytes, plus an extra indirection for every access.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.