segmentation fault in c about using malloc, and char array pointer - c

im trying to make a program which reads what we wrote without concerning the memory, and print out what we wrote!
but it goes to segmentation fault (core dump)
#include <stdio.h>
#include <string.h>
int isdigit(char c);
int main()
{
char *input
int length;
printf("Number or Letter\n");
gets(input);
input = (char*)malloc(sizeof(char)*strlen(input));
printf(input[0]);
return 0;
}

To read in an arbitrary long input string, you must use some kind of memory re-allocation when the input string grows beyond the already allocated memory. For instance you could use realloc like this:
#include <stdio.h>
#include <stdlib.h>
#define INCREASE 32
int main(void) {
int c;
int allocated = INCREASE;
int used = 1;
char* in = malloc(allocated*sizeof(char));
if (!in) exit(1);
*in = '\0';
while((c = fgetc(stdin)) != EOF && c != '\n')
{
if (used > (allocated-1))
{
// printf("Realloc\n");
allocated += INCREASE;
char* t = realloc(in, allocated);
if (t)
{
in = t;
}
else
{
free(in);
exit(1);
}
}
in[used-1] = c;
in[used] = '\0';
++used;
}
printf("%s\n", in);
free(in);
return 0;
}

Related

Segmentation Fault using when while loop

I am writing a program which take a line from user and invert case of letters
The following code works fine
#define _GNU_SOURCE
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
int main(void) {
printf("Enter line: ");
char *input_ptr;
size_t input_length;
ssize_t read = (int) getline(&input_ptr, (void *) (&input_length), stdin);
if (read != -1) {
int i = 0;
for (; i < input_length; i++) {
int c = *(input_ptr + i);
if (isupper(c)) {
printf("%c", tolower(c));
} else {
printf("%c", toupper(c));
}
}
} else {
puts("Something Wrong Happened ...");
}
return 0;
}
However, when I change the for loop to while loop:
#define _GNU_SOURCE
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
int main(void) {
printf("Enter line: ");
char *input_ptr;
size_t input_length;
ssize_t read = (int) getline(&input_ptr, (void *) (&input_length), stdin);
if (read != -1) {
while (*input_ptr != '\0') {
int c = *input_ptr;
input_ptr++;
if (isupper(c)) {
printf("%c", tolower(c));
} else {
printf("%c", toupper(c));
}
}
} else {
puts("Something Wrong Happened ...");
}
return 0;
}
It says segmentation error after I have entered my line.
May I know what happened? Thanks in advance.
You have not initialised input_ptr so the code has undefined behaviour. You might have passed an invalid buffer address to getline. You should also initialise input_length, so
char *input_ptr = NULL;
size_t input_length = 0;
The function getline() expects either a pointer to memory you allocated yourself, or NULL to indicate that the function should allocate memory.
If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line. This buffer should be freed by the user program even if getline() failed.
Note, you should not increment a pointer which you intend to free later.

Why doesn't the code run with a test.in file created by Sublime TextEditor

This was a piece of code I have written for my assignment, some of the weird code design are not controllable by me. I am currently writing these on MacOS.
file1
#include <stdio.h>
extern int read_palindrome();
int main()
{
if (read_palindrome()) printf("input is a palindrome");
else printf("input is not a palindrome");
return 0;
}
file2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_palindrome2(char *, int);
// malloc() will be used as usual to set aside an initial memory
// The entire input will be read gradually by characters using getchar()
// In the event we require more memory than what we have previously,
// use realloc() to increase memory size dynamically
int read_palindrome() {
unsigned int len_max = 128;
unsigned int current_size = 0;
char *pStr = malloc(len_max);
current_size = len_max;
int i = 0;
int c = EOF;
if (pStr == NULL) {
return -1;
}
while (( c = getchar() ) != '\n') {
pStr[i] = (char)c;
i++;
if(i == current_size) {
current_size += len_max;
char *tmp = realloc(pStr, current_size);
if (tmp == NULL) {
free(pStr);
return -1;
}
pStr = tmp;
}
}
int retval = check_palindrome2(pStr,i);
free(pStr);
return retval;
}
int check_palindrome2(char *s, int length) {
for (int i = 0; i < length / 2; i++) {
if (s[i] != s[length-i-1])
return 0;
}
return 1;
}
I would think this code works except for empty files, which will cause my program to continuously expect input and not terminate. However, I realised when using Sublime Text, creating a test.in file without pressing "Enter" somehow displays the "non-terminating" behaviour as well, while typing something in vim without pressing "Enter" for a newline still allows the code to work. Does anyone know the reason behind this phenomenon?

segmentation fault (core dumped) error in C program

I tried to compile and run the following program to reverse a string using the gcc compiler for linux but it shows the error : segmentation fault (core dumped).I even tried to debug using gdb but it didn't help. The program given below firstly inputs t which is the number of test cases.I tested the program with 3 test cases but after taking the 2nd input from user, the compiler shows error.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* strrev(char*);
int main(int argc, char *argv[])
{
int t,i=0,temp=0;
char *str[10],*rev[10];
scanf("%d",&t); //input the number of test cases
while(i<t)
{
scanf("%s",str[i]);
i++;
}
while(temp<t) //reverse the string and display it
{
rev[temp]=strrev(str[temp]);
printf("%s \n",rev[temp]);
temp++;
}
return 0;
getchar();
}
Function to reverse the string:
char *strrev(char *str)
{
int i = strlen(str)-1,j=0;
char ch;
while(i>j)
{
ch = str[i];
str[i]= str[j];
str[j] = ch;
i--;
j++;
}
return str;
}
You are getting segmentation fault because you haven't allocated space for elements of str.
You need to allocate memory first in main function.
scanf("%d",&t); //input the number of test cases
if(t <= 10)
for(size_t i = 0; i < t; i++)
str[i] = malloc(50); // Assuming string is no more than 50characters.
else
exit(0);
Beside this there are many flaws in your code. Here is the code after fixing them
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void strrev(char*); // Change return type to void
int main(void)
{
int t,i=0,temp=0, ch;
char *str[10];
scanf("%d",&t); //input the number of test cases
while((ch = getchar()) != EOF && ch != '\n'); // To consume newline character after scanf
// Allocate memory for str elements
if(t <= 10)
for(size_t i = 0; i < t; i++)
str[i] = malloc(50); // Assuming string is no more than 50characters.
else
exit(0);
i = 0;
while(i < t)
{
fgets(str[i],50,stdin); // Use fgets instead of scanf to read string
i++;
}
while(temp<t) //reverse the string and display it
{
// Since you are reversing string by flipping the characters the same
// string just pass pointer to it. str[temp] will be updated in function.
strrev(str[temp]);
printf("Reverse is %s \n", str[temp]);
temp++;
}
return 0;
}
void strrev(char *str)
{
size_t i = strlen(str)-1,j=0;
char ch;
while(i>j)
{
ch = str[i];
str[i]= str[j];
str[j] = ch;
i--;
j++;
}
//printf("Reverse is %s \n", str);
}
you have missed to allocate memory before reading char* value, so you can do this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* strrev(char*);
int main(int argc, char *argv[])
{
int t,i=0,temp=0;
char *str[10],*rev[10];
scanf("%d",&t); //input the number of test cases
while(i<t)
{
str[i] = (char*)malloc(100); // just allocate memory
scanf("%s", str[i]);
i++;
}
while(temp<t) //reverse the string and display it
{
rev[temp]=strrev(str[temp]);
printf("%s \n",rev[temp]);
temp++;
}
return 0;
getchar();
}
char *str[10],*rev[10];
You did not assign storage to hold string values yet for those pointers.
char * str; /* this is a string pointer */
char * str = malloc(15); /* this creates storage for a string */
char str[10]; /* this creates a static char array, also is a string */
I also had the same issue. I just fixed it by correcting the indices of the matrix.

How can i make C program that scans user's input(text) and save it on a dynamic string

I want to read input from user(text) using a C program and here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int i=0,x=0;
char *c;
c[i]=(char*)malloc(sizeof(char));
while(1){
c[i]=getc(stdin);
if(c[i]=='\n')
break;
i++;
realloc(c, i+1 );
}
c[i]='\0';
//printf("\n%d",strlen(c));
printf("\n\n%s",c);
return 0;
}
This program when it compiles there is 1 warning at c[i]=(char*)malloc(sizeof(char));:
warning: assignment makes integer from pointer without a cast [enabled by default]
This program works succesfully but if i remove x=0 from the code there is:
Segmentation fault (core dumped)
What should i change on this code so it can work without warnings or a useless random variable like x=0 to work.
Thank you!
As said by #Dabo, adjust the assignment.
c = malloc(sizeof(char));
Following are additional suggestions:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Use size_t rather than int for index
size_t i=0;
char *c;
c = malloc(1);
if (c == NULL) return 1; // Out of memory
while(1){
// To detect EOF condition, use type `int` for get() result
int ch = getc(stdin);
if(ch == EOF || ch == '\n') {
break;
}
c[i++] = ch;
// Important, test & save new pointer
char *c2 = realloc(c, i+1 );
if (c2 == NULL) return 1; // Out of memory
c = c2;
}
c[i] = '\0';
// Use %zu when printing size_t variables
printf("\n%zu",strlen(c));
printf("\n\n%s",c);
// Good practice to allocated free memory
free(c);
return 0;
}
Edit: fixed
Simply replace this
c[i]=(char*)malloc(sizeof(char));
with this
c = (char*)malloc(sizeof(char));
and remove cast, you don't need it in C.
c = malloc(sizeof(char));
Try this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int i=0;
char *c=(char*)malloc(sizeof(char));
while(1){
c[i]=getc(stdin);
if(c[i]=='\n')
break;
i++;
}
c[i]='\0';
printf("\n\n%s",c);
return 0;
}

Segmentation Fault erorr

My program reads a file specified in the argument and prints out each string and its frequency inside the file.
The program works for this file: http://www.cse.yorku.ca/course/3221/dataset1.txt
but not this file: http://www.cse.yorku.ca/course/3221/dataset2.txt.
It gives Segmentation fault (core dumped) error for the second file.
What could be wrong? Please help!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char word[101];
int freq;
} WordArray;
int main(int argc, char *argv[])
{
WordArray *array = malloc(sizeof(WordArray));
FILE *file;
int i = 0;
file = fopen(argv[1], "r");
char *str = (char*) malloc (108);
while(fgets(str, 100, file) != NULL)
{
int pos = 0;
char *word = malloc (100);
while (sscanf(str, "%s%n", word, &pos ) == 1)
{
int j;
for (j = 0; j < i; j++)
{
if (strcmp(array[j].word, word) == 0)
{
array[j].freq = array[j].freq + 1;
break;
}
}
if (j==i)
{
array = (WordArray *) realloc (array, sizeof(WordArray) * (i+1));
strcpy(array[i].word, word);
array[i].freq = 1;
i++;
}
str += pos;
}
}
fclose(file);
int k;
for (k=0; k<i; k++)
{
printf("%s %d\n", array[k].word, array[k].freq);
}
return 0;
}
Several problems:
You increment str as part of the second loop and don't reset it. I think this means your program is slowly walking through memory.
You fail to free word - probably better to allocate it outside the loop and on the stack but that won't cause a crash unless you input is huge and you run out of memory.
You don't need to cast result of malloc for modern compilers (yes, it used to be needed).
May want to check the results of malloc and realloc for safety.
I assume the first item is your problem.

Resources