C program using printf & scanf crashes on input

Go To StackoverFlow.com

2

I am writing following c code and getting an error :

#include<stdio.h>
#include<stdlib.h>

int main()
{
char *prot;
char addr[20];
FILE *fp;
int i = 0;
int tos,pld;

prot = (char *)malloc(sizeof(char *));
//addr = (char *)malloc(sizeof(char *));

printf("\n enter the protocol for test::");
scanf(" %s",prot);
printf("\n enter the addr::");
scanf(" %s",addr);
printf("\n enter the length of the payload::");
scanf(" %d",pld);
printf("\n enter the tos :: ");
scanf(" %d",tos);

I am getting the following error while entering the value.There is a segmentation fault coming could anyone tell me why this segment fault is coming:

enter the protocol for test::we

enter the addr::qw

enter the length of the payload::12

Segmentation fault
2012-04-04 00:24
by karan421


5

prot = (char *)malloc(sizeof(char *));

Should be:

prot = malloc(sizeof(char) * SIZE); // SIZE is the no. of chars you want

Another problem is: You should use & for integers in scanf()!

With changes:

printf("\n enter the length of the payload::");
scanf(" %d",&pld);
printf("\n enter the tos :: ");
scanf(" %d",&tos);
2012-04-04 00:27
by P.P.
could u explain the bug for proto ....I mean its working fine its printing the whole content....please do explain.. - karan421 2012-04-04 00:33
And you shouldn't cast the return value from malloc either - paxdiablo 2012-04-04 00:35
I thought my answer is clear enough. Which part you don't understand? 'prot' is a char pointer. So you should allocate memory. So you call memory to allocate SIZE number of chars which will be pointed by 'prot'. Another is: you should use address-of (&) for reading integers (in scanf) - P.P. 2012-04-04 00:38
@paxdiablo then how should i initialize the pointe - karan421 2012-04-04 00:38
@karan421, just use prot = malloc(sizeof ... without the cast. The void* returned from malloc will automatically cast to any other pointer, and explicitly casting it can hide errors such as when you forget to include the header with the malloc prototype - paxdiablo 2012-04-04 00:41
Edited my post. I think it would be clearer now - P.P. 2012-04-04 00:42


3

The segmentation fault is because scanf expects a pointer to the variable the scanned value shall be stored in, but you pass the variable pld itself. That is uninitialised and hence when interpreted as a pointer points into the wild. The same happens with the tos. And of course, you should allocate the proper amount of space for prot as has otherwise been pointed out.

2012-04-04 00:28
by Daniel Fischer


3

Your memory allocation for prot has allocated 4 bytes (on a 32-bit system) or 8 bytes (on a 64-bit system) for the string. If you read more than that into it, you are overflowing your buffer.

Unless there was a good reason to do otherwise, I'd simply use:

char prot[128];

for any suitable size for the string.

You should also check all your scanf() calls to ensure they succeed; you should probably apply a limit to the size of the strings. For a char prot[128];, the safe conversion is %127s; the null is not counted in the conversion specification.

If your compiler was not warning you about these lines:

scanf(" %d",pld);
scanf(" %d",tos);

you either need to turn on more warnings or get a better compiler. If it was warning you, pay heed to your compiler; it knows more about C than you do (and probably more about it than I do, too).

scanf(" %d", &pld);
scanf(" %d", &tos);
2012-04-04 00:28
by Jonathan Leffler


1

This is probably not the source of your current problem, but it is a bug:

prot = (char *)malloc(sizeof(char *));

I doubt you meant to make a buffer the size of one character pointer.

Anyway, to pinpoint your immediate issue, please run your program under valgrind and/or a debugger. In this particular case just enabling compiler warnings would have caught your problem, which is that you're passing integers by value where you should be passing by pointer to scanf. This could have been solved by the compiler, instead of coming to us, if only you enable the relevant options.

2012-04-04 00:27
by John Zwinck


1

scanf expects pointers to the variables you're filling (except in the case of strings, which are already pointers to char).

Try

scanf(" %d", &pld);

and the same with tos.

2012-04-04 00:29
by cHao


0

scanf family of functions are main source of problems in homework.

  1. They always expect addresses, so they can be used as OUTPUT.
  2. They cannot be type-checked because the prototype of that part is ..., so you can put anything there. compiler does not complain.

When things don't work, check the receiver arguments, they need to be address of items to be written into, and the type has to match what your specify in the format string.

2012-04-04 05:06
by pizza
Ads