Article View: fr.comp.lang.c
Article #15057Re: bug dans un petit programme
From: Eric Levenez
Date: Sun, 16 Oct 2005 10:42
Date: Sun, 16 Oct 2005 10:42
85 lines
3004 bytes
3004 bytes
Le 16/10/05 12:01, dans
<1129454997.732041.79560@g43g2000cwa.googlegroups.com>, ��Thierry B��
<thierry.bouhnik@gmail.com> a �crit�:
> Bonjour,
>
> Je voulais vous poser une petite question.
>
> Voici un tout petit programme C:
>
> #define _POSIX_SOURCE 1
Pourquoi ce define ?
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <sys/wait.h>
> #include <unistd.h>
Pourquoi ces 3 includes ?
> int main(int argc, char *argv[]) {
> int arg;
> char *buf;
warning: 'buf' is used uninitialized in this function
> printf("Nombre d'argument: %d\n",argc);
> arg = atoi(argv[1]);
argv[1] n'existe pas forc�ment
> if (arg < 5) {
> sprintf(buf, "%d", arg);
sprintf �crit des donn�es dans un buffer non allou�.
> printf("Le nombre est %s.\n",buf);
> }
> return 0;
> }
>
> Quand je le lance, il me met ca:
>
> thierry@debian:~/Master/Posix/Supplement_Semaine2$ ./bin/exo3 2
> Nombre d'argument: 2
> Le nombre est 2.
> 3599:
> 3599: calling fini: /lib/tls/libc.so.6 [0]
> 3599:
>
> Il m'affiche bien le r�sultat, mais il met aussi des lignes bizarres,
> mais quand je remplace sprintf(buf, "%d", arg) par sprintf(buf, " %d",
> arg) cad en ajoutant juste un espace entre le " et %d, ca marche nikel.
>
> Quelqu'un a une id�e?
Il faut allouer la place de buf.
Il faut tester le nombre d'arguments au programme.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int arg;
char buf[42];
(void)printf("Nombre d'argument: %d\n", argc);
if (argc > 1) {
arg = atoi(argv[1]);
if (arg < 5) {
(void)sprintf(buf, "%d", arg);
(void)printf("Le nombre est %s.\n", buf);
}
}
return 0;
}
--
�ric L�v�nez -- <http://www.levenez.com/>
Unix is not only an OS, it's a way of life.
Technical Details
Message-ID:
<BF77FB36.4824C%new@levenez.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsfeed.gamma.ru!Gamma.RU!news-out2.kabelfoon.nl!newsfeed.kabelfoon.nl!xindi.nntp.kabelfoon.nl!news.astraweb.com!newsrouter-eu.astraweb.com!proxad.net!infeed-1.proxad.net!news18-e.free.fr!not-for-mailReferences:
<1129454997.732041.79560@g43g2000cwa.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,825aa0bc6fede96b X-Google-Attributes: gidf9822,public X-Google-Language: FRENCH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsfeed.gamma.ru!Gamma.RU!news-out2.kabelfoon.nl!newsfeed.kabelfoon.nl!xindi.nntp.kabelfoon.nl!news.astraweb.com!newsrouter-eu.astraweb.com!proxad.net!infeed-1.proxad.net!news18-e.free.fr!not-for-mail User-Agent: Microsoft-Entourage/11.2.0.050811 Date: Sun, 16 Oct 2005 12:42:30 +0200 Subject: Re: bug dans un petit programme From: Eric Levenez <new@levenez.com> Newsgroups: fr.comp.lang.c Message-ID: <BF77FB36.4824C%new@levenez.com> Thread-Topic: bug dans un petit programme Thread-Index: AcXSPk6xjW/C9j4xEdqI2gARJC39yA== References: <1129454997.732041.79560@g43g2000cwa.googlegroups.com> Mime-version: 1.0 Content-type: text/plain; charset="ISO-8859-1" Content-transfer-encoding: 8bit Lines: 86 Organization: Guest of ProXad - France NNTP-Posting-Date: 16 Oct 2005 12:42:32 MEST NNTP-Posting-Host: 82.244.40.208 X-Trace: 1129459352 news18-e.free.fr 1465 82.244.40.208:52012 X-Complaints-To: abuse@proxad.net