Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2000:
[Freeciv-Dev] Re: OO model for Freeciv? (was: #define $#@$%! template)
Home

[Freeciv-Dev] Re: OO model for Freeciv? (was: #define $#@$%! template)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Falk Hueffner <falk.hueffner@xxxxxxxxxxxxxxxxxxxxxxxx>
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: OO model for Freeciv? (was: #define $#@$%! template)
From: Greg Wooledge <wooledge@xxxxxxxxxxx>
Date: Mon, 1 May 2000 21:57:28 -0400

Falk Hueffner (falk.hueffner@xxxxxxxxxxxxxxxxxxxxxxxx) wrote:

> Using STL would be the #1 reason for me to use C++ for FreeCiv,
> because container and string handling in C really sucks. For example,
> if I want to add a '.' to a string, I write
> 
> s += '.';
> 
> What is the shortest way to express this in C?

  strcat(s, ".");

Of course, that does not do any size-checking of the array s.

> char* newspace=(char*) malloc(strlen(s) + 1);
> strcpy(s, newspace);
> free(s);
> s = newspace;
> s[strlen(s) + 1] = '.';

(You switched the args of strcpy() and forgot the \0 handling....)

That's pretty inefficient, assuming you'll be concatenating to strings
fairly often.  If you'll be doing lots of that, I'd recommend using perl,
or (if you stick to C) at least writing a string-handling library to
save typing and give you a single place for optimization.

A more efficient approach is to make the char arrays big enough to
hold the biggest string you'll ever put in them.  If you don't have
a theoretical upper bound on the array (e.g., reading user input), it
makes more sense to make it "pretty big" and then allocate additional
large chunks as needed -- not 1 byte at a time!

> Well, nevertheless I think we should stick to C :-)

So do I.  There's no compelling reason to switch, IMHO.

-- 
Greg Wooledge                    | "Truth belongs to everybody."
wooledge@xxxxxxxxxxx             |   Red Hot Chili Peppers
http://www.kellnet.com/wooledge/ |

Attachment: pgptuQ1guPXnX.pgp
Description: PGP signature


[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] Re: OO model for Freeciv? (was: #define $#@$%! template), Greg Wooledge <=