Thread: Manipulating private individual char elements

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    43

    Manipulating private individual char elements

    Hi everybody :-) this is my first post... I'm hoping to learn C++ so that I can change my career and get out of the crappy Logistics industry!! I'm a total programming noob (C++ is my first language I'm learning) and I'm a little stuck.

    Just as an exercise I'm trying to manipulate individual elements of a private char array (using the member function setChar here). This is my code:

    insert
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Cat
    {
    public:
    	Cat() {itsAge = new int; *itsAge=3; itsName = new char[10];}
    	Cat(int age) {itsAge = new int; *itsAge = age; new char[10];}
    	~Cat () {delete itsAge; itsAge = 0; delete itsName; itsName = 0;}
    	int getAge () const {return *itsAge;}
    	char* getName () {return itsName;} 	
            char getChar (unsigned short offset) {return itsName[offset];}
    	void setChar (int x) {itsName[x] = 'J';}
    	void setName (char* NewName) {itsName = NewName;}
    private:
    	int *itsAge;
    	char *itsName;
    };
    
    int main ()
    {
    	Cat Furball;
    	Furball.setName("Furball");
    	cout << Furball.getName() << "\n";
    	cout << Furball.getChar(0) << "\n";
    	Furball.setChar(0);
    	cout << Furball.getChar(0) << "\n";
    
    	return 0;
    }
    That which I've highlighted in bold, the setChar function, is what is causing this basic program to crash.

    Can anyone help before windows lives up to its name and get's thrown out of my living room one?!!

    Cheers :-)

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your setName() method needs to copy the name, not just point the pointer somewhere else. You've pointed the pointer at a string literal, then you try to modify it, which explodes.

    There's also a typo in your second Cat() constructor.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For one thing, you should use delete[] to match with new[]. For another, since you are dealing with a C-style string, it should be null terminated so that you can print it correctly (among other things).

    In the future, you should also implement the copy constructor and copy assignment operator or declare them private. However, apply my first two suggestions first and see if they has anything to do with fixing the problem.

    EDIT:
    Oh yes, brewbuck pointed out what appears to be the most pertinent problem at the moment.
    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. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you been using java before you started doing C++?

    Doing "new" on a single integer is completely pointless and serves no purpose other than making everything much harder. I personally wouldn't allocate memory for a 10 byte string either - just have a fixed size array in the class itself. [Of course, you then overwrite the name with a pointer, but you are supposed to fix that.]

    Code:
    Cat(int age) {itsAge = new int; *itsAge = age; new char[10];}
    The red bit allocated 10 bytes of memory, then immediately looses control of it. That's a memory leak if there ever was one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Any reason you aren't using std::string?
    Also, don't write a lot of statements in just one row.
    Break them into several lines. It hurts readability otherwise (might just be why none before matsp picked up your memory leak mistake).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM