www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/18/19:49:04

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: C++, complex, etc
Date: 18 May 2000 13:57:14 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 37
Message-ID: <8g0srq$ige$1@nets3.rz.RWTH-Aachen.DE>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 1000518113101 DOT 12245I-100000 AT is> <3923BA11 DOT AD387617 AT mtu-net DOT ru> <8g0to9 DOT 3vs4qnf DOT 0 AT buerssner-17104 DOT user DOT cis DOT dfn DOT de> <3923E49D DOT 870F5E34 AT mtu-net DOT ru>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 958658234 18958 137.226.32.75 (18 May 2000 13:57:14 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 18 May 2000 13:57:14 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Alexei A. Frounze <alex DOT fru AT mtu-net DOT ru> wrote:
> Do I need to declare a variable of "size_t" type, if I want to know length
> of the string using strlen()???

If you want to store the value of the strlen() call: Yes, most
certainly you have to. If you don't, you risk breaking your program on
some platform, where it could usually have worked nicely. A simple
example: if the char* variable 'mystring' happens to point to string
of 40000 characters, and you do this:

	int i;
	int length = strlen(mystring)
	/* set all of 'mystring' to the letter '-':
	for (i=0; i< length; i++)
	   mystring[i] = '-';
	
you'll be quite confused by the result on a 16bit platform. Even worse would
have been

	/* set the 10th-last letter to '+': */
	mystring[length-10] = '+';

This one will usually crash the program, or overwrite content of other
variables totally unrelated to 'mystring'.  Changing the types of 'i'
and 'length' to size_t gives working code.

> If so, how do I mix int and size_t then? Or I need to use size_t only, if I
> have problems with type casting???

You shouldn't generally need any type casting between int and
size_t. If at all, it'll usually be from int to size_t, not the other
way round. If you think you have to do that, chances are high that
you're just writing broken code.

-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019