Thread: Superfluous characters after string -- pointer issues?

  1. #16
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    Quote Originally Posted by Salem View Post
    There is nothing wrong with your locally declared array of variable length, so long as the length is adequate.

    It certainly isn't a memory leak situation.

    Providing your length calculation is correct, strcpy() the first string, then strcat() the rest will be fine.
    The thing is, the one time I'm actually using the "n" functionality is with the first strncpy, which only copies up to the beginning of the basename.

    All the strncats can be replaced with strcat, however.

    So I guess I will need to manually append the end of string character after the strncpy.

    Am I right in understanding that strcpy and strcat DO append an end of string character, even though strncpy and strncat do not?


    Quote Originally Posted by tabstop View Post
    Because they're static variables and consequently get destroyed every time through the loop.
    That's what I thought...

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by marshaul View Post
    Am I right in understanding that strcpy and strcat DO append an end of string character, even though strncpy and strncat do not?
    Yes.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of repeatedly using strlen to obtain the length of a string whose length does not change, it would be more efficient and readable to cache the string length. This would also allow you to use strcpy instead of strcat to copy to the point in dest that has not yet been written to, thereby skipping over the effort strcat has to go through to find the null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > How can this not leak memory?
    Because it isn't a call to malloc.

    Well it might be a call to malloc, but it would be one the compiler generates, and it would be the compiler that would be responsible for calling free.

    > The thing is, the one time I'm actually using the "n" functionality is with the first strncpy
    You still need to take care of appending the \0 in the correct place then.

    Your destination array starts out containing garbage, so you need to make sure you always have a valid string (with it's \0) when you're building up your new string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    OK well this probably doesn't surprise most of you, but I appended a null character at the end of the string after using strncpy, and now everything works as expected.

    I just want to say that I really appreciate how helpful everyone on this forum is. I've browsed many a thread here, but this is the first I've posted, and I'm certainly glad I did!

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > How can this not leak memory?
    Because it isn't a call to malloc.
    I dunno... I'll have to write a test proggy and look at it in the debugger... but it seems to me that creating a runtime static array then creating a second one with the same name probably is creating orphaned blocks on the stack... It will be interesting to investigate.

    However; all doubt would be removed with a call to malloc in that spot instead of creating an array on the stack...

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    I dunno... I'll have to write a test proggy and look at it in the debugger... but it seems to me that creating a runtime static array then creating a second one with the same name probably is creating orphaned blocks on the stack... It will be interesting to investigate.
    If your investigation shows that that happens then you have a real live compiler bug to report

    Quote Originally Posted by C99 Clause 6.2.4 Paragraph 2 (part)
    The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime.
    Quote Originally Posted by C99 Clause 6.2.4 Paragraph 4
    An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration.
    Quote Originally Posted by C99 Clause 6.2.4 Paragraph 5 (part)
    For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way.
    Quote Originally Posted by C99 Clause 6.2.4 Paragraph 6
    For such an object that does have a variable length array type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration. (Note 27) If the scope is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate.
    Quote Originally Posted by C99 Clause 6.2.4 Note 27
    Leaving the innermost block containing the declaration, or jumping to a point in that block or an embedded block prior to the declaration, leaves the scope of the declaration.
    Quote Originally Posted by CommonTater
    However; all doubt would be removed with a call to malloc in that spot instead of creating an array on the stack...
    Not all doubt: you still need to check for a corresponding call to free (or otherwise). Also, it seems to me that variable length arrays are not required to be created on the stack.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  4. Replies: 8
    Last Post: 10-17-2005, 07:01 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM