Thread: Question about delete function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > 22 string='\0';
    This overwrites your pointer.
    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.

  2. #2
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    I'd use rather...

    Code:
    string[0]='\0';
    If you are building a String Class, in the uninitialized constructor, i.e....

    Code:
    String::String () 
    19 { 
    20         len=0; 
    21         string=new char [len+1]; 
    22         string='\0'; 
    23 }
    ...I'd alter the code to perform a minimal allocation, such as 16 bytes or something like that. Further, I'd add another private member variable for the capacity of the String, which will be one character less than the memory allocation (using the char data type). And in every member function you have to do careful 'book-keeping to maintain correct values for the length and capacity, or else you'll crash and burn.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    13
    Hi freddie
    I can not fully understand what you said because I am a beginner. So could you give me a simple code which perform a minimal allocation?


    Thanks
    Fan Li

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  2. List - Why my delete function doesn't delete?
    By juanjuanjuan in forum C Programming
    Replies: 7
    Last Post: 12-09-2014, 10:10 PM
  3. BST delete function
    By spikestar in forum C++ Programming
    Replies: 0
    Last Post: 08-31-2010, 08:55 PM
  4. Delete Function for AVL Trees
    By Lost in forum C Programming
    Replies: 5
    Last Post: 08-24-2009, 10:34 AM
  5. does C have a delete function??
    By aspand in forum C Programming
    Replies: 12
    Last Post: 05-17-2002, 03:14 PM

Tags for this Thread