C-style coding in C++

Discussion in 'C++' started by Garma, Jan 16, 2004.

  1. Garma

    Garma Guest

    In C++ code of classes, would it be bad to use all those C-runtime
    functions, for example, strcpy, strlen, strcat, etc.?

    What are Standard libraries which have the above C functions? istringstream
    and string?

    If using those C-runtime, does it mean that the code will also be linked to
    C-runtime library and C++ libraries? Is there any disadvantage with this?

    If a source code contains some classes that are not instantiated anywhere in
    the code, will compilers automatically ignore them automatically?

    Thanks for your comments!
     
    Garma, Jan 16, 2004
    #1
    1. Advertising

  2. Garma wrote:

    > In C++ code of classes, would it be bad to use all those C-runtime
    > functions, for example, strcpy, strlen, strcat, etc.?


    Yes.

    > What are Standard libraries which have the above C functions?
    > <sstream> and <string>?


    Yes.

    > If using those C-runtime libraries,
    > does it mean that the code will also be linked to
    > C-runtime library and C++ libraries?


    Consult your compiler manual for the answer to that question.

    > Is there any disadvantage with this?


    It makes your C++ code more difficult to read, understand and maintain.

    > If a source code contains some classes
    > that are not instantiated anywhere in the code,
    > will compilers ignore them automatically?


    Yes.
     
    E. Robert Tisdale, Jan 16, 2004
    #2
    1. Advertising

  3. In article <GTGNb.44597$>, Garma wrote:
    > In C++ code of classes, would it be bad to use all those C-runtime
    > functions, for example, strcpy, strlen, strcat, etc.?


    One usually doesn't need these functions if the string class is used. Functions
    like strcpy and strcat are dangerous if not used judiciously (you will get
    buffer overruns if the destination string is not large enough to store the
    result). strncat and strncpy are safer (you still have to know how
    big the destination is, but not how big the source/s is/are)

    > What are Standard libraries which have the above C functions? istringstream
    > and string?


    Don't understand the question. The functions are defined in <cstring>

    > If using those C-runtime, does it mean that the code will also be linked to
    > C-runtime library and C++ libraries? Is there any disadvantage with this?


    Implementation dependent. Unlikely to be a disadvantage arising this way.

    > If a source code contains some classes that are not instantiated anywhere in
    > the code, will compilers automatically ignore them automatically?


    No.

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
     
    Donovan Rebbechi, Jan 16, 2004
    #3
  4. "E. Robert Tisdale" <> wrote in message
    news:...
    > Garma wrote:
    >
    > > In C++ code of classes, would it be bad to use all those C-runtime
    > > functions, for example, strcpy, strlen, strcat, etc.?

    >
    > Yes.


    No. It depends on what you are doing. If you have a C string then strlen is
    the only reasonable way to get its length. Just be
    sure you wouldn't be better off using std::string.

    By they way they are not C runtime functions they are C++ runtime functions
    that happen to be defined to be (more or less) the
    same as those in the C standard.

    >
    > > What are Standard libraries which have the above C functions?
    > > <sstream> and <string>?

    >
    > Yes.


    The same as in C.
    Or preferably use the C++ified headers: for <string.h> use <cstring> and put
    std:: in front of them.

    e.g.
    #include <string.h>
    int len = strlen("hello");
    or better
    #include <cstring>
    int len = std::strlen("hello"); // actually the exact same function
    >
    > > If using those C-runtime libraries,
    > > does it mean that the code will also be linked to
    > > C-runtime library and C++ libraries?

    >
    > Consult your compiler manual for the answer to that question.


    No - for a standard compliant C++ compiler it will be linked to the C++
    library/libraries (which may just happen to be
    the same as the C ones).

    >
    > > Is there any disadvantage with this?

    >
    > It makes your C++ code more difficult to read, understand and maintain.


    What? the linking thing? How?

    >
    > > If a source code contains some classes
    > > that are not instantiated anywhere in the code,
    > > will compilers ignore them automatically?

    >
    > Yes.
    >


    Not necessarily - in most implementations, if the source for the class is in
    a separate file and is compiled and the resultant
    object file is added to a library then when you come to link your
    application it wont be linked in if it is not referenced.
    This isn't standard C++ stuff - its just how (static) libraries and linkers
    work.
     
    Nick Hounsome, Jan 16, 2004
    #4
  5. Garma

    Ron Natalie Guest

    "Garma" <> wrote in message news:GTGNb.44597$...
    > In C++ code of classes, would it be bad to use all those C-runtime
    > functions, for example, strcpy, strlen, strcat, etc.?


    No, what is bad style is using char* as a string type which it sucks
    badly at when you have std::string available.

    >
    > What are Standard libraries which have the above C functions? istringstream
    > and string?


    No the proper header is <cstring> (or string.h)


    > If using those C-runtime, does it mean that the code will also be linked to
    > C-runtime library and C++ libraries? Is there any disadvantage with this?


    They are required to work by the language. C++ includes the entire 1989
    C library. The C runtime support for that IS poart of the C++ library as
    far as the language is concerned.
    >
    > If a source code contains some classes that are not instantiated anywhere in
    > the code, will compilers automatically ignore them automatically?


    It doesn't "ignore" them (it has to read parse them), but they generally do not
    show up in the output unless they are used.
     
    Ron Natalie, Jan 16, 2004
    #5
  6. "Nick Hounsome" <> wrote in message
    news:xaQNb.2790$...
    >
    > "E. Robert Tisdale" <> wrote in message
    > news:...
    > > Garma wrote:
    > >
    > > > In C++ code of classes, would it be bad to use all those

    C-runtime
    > > > functions, for example, strcpy, strlen, strcat, etc.?

    > >
    > > Yes.

    >
    > No. It depends on what you are doing. If you have a C string then

    strlen is
    > the only reasonable way to get its length. Just be
    > sure you wouldn't be better off using std::string.


    How about std::char_traits<>::length? :)

    Jonathan
     
    Jonathan Turkanis, Jan 16, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Baxter

    style for coding latches

    Paul Baxter, Aug 10, 2003, in forum: VHDL
    Replies:
    7
    Views:
    1,163
    Mike Treseler
    Aug 15, 2003
  2. Willem Oosthuizen

    Coding style to prioritize certain inputs

    Willem Oosthuizen, Sep 2, 2003, in forum: VHDL
    Replies:
    5
    Views:
    508
    Mike Treseler
    Sep 4, 2003
  3. Analog Guy

    Coding style for CPLD vs FPGA

    Analog Guy, Mar 11, 2005, in forum: VHDL
    Replies:
    3
    Views:
    5,488
    Klaus Falser
    Mar 14, 2005
  4. calmar
    Replies:
    11
    Views:
    936
    calmar
    Feb 21, 2006
  5. Ken Varn
    Replies:
    0
    Views:
    505
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page