Manipulating private individual char elements

This is a discussion on Manipulating private individual char elements within the C++ Programming forums, part of the General Programming Boards category; Hi everybody :-) this is my first post... I'm hoping to learn C++ so that I can change my career ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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
    21,415
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    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, 04: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, 11:25 AM
  4. public function returning const char * of private member
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2002, 03:28 PM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 06:56 AM

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