Thread: New

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    New

    Some guy named stoned_coder ansvered on one of my question which was about fastnes . Here's the code he commented
    Code:
    AnsiString TFileHandle::Load() const
    {
      char *Text = new char[1];
      Text[0] = '\0';
      char *Temp;
      char Char;
    
      unsigned int Lenght;
    
      ifstream File(itsName.c_str());
    
      if (File.is_open())
      {
        while (File.get(Char))
        {
          Lenght = strlen(Text);
    
          Temp = new char[Lenght + 2];
    
          strcpy(Temp, Text);
    
          Temp[Lenght] = Char;
          Temp[Lenght + 1] = '\0';
    
          delete [] Text;
          Text = Temp;
        }
      }
    
      File.close();
    
      return Text;
    }
    You are "newing" one char at a time and you want this to e fast. Id totally rethink your approach and try again. Each call to new will cost you in efficiency terms about 8* a normal function call. So "newing" a single char for everyone read would really slow this up like glue.
    My NEW ( ) question is, if you use a String class the that code would without doubt include the same amount of "new" if you get me. Because how else are you going to make a array longer as needed. Then you might say why don't just make the array as long as you need it, but when you don't know how long you need it. (A file can be very short or long )

    Hope my text is readable, but I'm as always kind of timelimeted.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Investigate the STL (Standard Template Library) and vectors. Vectors behave, for the most part, as standard arrays with the added benefit of increasing/decreasing in size, on the heap, as required. (Vectors handle resizing themselves, so don't panic! )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    So if I understand what your saying it doesn't matter if I do this myself or use an object since the allocating/deleting on the heap happens just as often. Right?
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    The allocating/de-allocating of memory with vectors is part of their built-in functionality.

    Vectors, like standard arrays, occupy contiguous areas of memory. If the compiler detects that you're going to exceed the initial, available memory set aside for the vector, it re-writes the entire vector to a new, larger area of memory. This is done without any code needed from you.

    In fact, you can use the max_size() function to see how large the vector, or any container, can be expanded to, dependent, of course, on the type of data being stored.

    I hope this answers your question.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Yep... that no matter if you use these built-in vectors or your own code it'll be just as fast, becuase they should do the same(if not even more complicated and thereby more demanding in memory) Though you may make some erros making it yourself. On the other hand you'll then not have to wast allot of memory on those classes with extra "things".
    You'll have to chose betwem fastness or saveness... I'll go with saveness unless it's a special situation. Like a game they tend to be a bit slow.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

Popular pages Recent additions subscribe to a feed