[LCP]Very Small Doubt...

Vincent Penquerc'h Vincent.Penquerch at artworks.co.uk
Mon Jul 8 19:55:07 UTC 2002


> int n;
> char *str, *str1;
> scanf("%s", str);
> scanf("%s", str1);
> scanf("%d",&n);

int n
places spce for an int on the stack, and you tell scanf
to place the int in it. Fine.

char *str
places spaces for a pointer to character on the stack.
However, you don't tell scanf to place its data in it
(you lack the &), but where it's pointing to. And you
haven't made it point anywhere, so it's pointing ...
somewhere unknown.

- char *str,*str1;
+ char str[256],str1[256];

or

- char *str,*str1;
+ char *str=malloc(256),*str1=malloc(256);


However, remember than with scanf, you can't (AFAIK) size
the allocated memory to what is read, thus, stack smashing
possibility in the first patch, and arena corruption in
the second. A safer way is to read little bits by little
bits (of known size) and allocate accordingly.

--
Vincent Penquerc'h 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.linux.org.au/pipermail/linuxcprogramming/attachments/20020708/10c51a04/attachment.htm 


More information about the linuxCprogramming mailing list