Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
My program compiles successfully. It reads all the integer values but whenever I try to input the character , it gives segmentation fault error. I can't figure out what is the problem. Here is my code
#include <stdio.h>
struct soldier
{
int skill;
int superior;
};
struct soldier army[1000000];
int n;
int skillSum=0;
void findSkillSum(int);
int main()
{
int m,parent,child,s,update,i;
char command;
scanf("%d %d",&n,&m);
// struct soldier army[n+10];
for(i=1;i<=n;i++)
scanf("%d",&army[i].skill);
army[1].superior =0;
for(i=1;i<n;i++)
{
scanf("%d %d",&parent,&child);
army[child].superior = parent;
}
for(i=1;i<=m;i++)
{
scanf("%c",&command);
// s=command[2]-48;
if(command=='Q')
{
scanf("%d",&s);
findSkillSum(s);
printf("%d",skillSum);
printf("\n");
}
else
{
scanf("%d",&s);
scanf("%d",&update);
army[s].skill=update;
}
}
}
void findSkillSum(int s)
{
int i;
for (i=1;i<=n;i++)
{
if (army[i].superior==s)
{
skillSum+=army[i].skill;
findSkillSum(i);
}
}
}
A space should be added before %c to get the next non-whitespace character
scanf(" %c",&command);
Seeing that you've already solved the segmentation fault, I'll just give you a tip to understand what causes it.
GDB:
If you want to know what is provoking a segmentation fault, you just need to debug your program with gdb. That's easy as:
gdb -q ./program.out
This will start gdb, then just run the program with run command, and insert the input. At the moment I inserted a character insted than a number the program stoped with a SIGSEGV fault.
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400637 in main () at 1.c:29
29 army[child].superior = parent;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define SIZE 200
int main() {
FILE *input = fopen("word_list_final.txt", "r");
char buffer[SIZE];
int counter = 0;
if (input == NULL) {
printf("Error! Could not open file\n");
exit(-1);
}
while (fscanf(input, "%s\n", buffer) != EOF) {
counter++;
}
fclose(input);
printf("%d\n", counter);
return 0;
}
After executing, program prints correct result and mentioned message. (File, I'm reading from, contains one word per line)
Outputs:
89937042
*** stack smashing detected ***: terminated
Aborted (core dumped)
How to get rid of error message?
Most likely your input file contains a line of 200 characters or more, so when fscanf places the bytes in buffer, there is an overflow, which overwrites whatever is on your stack after the buffer array.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a pretty simple program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// argc is amount of elements that the user inputs, check if 1234 isn't in code
if (strcmp(argv[argc-1],"1234" != 0)) {
exit(-1);
}
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i],"-h")) {
// do whatever
printf("Hello world!");
} else if (strcmp(argv[i],"-f")) {
// argv[i+1] will be the file, print out the file to console
}
}
}
It allows the user to enter something, say ./my_kill -h 1234. If 1234 isn't the last thing, it's supposed to just exit. Then in the for loop, if -h is used it prints hello world. For some reason it is giving me a segmentation fault and I don't understand why.
You have a parenthesis in the wrong position.
if (strcmp(argv[argc-1],"1234" != 0))
should be
if (strcmp(argv[argc-1],"1234") != 0)
Also, the first thing you should do is check if there is any information inside of argv.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
#include<stdio.h>
void main()
{
FILE *a[10];
int i,j,k;
float b[10][4][4];
for(i=0;i<8;i++)
{
char filename[100];
sprintf(filename,"infile%d.txt",i);
a[i]=fopen(filename,"r");
}
for(i=0;i<8;i++)
{
for(j=0;j<2;j++)
{
for (k=0;k<3;k++)
{
fscanf(a[i],"%f",&b[i][j][k]);
}
}
}
for (i=0;i<8;i++)
{
printf("\n-----------------%d--------------------",i);
for(j=0;j<2;j++)
{
for(k=0;k<3;k++)
{
printf("\nb[%d][%d][%d]=%f",i,j,k,b[i][j][k]);
}
}
}
}
I have written the above code in C. Which just reads 8 different files and print it in terminal. The file name is infile0, infile1 and son on up to infile7. Though the code runs, it shows segmentation fault core dumped in the terminal. I couldn't figure out why this happens at all. Can some one help me figure out the mistake in the code.
Compiling with -g and running trough valgrind gives an error line 19 when no files are present.
Maybe you should add
if(!a[i])continue;
before scanf. (and set some message or default values).
also have a look at Input validation using scanf(), as you should check scanf return value.
I hope it helps, this my my first so contribution.
EDIT:
the -g flag is for your compiler, e.g
gcc -g myfile.c -o myfile
then run your binary trough valgrind.
It will tell you any problems with memory: leaks, invalid red/write...
valgrind ./myfile
Don't forget to install valgrind if it is not on your system.
A lot of remarks can be done from your code
1) In
void main()
main returns an int, not void, so must be
int main()
2) In
FILE *a[10];
..
for(i=0;i<8;i++)
{
...
a[i]=fopen(filename,"r");
why do you have an array with 10 entries to only use 8 of them ?, better to have
FILE *a[8];
3) 10 (becoming 8) is used a lot of times in your code, if you decide to change the number of elements you will have to change it every where, it is more simple to use a #define or sizeof(a)/sizeof(a[0])
4) In
char filename[100];
sprintf(filename,"infile%d.txt",i);
you are very generous with the needed size, even your int are in 64b and you increase so much the number of entries in a (no change to have enough stack nor file description anyway) 20 digits are enough for a positive number, so char filename[20+10+1]; is enough
5) In
float b[10][4][4];
...
for(j=0;j<2;j++)
{
for (k=0;k<3;k++)
like for 2) there is no reason to use a so large array if you do not use all the entries
6) In
fscanf(a[i],"%f",&b[i][j][k]);
you do not check if the corresponding file was open so if a[i] is not NULL, probably your segmentation fault comes because a file wasn't open
you do not detect the end of file nor if the file doesn't contain a valid float, you need to do for instance if (fscanf(a[i],"%f",&b[i][j][k]) != 1) { ...error management... }
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
while understanding the parameters of main function i.e, int argc, char* argv[]
i wrote a piece of code to understand these parameters.
#include <stdio.h>
int main(int argc,char*argv[])
{
printf("test\n");
printf("%d %c",argc,*argv[argc-1]);
return 0;
}
This prints
test
1 F
here I don't understand why there is F as output. I mean how this is executed to result in output as F ?
I read about these perameters and main function at here and here. But still I don't understand how these works.
please explain .
EDIT: as mentioned in comments if I change the code to
printf("%d %s",argc,argv[argc-1]);
Now i'm getting the whole path of the file F:\file path
so does it mean argv[0] is the location of the file in drive?
It is not defined in the C standard, but on Unix argv[0] is the name of the executable. Then argv[1] the first argument, etc. I think that this is also true, most of the time, on Microsoft's Dos and their Windowing OSes.
In simple words:
When you give your commandline the instruction to run your program, you can append some text, which can be accessed in your program.
#include <stdio.h>
int main(int argc,char*argv[])
{
printf("This is the path or name of your programm: ");
printf("%s\n", argv[0]);
if(argc > 1) {
printf("This is the first argument you gave your programm: ");
printf("%s\n", argv[1]);
}
if(argc > 2) {
printf("This is the second argument you gave your programm: ");
printf("%s\n", argv[2]);
}
return 0;
}
Try to run this example with:
<path_to_the_programm> Hallo Welt
You will see argc is an integer, which tells you how many arguments you gave the program. I hope it helped you.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have this code:
char* env;
if (getenv("MP") == NULL)
{
env = "/usr";
}
else
{
env = getenv("MP");
}
printf("($MP is %s)\n", env);
printf("The program seg faults without printing me :(");
The program appears to seg fault after the first print if the $MP environmental variable is not set. If it is set, there is no seg fault and everything works fine.
I can get your program to segfault if I don't include stdlib.h
I.e. try this:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char* env;
if (getenv("MP") == NULL)
{
env = "/usr";
}
else
{
env = getenv("MP");
}
printf("($MP is %s)\n", env);
printf("The program seg faults without printing me :(");
return 0;
}
Why don't you do this?
const char *env = getenv("MP");
if (!env)
env = "/usr";