delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/23/22:23:25

Message-Id: <199710240221.MAA29326@rabble.uow.edu.au>
Subject: strncpy question
To: djgpp AT delorie DOT com (DJGPP)
Date: Fri, 24 Oct 1997 12:21:52 +1000 (EST)
From: Brett Porter <bporter AT rabble DOT uow DOT edu DOT au>
MIME-Version: 1.0

Here is something strange I have encountered using DJGPP...

I use this code in C++, where fName is a char* and aName is const char*

fName = new char[strlen(aName)+1];
strncpy( fName, aName, 74 );

I don't want anymore than 74 characters to be displayed when I cprintf(
fName ), because it goes to the text mode screen, so I do this and also put
a zero at fName[74] (if the string length is that long)

It started to overwrite other memory.
I realise what I had done here was bad coding, and I've fixed it up, but I
was curious what was going on here. IS the definition of strncpy in DJGPP to
zero out up to the limit specified, or does it stop at the NULL character? I
thought it was the latter but this example would seem to indicate otherwise.

No urgency in replying, I'm just curious

Brett

-- 
"Who here believes in telekenesis? Raise MY hand!"
--
Brett Porter
bporter AT rabble DOT uow DOT edu DOT au

http://www.geocities.com/CollegePark/Union/3596
	Humour, Programming, and more.

- Raw text -


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