strdup

Discussion in 'C Programming' started by klaushuotari@gmail.com, Mar 21, 2007.

  1. Guest

    Sorry to bother you, but I just have to.

    What about strdup()? It wasn't in standard C run-time library, yet
    many apps use it liberally as it was in there.

    I don't know if that particular function is included in C99. Not too
    difficult to code own version of it, but why it wasn't included in
    library in the first place? Is there some rationale behind it? There
    are peculiar and dangerous functions like strtok(), so why not
    strdup() which seems to be very useful function?

    What might be the reasons to exclude something that useful from
    library? And why C++ has the keyword "asm", but C doesn't? I don't
    understand.
    , Mar 21, 2007
    #1
    1. Advertising

  2. said:

    > Sorry to bother you, but I just have to.
    >
    > What about strdup()? It wasn't in standard C run-time library, yet
    > many apps use it liberally as it was in there.
    >
    > I don't know if that particular function is included in C99. Not too
    > difficult to code own version of it, but why it wasn't included in
    > library in the first place? Is there some rationale behind it?


    My guess would be that the ISO guys couldn't agree whether to put it in
    <string.h> (because it's a string function) or <stdlib.h> (because it's
    a function that allocates memory), so it died in committee.

    > And why C++ has the keyword "asm", but C doesn't?


    Because it's pointless, probably. Any embedded assembly language makes
    your code non-portable anyway, so you might as well just use a compiler
    extension for it.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Mar 21, 2007
    #2
    1. Advertising

  3. Guest

    Richard Heathfield kirjoitti:

    > said:
    >
    > > Sorry to bother you, but I just have to.
    > >
    > > What about strdup()? It wasn't in standard C run-time library, yet
    > > many apps use it liberally as it was in there.
    > >
    > > I don't know if that particular function is included in C99. Not too
    > > difficult to code own version of it, but why it wasn't included in
    > > library in the first place? Is there some rationale behind it?

    >
    > My guess would be that the ISO guys couldn't agree whether to put it in
    > <string.h> (because it's a string function) or <stdlib.h> (because it's
    > a function that allocates memory), so it died in committee.
    >
    > > And why C++ has the keyword "asm", but C doesn't?

    >
    > Because it's pointless, probably. Any embedded assembly language makes
    > your code non-portable anyway, so you might as well just use a compiler
    > extension for it.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at the above domain, - www.


    Probably so. But considering that C gets often labeled as "portable
    assembly" I would most definitely included possibility to drop to
    assembly level when coding in C. I know it argues against the paradigm
    of a portability, but nevertheless I would like the fact that the
    possibility to asm was inherent in C. I don't know... I just wonder
    that "asm" is a possibility in such a high-level lang such a C++, but
    not in C which gets used more often when programming toaster owens.
    Maybe it's good though. And anyways one could always call low-level
    routines in libraries. Maybe that's good.
    , Mar 21, 2007
    #3
  4. On 21 Mar 2007 15:02:46 -0700, klaushuotari@...com wrote:
    >What about strdup()? It wasn't in standard C run-time library, yet
    >many apps use it liberally as it was in there.
    >
    >I don't know if that particular function is included in C99.


    No, it isn't!

    >Not too
    >difficult to code own version of it, but why it wasn't included in
    >library in the first place? Is there some rationale behind it?


    Because the C Standard library doesn't include any functions that
    return allocated memory and oblige the user to call free. It's a
    simple but effective 'design pattern'.

    >What might be the reasons to exclude something that useful from
    >library?


    strdup() isn't "useful" for the mentioned reason.

    Best regards,
    Roland Pibinger
    Roland Pibinger, Mar 21, 2007
    #4
  5. wrote:
    > Richard Heathfield kirjoitti:
    > > said:
    > > > And why C++ has the keyword "asm", but C doesn't?

    > >
    > > Because it's pointless, probably. Any embedded assembly language
    > > makes your code non-portable anyway, so you might as well just
    > > use a compiler extension for it.


    Or an assembler!

    <snip unsnipped signature block>
    >
    > Probably so. But considering that C gets often labeled as
    > "portable assembly"


    A label that is taken too literally by many noobs. It's really
    statement implying that C is very low on the high level language
    ladder.

    > I would most definitely included possibility to drop to
    > assembly level when coding in C. I know it argues against the
    > paradigm of a portability,


    There's a difference between the portability of C programs and
    the portability of C. An asm keyword, and more importanty the
    additional syntax, likely implementation defined, yields no
    advantage for either.

    > but nevertheless I would like the fact that the
    > possibility to asm was inherent in C.


    Assembly was never inherent to C! Indeed the whole point of
    C was to _replace_ assembly. What typically happened was that
    you wrote critical code in assembler and simply linked them
    into your C code.

    > I don't know... I just wonder that "asm" is a possibility in
    > such a high-level lang such a C++,


    I'm sure most C++ implementors wonder why "asm" was in C++
    too. The majority of (rare) C or C++ code that uses assembly
    tend to use alternative syntax anyway!

    --
    Peter
    Peter Nilsson, Mar 21, 2007
    #5
  6. Flash Gordon Guest

    Roland Pibinger wrote, On 21/03/07 22:43:
    > On 21 Mar 2007 15:02:46 -0700, klaushuotari@...com wrote:
    >> What about strdup()? It wasn't in standard C run-time library, yet
    >> many apps use it liberally as it was in there.
    >>
    >> I don't know if that particular function is included in C99.

    >
    > No, it isn't!
    >
    >> Not too
    >> difficult to code own version of it, but why it wasn't included in
    >> library in the first place? Is there some rationale behind it?

    >
    > Because the C Standard library doesn't include any functions that
    > return allocated memory and oblige the user to call free. It's a
    > simple but effective 'design pattern'.


    What about malloc and realloc? You could call it stralloc if that would
    make you happier.

    >> What might be the reasons to exclude something that useful from
    >> library?

    >
    > strdup() isn't "useful" for the mentioned reason.


    There are times when it is incredibly useful. However, I'm not that
    bothered because I'm using wrapper functions for all allocation routines
    anyway, so implementing strdup (as ffstrdup) is no big deal to me.
    --
    Flash Gordon
    Flash Gordon, Mar 21, 2007
    #6
  7. In article <>,
    Roland Pibinger <> wrote:

    >>Not too
    >>difficult to code own version of it, but why it wasn't included in
    >>library in the first place? Is there some rationale behind it?


    >Because the C Standard library doesn't include any functions that
    >return allocated memory and oblige the user to call free.


    Can you point me to some reference showing that that was the reason?

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Mar 21, 2007
    #7
  8. "Roland Pibinger" <> wrote in message
    > Because the C Standard library doesn't include any functions that
    > return allocated memory and oblige the user to call free. It's a
    > simple but effective 'design pattern'.
    >

    But fopen() requires a matching fclose().

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Mar 21, 2007
    #8
  9. Old Wolf Guest

    On Mar 22, 10:43 am, (Roland Pibinger) wrote:
    > Because the C Standard library doesn't include any functions that
    > return allocated memory and oblige the user to call free.


    Actually, it includes three such functions.
    Old Wolf, Mar 21, 2007
    #9
  10. writes:
    > Richard Heathfield kirjoitti:
    >> said:

    [...]
    >> > And why C++ has the keyword "asm", but C doesn't?

    >>
    >> Because it's pointless, probably. Any embedded assembly language makes
    >> your code non-portable anyway, so you might as well just use a compiler
    >> extension for it.

    >
    > Probably so. But considering that C gets often labeled as "portable
    > assembly" I would most definitely included possibility to drop to
    > assembly level when coding in C. I know it argues against the paradigm
    > of a portability, but nevertheless I would like the fact that the
    > possibility to asm was inherent in C. I don't know... I just wonder
    > that "asm" is a possibility in such a high-level lang such a C++, but
    > not in C which gets used more often when programming toaster owens.
    > Maybe it's good though. And anyways one could always call low-level
    > routines in libraries. Maybe that's good.


    Please don't quote signatures.

    Yes, C is often called a portable assembler -- but it isn't.

    Compiler implementers are perfectly free to provide inline assembly as
    an extension. It just doesn't matter whether the standard reserves an
    "asm" keyword for this purpose. Probably C++ has it because it was
    originally based on an earlier version of C; both editions of K&R
    mention that some implementations reserve the words "asm" and
    "fortran".

    <OT>
    C++ even defines a construct for inline assembly:

    asm ( string-literal ) ;

    But the contents of the string-literal, and the effect of the
    construct, are entirely implementation-defined.
    </OT>

    Since there's no guarantee that any code using "asm" will be even
    vaguely portable, what is the advantage of reserving a keyword, as
    opposed to, say, using some identifier from the implementation
    namespace like __ASM__, or using a "#pragma"?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 22, 2007
    #10
  11. Guest

    On 22 maalis, 02:52, Keith Thompson <> wrote:
    > writes:
    > > Richard Heathfield kirjoitti:
    > >> said:

    > [...]
    > >> > And why C++ has the keyword "asm", but C doesn't?

    >
    > >> Because it's pointless, probably. Any embedded assembly language makes
    > >> your code non-portable anyway, so you might as well just use a compiler
    > >> extension for it.

    >
    > > Probably so. But considering that C gets often labeled as "portable
    > > assembly" I would most definitely included possibility to drop to
    > > assembly level when coding in C. I know it argues against the paradigm
    > > of a portability, but nevertheless I would like the fact that the
    > > possibility to asm was inherent in C. I don't know... I just wonder
    > > that "asm" is a possibility in such a high-level lang such a C++, but
    > > not in C which gets used more often when programming toaster owens.
    > > Maybe it's good though. And anyways one could always call low-level
    > > routines in libraries. Maybe that's good.

    >
    > Please don't quote signatures.
    >
    > Yes, C is often called a portable assembler -- but it isn't.
    >
    > Compiler implementers are perfectly free to provide inline assembly as
    > an extension. It just doesn't matter whether the standard reserves an
    > "asm" keyword for this purpose. Probably C++ has it because it was
    > originally based on an earlier version of C; both editions of K&R
    > mention that some implementations reserve the words "asm" and
    > "fortran".
    >
    > <OT>
    > C++ even defines a construct for inline assembly:
    >
    > asm ( string-literal ) ;
    >
    > But the contents of the string-literal, and the effect of the
    > construct, are entirely implementation-defined.
    > </OT>
    >
    > Since there's no guarantee that any code using "asm" will be even
    > vaguely portable, what is the advantage of reserving a keyword, as
    > opposed to, say, using some identifier from the implementation
    > namespace like __ASM__, or using a "#pragma"?
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > "We must do something. This is something. Therefore, we must do this."
    > -- Antony Jay and Jonathan Lynn, "Yes Minister"


    Good points there. What is the importance of portability? Not every
    platform will be the same. Some new platforms will demand new
    operators, new shift in thinking. Future computing power will demand
    whole new approach to programming. Soon we will have parallel
    computing coupled with neural networks or something like that (I'm no
    expert). What is the position of C? How to efficiently model real
    world problems with some programming language? I don't know, but at
    the moment I'm perfectly happy with C. It has passed the test of time.
    1 + 1 = 3.
    , Mar 22, 2007
    #11
  12. writes:
    > On 22 maalis, 02:52, Keith Thompson <> wrote:

    [...]
    >> Please don't quote signatures.

    [...]
    >> --
    >> Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    >> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    >> "We must do something. This is something. Therefore, we must do this."
    >> -- Antony Jay and Jonathan Lynn, "Yes Minister"


    The stuff following the "-- " delimiter, which I've left in place
    above, is called a "signature". When you post a followup, please
    don't include the signature, or anything else not relevant to your
    followup, in the quoted material. Quote just enough so that your
    followup makes sense to someone who hasn't seen the parent article.

    > Good points there. What is the importance of portability? Not every
    > platform will be the same. Some new platforms will demand new
    > operators, new shift in thinking. Future computing power will demand
    > whole new approach to programming. Soon we will have parallel
    > computing coupled with neural networks or something like that (I'm no
    > expert). What is the position of C? How to efficiently model real
    > world problems with some programming language? I don't know, but at
    > the moment I'm perfectly happy with C. It has passed the test of time.
    > 1 + 1 = 3.


    Portability can be important, unimportant, or impossible, depending on
    the application. Whether it's important or not, the "asm" keyword
    doesn't achieve it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 22, 2007
    #12
  13. Guest


    > Portability can be important, unimportant, or impossible, depending on
    > the application. Whether it's important or not, the "asm" keyword
    > doesn't achieve it.
    >


    The more I think of that the more I believe that's true. So should
    "asm" be a no-no when thinking about new keywords included in the
    language C?
    , Mar 22, 2007
    #13
  14. CBFalconer Guest

    Richard Heathfield wrote:
    > said:
    >

    .... snip ...
    >>
    >> What about strdup()? It wasn't in standard C run-time library,
    >> yet many apps use it liberally as it was in there.
    >>

    .... snip ...
    >
    > My guess would be that the ISO guys couldn't agree whether to put
    > it in <string.h> (because it's a string function) or <stdlib.h>
    > (because it's a function that allocates memory), so it died in
    > committee.


    The story I heard is that there was a bias against 'hidden'
    mallocs, which could lead to memory leaks. I don't subscribe to
    that particular bias.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 22, 2007
    #14
  15. CBFalconer Guest

    wrote:
    > On 22 maalis, 02:52, Keith Thompson <> wrote:
    > > writes:
    > > > Richard Heathfield kirjoitti:
    > > >> said:

    > > [...]
    > > >> > And why C++ has the keyword "asm", but C doesn't?

    > >
    > > >> Because it's pointless, probably. Any embedded assembly language makes
    > > >> your code non-portable anyway, so you might as well just use a compiler
    > > >> extension for it.

    > >
    > > > Probably so. But considering that C gets often labeled as "portable
    > > > assembly" I would most definitely included possibility to drop to
    > > > assembly level when coding in C. I know it argues against the paradigm
    > > > of a portability, but nevertheless I would like the fact that the
    > > > possibility to asm was inherent in C. I don't know... I just wonder
    > > > that "asm" is a possibility in such a high-level lang such a C++, but
    > > > not in C which gets used more often when programming toaster owens.
    > > > Maybe it's good though. And anyways one could always call low-level
    > > > routines in libraries. Maybe that's good.

    > >
    > > Please don't quote signatures.
    > >
    > > Yes, C is often called a portable assembler -- but it isn't.
    > >
    > > Compiler implementers are perfectly free to provide inline assembly as
    > > an extension. It just doesn't matter whether the standard reserves an
    > > "asm" keyword for this purpose. Probably C++ has it because it was
    > > originally based on an earlier version of C; both editions of K&R
    > > mention that some implementations reserve the words "asm" and
    > > "fortran".
    > >
    > > <OT>
    > > C++ even defines a construct for inline assembly:
    > >
    > > asm ( string-literal ) ;
    > >
    > > But the contents of the string-literal, and the effect of the
    > > construct, are entirely implementation-defined.
    > > </OT>
    > >
    > > Since there's no guarantee that any code using "asm" will be even
    > > vaguely portable, what is the advantage of reserving a keyword, as
    > > opposed to, say, using some identifier from the implementation
    > > namespace like __ASM__, or using a "#pragma"?
    > >
    > > --
    > > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > > "We must do something. This is something. Therefore, we must do this."
    > > -- Antony Jay and Jonathan Lynn, "Yes Minister"

    >
    > Good points there. What is the importance of portability? Not every
    > platform will be the same. Some new platforms will demand new
    > operators, new shift in thinking. Future computing power will demand
    > whole new approach to programming. Soon we will have parallel
    > computing coupled with neural networks or something like that (I'm no
    > expert). What is the position of C? How to efficiently model real
    > world problems with some programming language? I don't know, but at
    > the moment I'm perfectly happy with C. It has passed the test of time.
    > 1 + 1 = 3.


    I am quoting the whole schmeer to make a point. If you read Keiths
    article, why did you continue to quote the sig? Why did you fail
    to snip other irrelevant material?

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 22, 2007
    #15
  16. writes:
    >> Portability can be important, unimportant, or impossible, depending on
    >> the application. Whether it's important or not, the "asm" keyword
    >> doesn't achieve it.
    >>

    >
    > The more I think of that the more I believe that's true. So should
    > "asm" be a no-no when thinking about new keywords included in the
    > language C?


    Please don't snip attribution lines (lines of the form "So-and-so writes:").

    In my opinion, yes, adding an "asm" keyword to C would be a bad idea.


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 22, 2007
    #16
  17. Richard Bos Guest

    wrote:

    > Richard Heathfield kirjoitti:
    >
    > > said:
    > >
    > > > What about strdup()? It wasn't in standard C run-time library, yet
    > > > many apps use it liberally as it was in there.
    > > >
    > > > I don't know if that particular function is included in C99. Not too
    > > > difficult to code own version of it, but why it wasn't included in
    > > > library in the first place? Is there some rationale behind it?

    > >
    > > My guess would be that the ISO guys couldn't agree whether to put it in
    > > <string.h> (because it's a string function) or <stdlib.h> (because it's
    > > a function that allocates memory), so it died in committee.


    Also _because_ it is so easy to write yourself.

    > > > And why C++ has the keyword "asm", but C doesn't?

    > >
    > > Because it's pointless, probably. Any embedded assembly language makes
    > > your code non-portable anyway, so you might as well just use a compiler
    > > extension for it.

    >
    > Probably so. But considering that C gets often labeled as "portable
    > assembly"


    Only by people who know neither C nor assembly very well.

    > I would most definitely included possibility to drop to
    > assembly level when coding in C. I know it argues against the paradigm
    > of a portability, but nevertheless I would like the fact that the
    > possibility to asm was inherent in C.


    How would you write a C interpreter if you had to allow for assembly?
    (Come to think of it, how does a C++ interpreter handle this? Or are C++
    interpreters not allowed?)

    Richard
    Richard Bos, Mar 22, 2007
    #17
  18. On 21 Mar 2007 16:22:53 -0700, "Old Wolf" wrote:
    >On Mar 22, 10:43 am, (Roland Pibinger) wrote:
    >> Because the C Standard library doesn't include any functions that
    >> return allocated memory and oblige the user to call free.

    >
    >Actually, it includes three such functions.


    ;-)
    Roland Pibinger, Mar 22, 2007
    #18
  19. On 21 Mar 2007 23:12:59 GMT, Richard Tobin wrote:
    >Roland Pibinger wrote:
    >>Because the C Standard library doesn't include any functions that
    >>return allocated memory and oblige the user to call free.

    >
    >Can you point me to some reference showing that that was the reason?


    No, but try to find an example in K&R that forces the caller to 'free'
    the returned object.

    Best regards,
    Roland Pibinger
    Roland Pibinger, Mar 22, 2007
    #19
  20. On Wed, 21 Mar 2007 23:19:40 -0000, "Malcolm McLean" wrote:
    >"Roland Pibinger" wrote in message
    >> Because the C Standard library doesn't include any functions that
    >> return allocated memory and oblige the user to call free. It's a
    >> simple but effective 'design pattern'.
    >>

    >But fopen() requires a matching fclose().


    Yes, you (not some other function) call fopen() and then fclose(). Who
    allocates also de-allocates. That's the simple 'design pattern'.

    Best wishes,
    Roland Pibinger
    Roland Pibinger, Mar 22, 2007
    #20
    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. Allan Bruce

    strdup in c++

    Allan Bruce, Sep 18, 2003, in forum: C++
    Replies:
    39
    Views:
    31,653
    Gianni Mariani
    Sep 21, 2003
  2. Stefan Schwärzler

    strdup in Borland C++Builder

    Stefan Schwärzler, Sep 10, 2004, in forum: C++
    Replies:
    6
    Views:
    641
    Ivan Vecerina
    Sep 12, 2004
  3. Christopher Benson-Manica

    strdup

    Christopher Benson-Manica, Sep 19, 2003, in forum: C Programming
    Replies:
    11
    Views:
    1,157
    Simon Biber
    Sep 20, 2003
  4. Robin Becker

    BUG? PyMem_Free vs strdup

    Robin Becker, Nov 5, 2003, in forum: Python
    Replies:
    4
    Views:
    446
    Christos TZOTZIOY Georgiou
    Nov 5, 2003
  5. Gerhard Haering
    Replies:
    6
    Views:
    929
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Dec 23, 2004
Loading...

Share This Page