#include<stdio.h>
#include<stdlib.h>
main(){
	char *tab;
	int n;
	int i;
	char c;
	
	printf("dimension du tableau?");
	scanf("%d",&n);

	tab=(char*)malloc(n*sizeof(char));

	printf("remplir le tableau :");
	fflush(stdin);

	gets(tab);
	for(i=0;tab[i]!='\0';i++){
		printf("%c",tab[i]);   
	}
	
}

let's say i just want 5 letters in tab,so i run the program and put n=5,but what i don't get is that if i entered more then 5 letters the program continue workin without any error+memorize these letters in tab...isn't it supposed to stop on 5 letters or at least don't memorize these plus letters?
plz help me and thnx

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

isn't it supposed to stop on 5 letters or at least don't memorize these plus letters?

Why would it? You never told it to do anything of the sort. In fact, it's quite impossible to include those safety measures with the gets function. You simply invoke undefined behavior, and the program is allowed to do anything (including behaving normally or crashing).

Now for some tips:

main(){

Two problems here.

  1. Implicit int has been removed in the latest C standard, which means eventually your code will fail to compile. You should be explicit:
    int main(void)
  2. Since you tried to use implicit int, you're clearly not compiling under the latest standard. Therefore, failure to return a value is also undefined behavior. It's generally considered best practice to return a value, even in C99:
    int main(void)
    {
      return 0; /* Standard "success" value */
    }
printf("dimension du tableau?");

If you want the prompt to show up before blocking for input, either print a newline or flush the output stream. Since printing a newline isn't acceptable if you want the input to be on the same line as the prompt, your only guaranteed option is fflush:

printf("dimension du tableau? ");
fflush(stdout); /* printf uses stdout */
scanf("%d",&n);

Always, always, ALWAYS, ALWAYS DAMMIT, check input for failure. :)

/* scanf returns the number of successfully converted values */
if (scanf("%d", &n) != 1)
{
    /* scanf failed. recover or bail */
}
char *tab;
tab=(char*)malloc(n*sizeof(char));

Doesn't typing char three times bother you? Here's a trick for only typing it once:

char *tab;
tab = malloc(n * sizeof *tab);

Much shorter, and I even added whitespace. The return value of malloc doesn't need to be type cast in C. Using sizeof *tab instead of sizeof(char) is a trick that takes advantage of the fact that sizeof doesn't evaluate the expression, only the type of the expression. So even though tab is an uninitialized pointer, you don't invoke undefined behavior.

Also, sizeof(char) is guaranteed to be 1 in all cases. You can remove it entirely if you don't plan on moving to a wide character type for tab:

tab = malloc(n);

Finally, malloc returns NULL if it fails, and it does fail occasionally. You should check for this error as well.

fflush(stdin);

fflush is only designed for output streams. This line of code is broken, even if it happens to work on your compiler. Do a search for fflush(stdin). You'll find tons of resources on why it's wrong and alternatives.

gets(tab);

Just forget that gets even exists. Use fgets instead. fgets offers you a way to limit the number of characters that are read.

Edited 6 Years Ago by Narue: n/a

Comments
Just for the 'ALWAYS DAMIT' I feel the frustration

Here's a blurb from manpages malloc which discusses buffer overflows(its in the second half).

NOTES
Normally, malloc() allocates memory from the heap, and adjusts the size
of the heap as required, using sbrk(2). When allocating blocks of mem‐
ory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation
allocates the memory as a private anonymous mapping using mmap(2).
MMAP_THRESHOLD is 128 kB by default, but is adjustable using mal‐
lopt(3). Allocations performed using mmap(2) are unaffected by the
RLIMIT_DATA resource limit (see getrlimit(2)).

The Unix98 standard requires malloc(), calloc(), and realloc() to set
errno to ENOMEM upon failure. Glibc assumes that this is done (and the
glibc versions of these routines do this); if you use a private malloc
implementation that does not set errno, then certain library routines
may fail without having a reason in errno.

Crashes in malloc(), calloc(), realloc(), or free() are almost always
related to heap corruption, such as overflowing an allocated chunk or
freeing the same pointer twice.

Recent versions of Linux libc (later than 5.4.23) and glibc (2.x)
include a malloc() implementation which is tunable via environment
variables. When MALLOC_CHECK_ is set, a special (less efficient)
implementation is used which is designed to be tolerant against simple
errors, such as double calls of free() with the same argument, or over‐
runs of a single byte (off-by-one bugs). Not all such errors can be
protected against, however, and memory leaks can result. If MAL‐
LOC_CHECK_ is set to 0, any detected heap corruption is silently
ignored; if set to 1, a diagnostic message is printed on stderr; if set
to 2, abort(3) is called immediately; if set to 3, a diagnostic message
is printed on stderr and the program is aborted. Using a nonzero MAL‐
LOC_CHECK_ value can be useful because otherwise a crash may happen
much later, and the true cause for the problem is then very hard to
track down.

Edited 6 Years Ago by gerard4143: n/a

thnx guys,so i need to use the "for" for(i=0;i<n;i++){scanf("%c",tab)},and in the case of using gets() the memory reserved by malloc can be changed?

thnx guys,so i need to use the "for" for(i=0;i<n;i++){scanf("%c",tab)},and in the case of using gets() the memory reserved by malloc can be changed?

I would check out fgets() like Narue said.

char *fgets(char *s, int size, FILE *stream);

Edited 6 Years Ago by gerard4143: n/a

i'm using visual c++ 2008 express edition i'm not sur it does work+ in a exam ican't use things that we didn't take it at class:( but anyway thnx i'll take it in consideration in the futur

i'm using visual c++ 2008 express edition i'm not sur it does work+ in a exam ican't use things that we didn't take it at class:( but anyway thnx i'll take it in consideration in the futur

"Don't let school interfere with your education" ~ Samuel Langhorne Clemens

Narue> Just forget that gets even exists.

Hearken to that.

This article has been dead for over six months. Start a new discussion instead.