TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Frank Adam
from: David Nugent
date: 1996-06-27 19:51:00
subject: Memory Blocks snuffed ?

> > 1. Assume. (the caller properly allocated the required memory).
 > > 2. Re-allocate. (the size of the variable to the required length).

 > DN> Or your program can keep track of the memory itself. It only takes one
 > DN> or two  variables.
 > DN> If you prefer an OOP style of writing C code (which I do), then doing
 > DN> this is  trivial, and largely invisible to the user of the code. You

 > You mean a mix of C and classes ? I do that sometimes.

No, I mean using OOP methodology.

C++ simply makes encapsulation easier and certainly more convenient in
terms of syntactic sugar and automatic constructors and destructors and so
forth. But it is not necessary to use C++ in order to write object oriented
code. Not in the least.


 > DN> My experience is that the need to reallocate almost always stems from
 > DN> one of  two scenarios. The first is an array, as I've just
 > DN> mentioned. The second is a buffer that contains text, which
 > DN> you are adding, removing or searching (such as you'd find

 > In that case the String class alone would do that i believe..

Not necessarily efficiently, but true. A string class would normally do
exactly what you're seeking to duplicate - tracking the size of its memory
block.

FWIW, I use a text buffer module that handles this seemlessly, but it
provides a far more efficient means of inserting and deleting text that
you'll find in most string classes. IMHO a "text buffer" - while
it is probably a string in some respects - tends to be used in an entirely
different manner.

For example, you would normally fetch lines or paragraphs from it rather
than printing it in one hit. And you would more likely to insert things
into it and minipulate parts of it than you would with a string. The
algorithm I use is an adaptation of the "gap" technique (popular
with editors), which is based on the idea that the "hole" of free
space inside a buffer can slide within it - to insert text less than the
size of the gap, you just move the cursor (lower bounds of the gap) to the
point of insertion and copy the text in there and reduce the gap size. If
you're doing serial insertions, such as when inserting individual
characters as they are typed, the insertion involves minimal moving memory
about in a clumbsy and slow manner. Similarly, deleting one more more
characters is a matter of moving the cursor and extending the size of the
hole. No compaction.

You would not normally need this sort of handling with a string. In fact,
you'd probably want to avoid it since a "string" is more easily
handled if it can be easily printed or examined as a whole, specifically as
a char *.

--- MaltEd/2 1.0.b6
* Origin: Unique Computing Pty Limited (3:632/348)
SEEN-BY: 50/99 620/243 623/630 632/103 107 348 360 633/371 634/388 396
SEEN-BY: 635/301 502 503 506 544 639/252 711/401 409 410 413 430 808 809 932
SEEN-BY: 711/934 712/515 713/888 714/906 800/1
@PATH: 632/348 635/503 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.