a = new char[6] and yet a is much longer than 6?!?

This is a discussion on a = new char[6] and yet a is much longer than 6?!? within the C++ Programming forums, part of the General Programming Boards category; Hi people, Im making a text editor and I have a big problem, here is the code where I have ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    a = new char[6] and yet a is much longer than 6?!?

    Hi people, Im making a text editor and I have a big problem, here is the code where I have the problem
    Code:
    mytext = new char[count+lenght];
    I have debugged my program many times and Im sure that count == 0 and lenght == 6, yet mytext always ends up bigger and when I print it on the screen it ends up printing a lot of giberish thats shouldnt be there!

    Here is the .cpp file
    Code:
    #include "main.h"
    
    int main() {
    	Text paper;
    	char test[] = "Marcos";
    
    	paper.addtext(test, strlen(test));
    	paper.showtext();
    	return 0;
    }
    
    // Class Text
    Text::Text() {
    	mytext = new char[1];
    	mytext = NULL;
    	count = 0;
    }
    
    Text::~Text() {
    	delete[] mytext;
    }
    
    void Text::addtext(char *a, int lenght) {
    	char *temp; // temporarily hold what was already on mytext
    
    	if (mytext != NULL) {
    		temp = new char[count];
    	
    		strcpy(temp, mytext);
    	
    		delete[] temp;
    	} else {
    		temp = "";
    	}
    
    	delete[] mytext;
    
    	mytext = new char[count+lenght]; // make it long enough to hold what it had before + what we will add new
    	for (int i = 0; i < count; i++)
    		mytext[i] = temp[i]; // add what we had before
    
    	for (int i = 0; i < lenght; i++)
    		mytext[count+i] = a[i]; // add the new text after what we had before
    }
    
    void Text::deletetext() {
    }
    
    void Text::showtext() {
    	printf("%s", mytext);
    }
    Here is the .h file
    Code:
    #include <stdlib.h>
    #include <fstream>
    
    class Text {
    public:
    	Text();
    	~Text();
    
    	void addtext(char *, int);
    	void deletetext();
    	void showtext();
    private:
    	char *mytext;
    	int count;
    };
    Sorry for the trouble guys, but I really cant solve this.
    Why drink and drive when you can smoke and fly?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Long story short you did not allow for the null terminating character '\0'.
    Code:
    mytext = new char[count+lenght];
    maybe,
    Code:
    mytext = new char[(count+lenght)+1];
    and
    Code:
    	for (int i = 0; i < lenght; i++)
    		mytext[count+i] = a[i]; // add the new text after what 
    		mytext[lenght] = '\0';//add null terminating character

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thanks.
    Why drink and drive when you can smoke and fly?

  4. #4
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Hello Marcos,

    I just had a quick scan through your program, and from what I can see there are a few other issues besides what Anubis has mentioned.

    When you instantiate "Text", the internal variable "mytext" is assigned "new char[1]", then immediately assigned NULL, which means that you've leaked memory straight away.

    Next, when you call "addtext" on this object (ie. for the first time) "mytext" is NULL, and "temp" will be pointed at a static empty char buffer. Then, you "delete[] mytext", which will break because at this point "mytext" is NULL.

    You never update your internal "count" variable, which means when you repeatedly call the "addtext" function you'll never retain the text you had before.

    -- insert Anubis' post here --

    Have a go at fixing up these issues, and let us know how you go.

    Good luck

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Then, you "delete[] mytext", which will break because at this point "mytext" is NULL.
    Deleting a NULL pointer is a perfectly legal no-op.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Good point, I guess I'm still oldschool

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thanks for the interest people, I have done some work on it and now it works (bearly )
    This is what I have now in the .cpp file:
    Code:
    #include "main.h"
    
    int main() {
    	Text paper;
    	char buff[100];
    
    	while (1) {
    		scanf("%s", buff);
    		rewind(stdin);
    		if (!strcmp(buff, "-exit"))
    			break;
    		paper.addtext(buff, strlen(buff));
    	}
    
    	paper.savetofile();
    	return 0;
    }
    
    // Class Text
    Text::Text() {
    	mytext = new char;
    	mytext = NULL;
    	count = 0;
    }
    
    Text::~Text() {
    	delete[] mytext;
    }
    
    void Text::addtext(char *a, int lenght) {
    	char *temp;
    
    	if (mytext != NULL) {
    		temp = new char[count];
    
    		for (int i = 0; i < count; i++)
    			temp[i] = mytext[i];
    
    		temp[count] = NULL;
    	} else {
    		temp = "";
    	}
    
    	delete[] mytext;
    
    	mytext = new char[(count+lenght)+1];
    
    	for (int i = 0; i < count; i++)
    		mytext[i] = temp[i];
    
    	for (int i = 0; i < lenght; i++)
    		mytext[count+i] = a[i];
    
    	mytext[count+lenght] = NULL;
    
    	count += lenght;
    }
    
    void Text::deletetext() {
    }
    
    void Text::showtext() {
    	printf("%s", mytext);
    }
    
    void Text::savetofile() {
    	std::ofstream fout;
    
    	fout.open("Text.txt");
    	fout << mytext << "\n";
    	fout.close();
    }
    Im not really sure if this fixed the memory leak, (I dont quite understand them yet).
    Also, I was wondering what function I could use in main() to get a whole sentence because scanf() apearently stops reading after a space.
    Why drink and drive when you can smoke and fly?

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    fgets() reads until a newline of EOF or until a specified number of characters has been read in.

    edit:: This is C++, so you can use getline() and specify your own delimiter.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by Marcos
    Code:
    ...
    
    // Class Text
    Text::Text() {
        mytext = new char;  // Get rid of this line to fix memory leak
        mytext = NULL;
        count = 0;
    }
    
    ...
    Im not really sure if this fixed the memory leak, (I dont quite understand them yet).
    No, you haven't solved the memory leak, see above in red.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Ok, so I finally got rid of the memory leak.
    But I dont understand what I should put in the first parameter of getline() (_Istr).
    Can someone use it in an example?
    Why drink and drive when you can smoke and fly?

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by Marcos
    Ok, so I finally got rid of the memory leak.
    But I dont understand what I should put in the first parameter of getline() (_Istr).
    Can someone use it in an example?
    For your purposes, I think you would want:

    Code:
    cin.getline(buff,sizeof(buff),'\n');
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Yeah that worked great, thanks
    Why drink and drive when you can smoke and fly?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I guess I'm still oldschool
    Of what old school do you speak? Freeing a null pointer has always been a no-op, IIRC.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21