Thread: Dynamic Memory Allocation Problems

  1. #16
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    You haven't added a null character after the end of your word in backwards (even though you have allocated space for it) so strlen() doesn't know where it ends.
    Last edited by Golf7; 09-22-2015 at 09:36 AM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, "stringManipulation" is a vague name for your function. As it seems that you want to print the string backwards, give it a name along those lines.
    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

  3. #18
    Registered User
    Join Date
    Jun 2015
    Posts
    19
    Quote Originally Posted by Golf7 View Post
    You haven't added a null character after the end of your word in backwards (even though you have allocated space for it) so strlen() doesn't know where it ends.
    Thank you very much, Golf

    It was a very dumb mistake. Thank you!

  4. #19
    Registered User
    Join Date
    Jun 2015
    Posts
    19
    Thank you, laserlight, I have since revised the name to revereString.

  5. #20
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    In case you were confused by always getting 8 from sizeof(backwards), remember that this will give you the size of a char pointer and not the size of the memory you allocated with malloc (which backwards points to).
    Last edited by Golf7; 09-22-2015 at 11:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By slash.hack in forum C Programming
    Replies: 9
    Last Post: 09-30-2011, 04:31 AM
  2. Memory dynamic allocation
    By elvio.vicosa in forum C Programming
    Replies: 8
    Last Post: 10-16-2007, 03:30 AM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 02:07 AM