Copying to free store

This is a discussion on Copying to free store within the C++ Programming forums, part of the General Programming Boards category; Hi, I am doing an exercise that states to write a function that copies a c-style string into memory it ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Copying to free store

    Hi,

    I am doing an exercise that states to write a function that copies a c-style string into memory it allocates on the free store and to not use any standard library functions. This is what I have so far:
    Code:
    char* foo(char* c)
    {
    	char* e = new char;
    	e=c;
    	
    	return e;
    }
    
    int main()
    {
    		
    		
    	char* f = func("hello");
    
    	cout << f;
    	
    
    }
    It seems to work, and the cout prints correctly. My concern is that I have allocated memory on the free store without immediate initialisation, that was done in the line below. Is this how it is done?

    Thanks,

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    It is not. You have allocated one character on the free store and given its address to e. Then you have reassigned e to the string literal "hello", which exists in memory allocated by main(). Then you, via the function return, assigned the "hello" address to f. Printing f will print "hello", but you have done little but confuse yourself. No offense. Check it out:

    Code:
    char* foo(char* str)
    {
       //Find out how big the string referred to by str is.
       //That is, write your own strlen implementation.
       //Then,
       
       unsigned int len = MyStrlen(str);
       char* returnable = new char[len];
       
       //Copy, character by character, the contents of str into returnable.
       //That is, write your own implementation of strcpy.
       //Then,
    
       return returnable;
    }
    
    int main()
    {
       const char* lit = "hello";
       char* f = foo(lit);  ///const trouble? I'm not sure.
       std::cout << f << " is a copy of " << lit << std::endl;
    
       delete [] f; //so easy to forget... your program has a leak, by the way.
       return 0;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by CodeMonkey View Post
    It is not. You have allocated one character on the free store and given its address to e. Then you have reassigned e to the string literal "hello", which exists in memory allocated by main(). Then you, via the function return, assigned the "hello" address to f. Printing f will print "hello", but you have done little but confuse yourself. No offense. Check it out:

    Code:
    char* foo(char* str)
    {
       //Find out how big the string referred to by str is.
       //That is, write your own strlen implementation.
       //Then,
       
       unsigned int len = MyStrlen(str);
       char* returnable = new char[len];
       
       //Copy, character by character, the contents of str into returnable.
       //That is, write your own implementation of strcpy.
       //Then,
    
       return returnable;
    }
    
    int main()
    {
       const char* lit = "hello";
       char* f = foo(lit);  ///const trouble? I'm not sure.
       std::cout << f << " is a copy of " << lit << std::endl;
    
       delete [] f; //so easy to forget... your program has a leak, by the way.
       return 0;
    }
    Thanks, I will have a go at implementing that now.

    Darren.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    For my own strlen function, would it be something as simple as:
    Code:
    int len = 0;
    	for(int i=0; str[i]!=0; ++i){
    		++len;
    	}

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    I have this as the final solution, is this correct?
    Code:
    char* foo (char* str)
    {
    	int len = 0;
    	for(int i=0; str[i]!=0; ++i){
    		++len;
    	}
    
    	char* returnable = new char(len);
    
    	for(int i = 0; i<len; ++i){
    		returnable[i] = str[i];
    	}
    
    	return returnable;
    
    	
    
    	
    	
    }

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    Almost, look at the difference between your 'char* returnable =' line and CodeMonkey's above. Yours allocates one char and initializes it with the value of len (the effect of the round brackets), the other allocates an array of len chars (the effect of the square brackets). Just a little error in syntax, along with forgetting to account for the terminating 0 on returnable, but otherwise fine. Though if you're going to be doing other exercises which have the no standard library requirement, it'll be worth breaking the copying and length finding code into seperate functions.
    Last edited by adeyblue; 07-27-2010 at 09:14 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > char* returnable = new char[len];
    You forgot to count the \0

    > char* returnable = new char(len);
    There is a big difference between using [ ] and ( )
    [ ] allocates an array of the specified size
    ( ) allocates a single object, and initialises it to the provided value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by adeyblue View Post
    Almost, look at the difference between your 'char* returnable =' line and CodeMonkey's above. Yours allocates one char and initializes it with the value of len (the effect of the round brackets), the other allocates an array of len chars (the effect of the square brackets). Just a little error in syntax, along with forgetting to account for the terminating 0 on returnable, but otherwise fine. Though if you're going to be doing other exercises which have the no standard library requirement, it'll be worth breaking the copying and length finding code into seperate functions.
    Thanks for that.

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Quote Originally Posted by Salem View Post
    > char* returnable = new char[len];
    You forgot to count the \0
    My bad.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. free() doesn't seem to work...
    By AlienJedi in forum C Programming
    Replies: 10
    Last Post: 01-29-2008, 05:27 PM
  3. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  4. How to free memory in this program
    By Coconut in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 11:57 PM

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