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
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);
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
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.
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);
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.
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
.
scanf family of functions are main source of problems in homework.
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.