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";
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 to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So let's say you have a C program, that calls a function int foo() before returning control to main(), then main() uses puts() to write to the console.
What are some ways to prevent puts from writing to the console?
The only #includes you get are stdio and stdlib. You cannot touch any of the code in main(), only foo().
#include <stdio.h>
#include <stdlib.h>
unsigned int foo()
{
//Your code here
return 0;
}
int main()
{
foo();
puts("If you are reading this you have failed");
return 0;
}
Just redirect stdout to somewhere else (like a normal file or a character device like /dev/null) with freopen(). See the sample there
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
puts("stdout is printed to console");
if (freopen("redir.txt", "w", stdout) == NULL)
{
perror("freopen() failed");
return EXIT_FAILURE;
}
puts("stdout is redirected to a file"); // this is written to redir.txt
fclose(stdout);
}
Adapting this to your particular case should be simple
At //Your code here, put exit(EXIT_SUCCESS);.
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 3 years ago.
Improve this question
My env is:
system:ubuntu 18.04
gcc version 7.5.0
memory:
KiB Mem : 7930352 total, 5953392 free, 1241908 used, 735052 buff/cache
KiB Swap: 5169144 total, 5169144 free, 0 used. 6419340 avail Mem
The fpp always gets NULL point, there is the code below:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(void)
{
DIR *dirp;
char** fpp=NULL;
const char* basePath="/tmp/temp";
if ((dirp=opendir(basePath)) == NULL)
{
perror("Open dir error...");
exit(1);
}
fpp = (char**)malloc(8);
if (NULL == fpp);
{
printf("error,no mem for fpp\r\n");
}
if (NULL != fpp)
{
free(fpp);
fpp=NULL;
}
if (NULL != dirp)
closedir(dirp);
return 0;
}
You have
if (NULL == fpp);
{
printf("error,no mem for fpp\r\n");
}
which is equivalent to
if (NULL == fpp)
{
}
{
printf("error,no mem for fpp\r\n");
}
Remove the extraneous semi-colon.
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;