strncpy not that easy to use

Discussion in 'C Programming' started by smnoff, Aug 30, 2006.

  1. smnoff

    smnoff Guest

    How does( or should user use) strncpy() to allocate enough memory space for
    the destination string, s1?

    I guess I am having trouble using strncpy as it seems to be giving me
    errors.

    And just as well, I don't know the value of n until the program runs, so it
    needs to be dynamically allocated.

    Then again I hard coded n to be number like3 just to even see if that works.


    470 char *
    471 strncpy(char *s1, const char *s2, size_t n)
    472 {
    473 char *os1 = s1;
    474
    475 n++;
    476 while (--n != 0 && (*s1++ = *s2++) != '\0')
    477 ;
    478 if (n != 0)
    479 while (--n != 0)
    480 *s1++ = '\0';
    481 return (os1);
    482 }
    smnoff, Aug 30, 2006
    #1
    1. Advertising

  2. smnoff said:

    > How does( or should user use) strncpy() to allocate enough memory space
    > for the destination string, s1?


    strncpy doesn't allocate any space. It just copies and pads. If you are
    (rightly) concerned not to overflow your target buffer, strncpy is not
    sufficient. You must know how many bytes of data you need from the source
    string, and provide at least that many bytes (plus one for a null
    terminator, if relevant, which it nearly always is) for strncpy to use as a
    destination. If your real requirement is to copy the entire string, which
    it nearly always is, at that point you might just as well use strcpy.

    > I guess I am having trouble using strncpy as it seems to be giving me
    > errors.


    It is far more likely that /you/ are giving /it/ errors. If it really is
    necessary for you to use strncpy, which it hardly ever is, I suggest you
    show us your calling code, rather than your attempt at an implementation.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Aug 30, 2006
    #2
    1. Advertising

  3. "smnoff" <> writes:
    > How does( or should user use) strncpy() to allocate enough memory space for
    > the destination string, s1?
    >
    > I guess I am having trouble using strncpy as it seems to be giving me
    > errors.
    >
    > And just as well, I don't know the value of n until the program runs, so it
    > needs to be dynamically allocated.
    >
    > Then again I hard coded n to be number like3 just to even see if that works.
    >
    >
    > 470 char *
    > 471 strncpy(char *s1, const char *s2, size_t n)
    > 472 {
    > 473 char *os1 = s1;
    > 474
    > 475 n++;
    > 476 while (--n != 0 && (*s1++ = *s2++) != '\0')
    > 477 ;
    > 478 if (n != 0)
    > 479 while (--n != 0)
    > 480 *s1++ = '\0';
    > 481 return (os1);
    > 482 }


    You've quoted a particular implementation of strncpy(), but if you're
    going to use it you should base your usage on the documented semantics
    of the function, not on how it happens to be written in one particular
    implementation.

    You say it seems to be givin you errors, but you don't tell us what
    kind of errors, so there's not much we can do to help.

    In general, though, strncpy() isn't likely to be what you want to use.
    If the target array is too small, it may not be terminated with a '\0'
    character (i.e., it may not be a string); if it's larger than it needs
    to be, strncpy() pads it with additional '\0' characters, which is
    usually wasteful.

    It's usually better to use strcpy(), or perhaps memcpy() *after*
    making absolutely sure that the target array is big enough.

    If you have specific questions, post your actual code and tell us how
    it fails.

    --
    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.
    Keith Thompson, Aug 30, 2006
    #3
  4. smnoff

    smnoff Guest

    This code is part of a DLL that was created in Visual Studio and hence VS
    doesn't always give me the error message. This DLL is being called by an
    external program totally outside of Visual Studio.

    Actually, I really want to know how to make this dll code run entirely in
    Visual Studio so I can get the debugger to work fully as this is Visual
    Studio DLL project.

    Would I have to create a totally separate project to get all this debugging
    to work OR would I just get same unknown error messages I have been getting
    when I use the external program?




    "Keith Thompson" <> wrote in message
    news:...
    > "smnoff" <> writes:
    >> How does( or should user use) strncpy() to allocate enough memory space
    >> for
    >> the destination string, s1?
    >>
    >> I guess I am having trouble using strncpy as it seems to be giving me
    >> errors.
    >>
    >> And just as well, I don't know the value of n until the program runs, so
    >> it
    >> needs to be dynamically allocated.
    >>
    >> Then again I hard coded n to be number like3 just to even see if that
    >> works.
    >>
    >>
    >> 470 char *
    >> 471 strncpy(char *s1, const char *s2, size_t n)
    >> 472 {
    >> 473 char *os1 = s1;
    >> 474
    >> 475 n++;
    >> 476 while (--n != 0 && (*s1++ = *s2++) != '\0')
    >> 477 ;
    >> 478 if (n != 0)
    >> 479 while (--n != 0)
    >> 480 *s1++ = '\0';
    >> 481 return (os1);
    >> 482 }

    >
    > You've quoted a particular implementation of strncpy(), but if you're
    > going to use it you should base your usage on the documented semantics
    > of the function, not on how it happens to be written in one particular
    > implementation.
    >
    > You say it seems to be givin you errors, but you don't tell us what
    > kind of errors, so there's not much we can do to help.
    >
    > In general, though, strncpy() isn't likely to be what you want to use.
    > If the target array is too small, it may not be terminated with a '\0'
    > character (i.e., it may not be a string); if it's larger than it needs
    > to be, strncpy() pads it with additional '\0' characters, which is
    > usually wasteful.
    >
    > It's usually better to use strcpy(), or perhaps memcpy() *after*
    > making absolutely sure that the target array is big enough.
    >
    > If you have specific questions, post your actual code and tell us how
    > it fails.
    >
    > --
    > 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.
    smnoff, Aug 30, 2006
    #4
  5. "smnoff" <> writes:
    > This code is part of a DLL that was created in Visual Studio and hence VS
    > doesn't always give me the error message. This DLL is being called by an
    > external program totally outside of Visual Studio.
    >
    > Actually, I really want to know how to make this dll code run entirely in
    > Visual Studio so I can get the debugger to work fully as this is Visual
    > Studio DLL project.
    >
    > Would I have to create a totally separate project to get all this debugging
    > to work OR would I just get same unknown error messages I have been getting
    > when I use the external program?


    Please don't top-post. See <http://www.caliburn.nl/topposting.html>.

    If your IDE is hiding error messages from you, either find a way to
    get it to show them to you or get a better IDE. (I don't use IDEs
    myself, so perhaps I'm misunderstanding your situation.)

    We can help you with C programming language issues here. We can't
    help you with any problems you might be having with your IDE; you'll
    have to find a different forum for that.

    --
    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.
    Keith Thompson, Aug 30, 2006
    #5
  6. smnoff

    smnoff Guest

    What IDE do the users at this forum "generally" use?



    "Keith Thompson" <> wrote in message
    news:...
    > "smnoff" <> writes:
    >> This code is part of a DLL that was created in Visual Studio and hence VS
    >> doesn't always give me the error message. This DLL is being called by an
    >> external program totally outside of Visual Studio.
    >>
    >> Actually, I really want to know how to make this dll code run entirely in
    >> Visual Studio so I can get the debugger to work fully as this is Visual
    >> Studio DLL project.
    >>
    >> Would I have to create a totally separate project to get all this
    >> debugging
    >> to work OR would I just get same unknown error messages I have been
    >> getting
    >> when I use the external program?

    >
    > Please don't top-post. See <http://www.caliburn.nl/topposting.html>.
    >
    > If your IDE is hiding error messages from you, either find a way to
    > get it to show them to you or get a better IDE. (I don't use IDEs
    > myself, so perhaps I'm misunderstanding your situation.)
    >
    > We can help you with C programming language issues here. We can't
    > help you with any problems you might be having with your IDE; you'll
    > have to find a different forum for that.
    >
    > --
    > 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.
    smnoff, Aug 31, 2006
    #6
  7. "smnoff" <> writes:

    > "Keith Thompson" <> wrote in message
    > news:...
    >>
    >> Please don't top-post. See <http://www.caliburn.nl/topposting.html>.
    >>
    >> If your IDE is hiding error messages from you, either find a way to
    >> get it to show them to you or get a better IDE. (I don't use IDEs
    >> myself, so perhaps I'm misunderstanding your situation.)
    >>
    >> We can help you with C programming language issues here. We can't
    >> help you with any problems you might be having with your IDE; you'll
    >> have to find a different forum for that.
    >>


    Hmm. I'll just explicit-AOL Keith's post, and hope you get the message:

    Please don't top-post. See <http://www.caliburn.nl/topposting.html>.

    If your IDE is hiding error messages from you, either find a way to
    get it to show them to you or get a better IDE. (I don't use IDEs
    myself, so perhaps I'm misunderstanding your situation.)

    We can help you with C programming language issues here. We can't
    help you with any problems you might be having with your IDE; you'll
    have to find a different forum for that.

    Mee too!

    --
    Andrew Poelstra <http://www.wpsoftware.net/projects>
    To reach me by email, use `apoelstra' at the above domain.
    "Do BOTH ends of the cable need to be plugged in?" -Anon.
    Andrew Poelstra, Aug 31, 2006
    #7
  8. smnoff

    Ian Collins Guest

    smnoff wrote:
    > What IDE do the users at this forum "generally" use?
    >

    Did you read Keith's post?

    > "Keith Thompson" <> wrote in message
    > news:...
    >
    >>
    >>Please don't top-post. See <http://www.caliburn.nl/topposting.html>.
    >>


    --
    Ian Collins.
    Ian Collins, Aug 31, 2006
    #8
  9. "smnoff" <> writes:

    > What IDE do the users at this forum "generally" use?
    >


    This isn't relevant to the C language. Many of use don't use IDE's
    at all, and those who do may switch IDE's frequently, depending
    on what they want to do or where they are.

    I use emacs and gedit for all my code now, depending on whether my
    computer has a GUI.

    Also, you may find this interesting:
    http://www.charlespetzold.com/etc/DoesVisualStudioRotTheMind.html

    --
    Andrew Poelstra <http://www.wpsoftware.net/projects>
    To reach me by email, use `apoelstra' at the above domain.
    "Do BOTH ends of the cable need to be plugged in?" -Anon.
    Andrew Poelstra, Aug 31, 2006
    #9
  10. smnoff

    CBFalconer Guest

    *** Rude top-posting fixed ***
    smnoff wrote:
    > "Keith Thompson" <> wrote in message
    >> "smnoff" <> writes:
    >>
    >>> This code is part of a DLL that was created in Visual Studio and
    >>> hence VS doesn't always give me the error message. This DLL is
    >>> being called by an external program totally outside of Visual
    >>> Studio.
    >>>
    >>> Actually, I really want to know how to make this dll code run
    >>> entirely in Visual Studio so I can get the debugger to work
    >>> fully as this is Visual Studio DLL project.
    >>>
    >>> Would I have to create a totally separate project to get all this
    >>> debugging to work OR would I just get same unknown error messages
    >>> I have been getting when I use the external program?

    >>
    >> Please don't top-post. See <http://www.caliburn.nl/topposting.html>.
    >>
    >> If your IDE is hiding error messages from you, either find a way to
    >> get it to show them to you or get a better IDE. (I don't use IDEs
    >> myself, so perhaps I'm misunderstanding your situation.)
    >>
    >> We can help you with C programming language issues here. We can't
    >> help you with any problems you might be having with your IDE; you'll
    >> have to find a different forum for that.

    >
    > What IDE do the users at this forum "generally" use?


    Are you illiterate? Why do you top-post when you have just been
    politely asked to refrain. Why do you persist in asking about IDEs
    when you have just been told they are off-topic here? Why do you
    assume people generally use any IDE whatsoever, when they are
    basically just a nuisance?

    --
    Some informative links:
    news:news.announce.newusers
    http://www.geocities.com/nnqweb/
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
    CBFalconer, Aug 31, 2006
    #10
  11. smnoff wrote:
    > What IDE do the users at this forum "generally" use?
    >
    > "Keith Thompson" <> wrote in message
    > news:...
    > > "smnoff" <> writes:
    > >> This code is part of a DLL that was created in Visual Studio and hence VS
    > >> doesn't always give me the error message. This DLL is being called by an
    > >> external program totally outside of Visual Studio.
    > >>
    > >> Actually, I really want to know how to make this dll code run entirely in
    > >> Visual Studio so I can get the debugger to work fully as this is Visual
    > >> Studio DLL project.
    > >>
    > >> Would I have to create a totally separate project to get all this
    > >> debugging
    > >> to work OR would I just get same unknown error messages I have been
    > >> getting
    > >> when I use the external program?

    > >
    > > Please don't top-post. See <http://www.caliburn.nl/topposting.html>.
    > >
    > > If your IDE is hiding error messages from you, either find a way to
    > > get it to show them to you or get a better IDE. (I don't use IDEs
    > > myself, so perhaps I'm misunderstanding your situation.)
    > >
    > > We can help you with C programming language issues here. We can't
    > > help you with any problems you might be having with your IDE; you'll
    > > have to find a different forum for that.


    As Keith said, please don't top-post.

    I can't speak for other people here, but I use the same IDEs as Keith.
    J. J. Farrell, Aug 31, 2006
    #11
  12. smnoff

    smnoff Guest

    > Are you illiterate? Why do you top-post when you have just been
    > politely asked to refrain.



    Cause I didn't know what top-posting meant and all those who annoyed by it
    didn't
    provide a link to what "top-posting" was. That's WHY!

    If they wanted something to stop they should take the time to at least copy
    and paste a link to what they wanted to stop. Nobody knows what
    "top-posting" is as this is the ONLY group that has mentioned it. And by the
    way, that top-posting link is wrong, not a single newsgroup
    person/user/frequent poster has ever, repeat ever said that was a problem,
    It's just this group.
    smnoff, Aug 31, 2006
    #12
  13. smnoff

    smnoff Guest

    > As Keith said, please don't top-post.
    >
    > I can't speak for other people here, but I use the same IDEs as Keith.



    And by the way, lots of users of e-mail top-post.
    Was it easy to read other people's e-mail when I got started? NO it wasn't
    and they didn't give it a name either.
    But I had to get used to it.
    smnoff, Aug 31, 2006
    #13
  14. smnoff

    CBFalconer Guest

    smnoff wrote:
    >
    >> Are you illiterate? Why do you top-post when you have just been
    >> politely asked to refrain.

    >
    > Cause I didn't know what top-posting meant and all those who
    > annoyed by it didn't provide a link to what "top-posting" was.
    > That's WHY!
    >
    > If they wanted something to stop they should take the time to at
    > least copy and paste a link to what they wanted to stop. Nobody
    > knows what "top-posting" is as this is the ONLY group that has
    > mentioned it. And by the way, that top-posting link is wrong, not
    > a single newsgroup person/user/frequent poster has ever, repeat
    > ever said that was a problem, It's just this group.


    Both Keith Thompson and myself gave you the following link:

    <http://www.caliburn.nl/topposting.html>

    so that excuse is a bald faced lie. Some groups (mistakenly) will
    tolerate top-posting, but it is never correct. Also, don't remove
    attributions for material you quote. Attributions are the initial
    lines that say: "Joe Blow wrote:"

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE maineline address!
    CBFalconer, Aug 31, 2006
    #14
  15. "smnoff" <> writes:
    >> Are you illiterate? Why do you top-post when you have just been
    >> politely asked to refrain.

    >
    >
    > Cause I didn't know what top-posting meant and all those who annoyed by it
    > didn't
    > provide a link to what "top-posting" was. That's WHY!


    I did.

    --
    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.
    Keith Thompson, Aug 31, 2006
    #15
  16. smnoff

    smnoff Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "smnoff" <> writes:
    >>> Are you illiterate? Why do you top-post when you have just been
    >>> politely asked to refrain.

    >>
    >>
    >> Cause I didn't know what top-posting meant and all those who annoyed by
    >> it
    >> didn't
    >> provide a link to what "top-posting" was. That's WHY!

    >
    > I did.
    >
    > --
    > 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.



    It took you forever to give the link as well. But until you did, you didn't
    get what you wanted either.
    smnoff, Aug 31, 2006
    #16
  17. smnoff

    smnoff Guest

    "CBFalconer" <> wrote in message
    news:...
    > smnoff wrote:
    >>
    >>> Are you illiterate? Why do you top-post when you have just been
    >>> politely asked to refrain.

    >>
    >> Cause I didn't know what top-posting meant and all those who
    >> annoyed by it didn't provide a link to what "top-posting" was.
    >> That's WHY!
    >>
    >> If they wanted something to stop they should take the time to at
    >> least copy and paste a link to what they wanted to stop. Nobody
    >> knows what "top-posting" is as this is the ONLY group that has
    >> mentioned it. And by the way, that top-posting link is wrong, not
    >> a single newsgroup person/user/frequent poster has ever, repeat
    >> ever said that was a problem, It's just this group.

    >
    > Both Keith Thompson and myself gave you the following link:
    >
    > <http://www.caliburn.nl/topposting.html>
    >
    > so that excuse is a bald faced lie. Some groups (mistakenly) will
    > tolerate top-posting, but it is never correct. Also, don't remove
    > attributions for material you quote. Attributions are the initial
    > lines that say: "Joe Blow wrote:"
    >
    > --
    > Chuck F () ()
    > Available for consulting/temporary embedded and systems.
    > <http://cbfalconer.home.att.net> USE maineline address!
    >
    >



    It took you forever to give the link when it should have been done weeks
    ago. and if I didn't see it, you should have kept redrawing attention to it
    to begin with, period. So it's your fault, period.
    smnoff, Aug 31, 2006
    #17
  18. [Top-posting again]

    smnoff said:

    > It took you forever to give the link when it should have been done weeks
    > ago.


    No, it should never be necessary to give such a link, and it should never be
    necessary to explain to people how to use this group, because they're
    supposed to spend a few weeks reading it, so that they can learn by
    example.

    > and if I didn't see it, you should have kept redrawing attention to
    > it to begin with, period.


    No, it's not his job to teach you good manners and common sense. That's your
    responsibility, not his.

    > So it's your fault, period.


    No, it's yours.

    And now you have two choices - continue to be antagonistic towards the very
    people from whom you are seeking expert help, or wise up fast.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Aug 31, 2006
    #18
  19. smnoff said:

    > And by the way, lots of users of e-mail top-post.


    So what? The stupidity of other people does not mean that contributors to
    comp.lang.c have to be stupid too.

    > Was it easy to read other people's e-mail when I got started? NO it wasn't


    Then you should have complained at the time.

    > and they didn't give it a name either.
    > But I had to get used to it.


    No, you didn't. And you still don't.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Aug 31, 2006
    #19
  20. "smnoff" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> "smnoff" <> writes:
    >>>> Are you illiterate? Why do you top-post when you have just been
    >>>> politely asked to refrain.
    >>>
    >>>
    >>> Cause I didn't know what top-posting meant and all those who annoyed by
    >>> it
    >>> didn't
    >>> provide a link to what "top-posting" was. That's WHY!

    >>
    >> I did.

    >
    > It took you forever to give the link as well. But until you did, you didn't
    > get what you wanted either.


    What are you talking about? You posted Wednesday at 11:31:50 Pacific
    time. I posted a followup at 12:27:45, less than an hour later; in that
    followup, I wrote:

    | Please don't top-post. See <http://www.caliburn.nl/topposting.html>.

    In any case, you now know what top-posting is, and you know that it's
    discouraged in this newsgroup (as it is in most newsgroups).

    Shall we get back to discussing C?

    --
    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.
    Keith Thompson, Aug 31, 2006
    #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. Simon
    Replies:
    3
    Views:
    695
    Simon
    Sep 6, 2004
  2. Replies:
    4
    Views:
    1,120
    Dave Moore
    Feb 1, 2005
  3. Re: bizzare strncpy()

    , Jul 9, 2003, in forum: C Programming
    Replies:
    4
    Views:
    359
    Martin Ambuhl
    Jul 9, 2003
  4. Vijay Kumar R Zanvar

    Code Review: strncpy

    Vijay Kumar R Zanvar, Jan 14, 2004, in forum: C Programming
    Replies:
    30
    Views:
    3,973
  5. Ron
    Replies:
    9
    Views:
    609
    CBFalconer
    Mar 10, 2007
Loading...

Share This Page