Thread: Copying one char* to another not working?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    26

    Copying one char* to another not working?

    I have this constructor:

    Code:
    Reverser::Reverser( char* string )
    {
        int bufferSize = 0;
    	bufferSize = GetLength( string );
    
    	Size = bufferSize;
    	Buffer = new char[Size];
    
    	// Copy buffer
    	//Copy( buffer, Buffer, 0 );
    
        int a = Size - 1;
    	for ( int i = 0; i < Size; i++, a-- )
    		Buffer[i] = string[a];
    }
    it creates a Reverser object, creates a new char* (Buffer), and then copies the contents of string to Buffer, backwards.

    for some reason, it is copying an empty string into Buffer...?

    when debugging it, it gets Size correct, but through every iteration of the for loop, Buffer[i] = that I with an apostrophe over it (which means nothing is stored in it...)

    UPDATE...okay, adding a cout << Buffer[i]; in the for loop proves that it is indeed working right.

    so the problem must be in my main loop?

    Code:
    #include <iostream>
    #include "Reverser.h"
    
    using namespace std;
    
    int main()
    {
        Reverser * aString = new Reverser( "wonderful" );
    
        char* output = aString->GetReverse();
    
        cout << output;
    
        delete aString;
    }
    UPDATE x2: Nevermind, I got it, just needed int a = Size - 2; instead of Size - 1, it now works, but after it outputs the string, I get a bunch of jibberish afterwords..?
    Last edited by Saggio; 10-10-2006 at 04:42 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    UPDATE x2: Nevermind, I got it, just needed int a = Size - 2; instead of Size - 1, it now works, but after it outputs the string, I get a bunch of jibberish afterwords..?
    The jibberish is likely because you are not properly null terminating your character string (if GetLength returns what I think it does). When you get the length of the incomming string you only get the length minus the null terminator. You need to account for this when you call new to allocate memory for the new string.

    Code:
    Reverser::Reverser( char* string )
    {
        int bufferSize = GetLength(string);
    
        Buffer = new char[bufferSize+1];  // +1 for the null
    
        for( int i = 0; i < bufferSize; ++i )
            Buffer[i] = string[bufferSize-i-1];
        Buffer[bufferSize] = 0;  // Properly null terminate the string
    }
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  2. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  3. copying file into string - not working?
    By ccoder01 in forum C Programming
    Replies: 3
    Last Post: 04-22-2004, 06:57 AM
  4. progress indicator for file copying
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 05-24-2002, 02:30 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM