delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/14/04:22:16

Date: Thu, 14 Aug 1997 20:06:08 +1100
From: Bill Currie <billc AT blackmagic DOT tait DOT co DOT nz>
Subject: Re: why does this work like this (char allocation question)??
In-reply-to: <33EC75ED.2EA9@primenet.com>
To: "Smith A. Cat" <imbe AT primenet DOT com>, djgpp AT delorie DOT com
Message-id: <199708140810.UAA11528@teleng1.tait.co.nz gatekeeper.tait.co.nz>
Organization: Tait Electronics Limited
MIME-version: 1.0
Comments: Authenticated sender is <billc AT blackmagic DOT tait DOT co DOT nz>

On 13 Aug 97 at 20:08, Smith A. Cat wrote:

> when i run this:
> 
> #include <stdio.h>
> char buffer[4];
> void main(void){
> printf("%s  %d", gets(buffer), sizeof(buffer));
> }
> 
[snip]
> why doesn't it seg out as soon as the string exceeds four
> characters??

there happened to be someting between buffer and a critical variable 
used by gets() (or one of the functions it calls) that weren't 
touched any other way.

> 
> the same thing happens if you declare the string as a pointer
> (*buffer).

Ummm, pure luck?

> 
> does a declared variable name automagically get 64 bytes of exercise
> space??

Definitly not.

For safer string input, use fgets; it takes the maximum buffer size 
as the second paramenter (ie fgets(buffer,maxlen,file)).


Bill
--
Leave others their otherness.

- Raw text -


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