Thread: Having a problem with string, not sure where to look

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    Having a problem with string, not sure where to look

    I'm just messing around a little bit, trying to understand pointers and where they are used in C++ to increase program speed and efficiency. Basically trying to compare similar algorithms in C++ using pointers with their C#/Java counterpart.

    My problem is when I'm attempting to print out the Book title from the array. I'm guessing I'm doing something wrong when using the string library. So, here is what I've got right now:

    Book.h
    Code:
    #pragma once
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Book
    {
    public:
    	Book(void);
    	~Book(void);
    
    	string getTitle();
    	void setTitle(string title);
    
    	int getReleaseYear();
    	void setReleaseYear(int releaseYear);
    private:
    	string title;
    	int releaseYear;
    };
    Book.cpp
    Code:
    #include "Book.h"
    
    Book::Book(void) {}
    Book::~Book(void) {}
    
    string Book::getTitle()
    {
    	return Book::title;
    }
    
    void Book::setTitle(string title)
    {
    	Book::title = title;
    }
    
    int Book::getReleaseYear()
    {
    	return Book::releaseYear;
    }
    
    void Book::setReleaseYear(int releaseYear)
    {
    	Book::releaseYear = releaseYear;
    }
    CppArray.cpp
    Code:
    #include <iostream>
    #include "Book.h"
    
    using namespace std;
    
    int main()
    {
    	const int size = 10;
    	Book *b;
    	Book books[size];
    
    	for( int i = 0; i < size; i++ )
    	{
    		b = &books[i];
    		b->setTitle("Title " + i);
    		b->setReleaseYear(i);
    	}
    
    	for( int x = 0; x < size; x++ )
    	{
    		b = &books[x];
    		cout << "Title: " << b->getTitle() << "\t" << "Release Year: " << b->getReleaseYear() << "\n";
    	}
    
    	delete b;
    
    	cin.get();
    	return 0;
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    This one was funny

    Code:
    b->setTitle("Title " + i);
    "Title " is a char* and the compiler, instead of appending the integer i to it, did pointer arithmatic on it. New code:

    Code:
    	...
    	char titleBuf[32];
    	Book *b;
    	Book books[size];
    
    	for( int i = 0; i < size; i++ )
    	{
    		b = &books[i];
    		sprintf(titleBuf, "Title %d", i);
    		b->setTitle(titleBuf);
    		b->setReleaseYear(i);
    	}
    	...
    Also,

    Code:
    delete b;
    'b' wasn't dynamically allocated in the first place so this is unnecessary. I know you were just looking into pointers and things, but all that is really needed for these things is just like

    Code:
    books[x].getTitle()
    books[i].setReleaseYear(..)
    Edit: instead of s[n]printf, you could also use C++ stringstream's to convert the integer to a string.

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Its not Book::variable, its this.variable (or this->variable for a pointer) if you're refering to a variable of that object. this is just a reference to the object you're in.

    Thats probably where your source of error is coming from because at a glance the loop concept looks fine. Accept that b isnt initialized.

    You dont need void in empty brackets. I see that sometimes, anyone mind explaining why?

    Of course it would just be easier to do this, haha..

    Code:
    	const int size = 10;
    	Book* books = new Book[size];
    
    	int i;
    	for(i = 0; i < size; i++)
    	{
    		books[i].setTitle("Title " + i);
    		books[i].setReleaseYear(i);
    	}
    
    	for(i = 0; i < size; i++)
    	{
    		cout << "Title: " << books[1].getTitle() << "\t" << "Release Year: " << books[1].getReleaseYear() << endl;
    	}
    
    	delete[] books;
    Edit: Are those 1's in your second loop, should be i's I suppose.
    Last edited by Dae; 11-25-2005 at 09:03 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Haha, funny!

    Yeah, I figured I could simply call the method directly from the array spot, but like you said, I'm just messing around with pointers. I'm having a hard time finding a good example of their performance increase.

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by CompiledMonkey
    Haha, funny!

    Yeah, I figured I could simply call the method directly from the array spot, but like you said, I'm just messing around with pointers. I'm having a hard time finding a good example of their performance increase.
    Imagine instead of passing references to objects in Java, you were passing all the data. Theres your performance increase.

    In java all objects are passed by reference, in C++ we are given the option to not pass by reference, or to use pointers; or references, we have those too, lol.

    How fun would linked lists be without pointers? or references. I don't want to imagine it.

    new also allocates on the stack I think, which could possibly be faster.
    Last edited by Dae; 11-25-2005 at 09:12 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    So, in your example above, what's the point of creating the book array as a pointer array?

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    @Dae:
    Code:
    	Book* books = new Book[size];
    	...
    		books[i].setTitle("Title " + i);
    		books[i].setReleaseYear(i); // damnit it thinks [i] is italicize! there
    		... etc etc ...
    Now they should be ->'s

    >> I'm having a hard time finding a good example of their performance increase.

    As Dae noted, pointers are very useful in passing data around in your program. It takes a whole bunch of energy to pick the whole thing up and give it to the function, so it's better just to say, "it's over there" and point to where the data is, so that it can do further manipulation on it (Google pass by reference/pass by pointer).

    >> new also allocates on the stack I think, which could possibly be faster.

    Nah, it allocates memory on the heap which is not necessarily faster, but it is dynamic. Edit:

    >> So, in your example above, what's the point of creating the book array as a pointer array?

    In his example there is none. But say you wanted to make a library class with a scalable number of books that the client would specify. Say like:

    Code:
    class Library
    {
    public:
    	Library(int numBooks);
    	~Library();
    private:
    	Book* books;
    };
    
    Library::Library(int numBooks)
    {
    	books = new Book[numBooks];
    }
    Library::~Library()
    {
    	if(books) delete[] books;
    }
    This is a pretty bad example and something like an std::vector would be more appropriate.
    Last edited by Tonto; 11-25-2005 at 09:23 PM.

  8. #8
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    If the books variable wasn't declared as a pointer, could you still use delete on it?

  9. #9
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    A good example of where you would want to use an array of pointers is say a stack data structure. If theres something like 10 spots on the stack, it would be more efficient to simply assign the pointer to the object, and not deep copy the object.

    ie. stackslot1 = object3304 is 100x less efficient than stackslot1 = &object3304.

    Code:
    Now they should be ->'s
    There was actually a [ i ] on the first one, but the bbcode keeps screwing those up. Are you positive they should be ->'s and not .'s? I recently thought so but I had problems with errors (22 C:\Programming\Projects\Game\asdasdasd.cpp base operand of `->' has non-pointer type `Book' ) doing so, so talking with someone we came to the idea that [] is dereferencing the pointer, so instead of (books+1)-> you would use books[1]., and since I'm a newb I just think of it like theres 1 pointer and n objects so of course you cant treat books[i] like its a pointer because theres only 1, and not n amount of pointers.
    Last edited by Dae; 11-25-2005 at 09:40 PM. Reason: forgot to make it 1 as the element
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by CompiledMonkey
    If the books variable wasn't declared as a pointer, could you still use delete on it?
    Um, no I dont think so.. new and delete go hand in hand.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Quote Originally Posted by Dae
    Um, no I dont think so.. new and delete go hand in hand.
    But, you don't need a pointer to use new, right?

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You are definitely right. "woops".

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    >> I'm having a hard time finding a good example of their performance increase.

    Then let's perform a thought experiment. Imagine you have a class called HistoryOfTheWorld. It has 40,000 data members, and the class has 100,000 different member functions. You can create an object of your class like this:

    HistoryOfTheWorld x;

    Now suppose you want to send your object to a non-member function you defined called print(), which displays the history of the world to the screen:
    Code:
    void print(HistoryOfTheWorld obj)
    {
    	...
    }
    To call your function, you would do this:

    print(x);

    Now, what happens when you do that? You are passing x by value, so a copy of x is made for the function, and then the copy is assigned to the function parameter obj. The function then uses the copy to perform its tasks. Well, as you can imagine copying an object with 40,000 variables and 100,000 member functions is going to take a lot of memory and some time.

    If instead you defined your function with a pointer parameter:
    Code:
    void print(HistoryOfTheWorld* pHistory)
    {
    	...
    }
    You could call your function like this:
    Code:
    print(&x);  //sends the address of x to the function print
    Just like before a copy of the argument is made for the function. However, in this case the argument is an address, and an address looks something like "AB80009F". Copying an address takes very little memory and occurs almost instantaneously.

    In Java, every object name is a pointer to an object, which Java calls a "reference to the object". So, when you pass an object to a function in Java, you are passing a "reference to the object" which is an address, and the address is what gets copied for the function to use. In Java, you can't pass an object "by value" as you can in C++.
    Last edited by 7stud; 11-25-2005 at 09:52 PM.

  14. #14
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by CompiledMonkey
    But, you don't need a pointer to use new, right?
    Its dynamically allocating it, and a pointer goes to a free space of memory, so I would assume that yes, you need a pointer to use new. I havent heard of using new without a pointer, that I know of, but I could be wrong - there are a lot of technicalities or exceptions in C++.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Dae
    Its dynamically allocating it, and a pointer goes to a free space of memory, so I would assume that yes, you need a pointer to use new. I havent heard of using new without a pointer, that I know of, but I could be wrong - there are a lot of technicalities or exceptions in C++.
    The 'new' operator returns an address of some memory, and an address has to be assigned to a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM