CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2010
    Posts
    16

    ***glibc detected***

    I have been digging through my code for the past two days and I cannot seem to nail down the source of my problems. you will have to excuse the mess of all the std::cout calls. I was trying to narrow down where my problems were coming from, though i dont think it seemed to help all that much.

    Its a basic string class, I have included two .cpp files one .h file and a txt file. the program reads data from the txt file. The first two numbers represent the left and right column, the program will then need to left-right justify the text adding spaces between words to make them fill the width. Im pretty sure my justify algorithm is correct but to be honest ive been staring at this code so long its all starting look the same....

    You guys have been a great help before, any help on this would be greatly appreciated.

    EDIT: sorry forgot to mention the errors, ive been getting glibc detected errors at random spots (or what seems to be random- each time I change something the point of error during excecution seems to change) sometimes they were free(): invalid nSegmentation fault, or free(): invalid next size (fast), or malloc(): memory corruption (fast):
    Attached Files Attached Files
    Last edited by originalmoose; November 9th, 2010 at 11:14 PM.

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: ***glibc detected***

    Personally I would set aside what you have done and start again. Make your class small with a complete yet minimal interface. Almost all operators can then be written as non-members. A few of the functions you provide would probably be better as non-members too.
    Maybe prefer const char* to const char[] as arrays are not garaunteed to be null terminated yet your code will fail horribly if they are not. Theres no need for the int constructor either as its only doing what the default constructor is doing.
    I would start with default constructor, copy constructor, assignment operator and destructor. Write some test code to test the class at this point. If these functions dont work, no point going further. Then add other constructors and their tests and test again. Then add the interface and implement it testing each step as you write them. You will find your own errors and learn from your mistakes. Finally add the operators and non-member functions and test those too.
    Above all else learn how to use the debugger to examine variables while your code is executing so that you can be sure that what you intend to happen is actually what is happening.

    Read this before you start.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  3. #3
    Join Date
    Aug 2010
    Posts
    16

    Re: ***glibc detected***

    thats what i was afraid i was going to have to do. The reason the code ended up the way it currently is, the assignment we have for my class has us building the class in segments and the way I have it written is a cleaned up version of what the requirements are for the assignment. Though im getting so fed up im going to take your advice and rewrite the whole class on Thursday since I have the day off for veterans day. especially since i need the class working flawlessly for our next assignment

    Thanks for your input and the helpful link

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: ***glibc detected***

    I haven't looked at your code, but there's one very important thing to keep in mind when writing any string class: the manipulation of the dynamic char array is probably the hardest part to get right, so why not encapsulate that within *another* class, say DynamicCharArray? Make sure you include the copy constructor and operator=, and thoroughly test that class first. Once it's working, then make a DynamicCharArray a member of your String class, and write the String-specific functionality to the DCA interface.

    It's a general rule of programming----encapsulate the difficult part of the code as tightly as possible, to make the remainder of the code easier to get right.

  5. #5
    Join Date
    Aug 2010
    Posts
    16

    Re: ***glibc detected***

    Quote Originally Posted by Lindley View Post
    I haven't looked at your code, but there's one very important thing to keep in mind when writing any string class: the manipulation of the dynamic char array is probably the hardest part to get right, so why not encapsulate that within *another* class, say DynamicCharArray? Make sure you include the copy constructor and operator=, and thoroughly test that class first. Once it's working, then make a DynamicCharArray a member of your String class, and write the String-specific functionality to the DCA interface.

    It's a general rule of programming----encapsulate the difficult part of the code as tightly as possible, to make the remainder of the code easier to get right.
    That seems like a pretty good idea as well, thanks for the tips guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured