Segmentation error in C while allocation memory - c

I am a total begginer at C programming and am trying to write a program that reads the value of "stat" file in /proc/. It works for the first few entries, but then it returns "Segmentation error (core dumped)".
So far I found out that the error has to do with the allocation of memory, but I cant seem to find a way to fix it.
My code so far is:
char* readFile(char* filename)
{
FILE *fp;
struct stat buf;
fp=fopen(filename,"r");
stat(filename,&buf);
char *string = malloc(buf.st_size);
char *s;
while(!feof(fp))
{
s=malloc(1024);
fgets(s,1024,fp);
s[strlen(s)-1]='\0';
strcat(string,s);
}
return string;
}
char* readStat(char* path, int statNumber)
{
char* str = malloc(sizeof(readFile(path)));
str = readFile(path);
char * pch = malloc(sizeof(str));
char * vals;
pch = strtok (str," ");
int i = 1;
while (pch != NULL)
{
if(i == statNumber)
vals = pch;
pch = strtok(NULL, " ");
i++;
}
return vals;
}

1) the
s=malloc(1024);
should not into the while it should be oitside the while loop and before the while.
And free it before leaving the function:
free(s);
2) add
string[0] = '\0';
just after
char *string = malloc(buf.st_size);
Otherwise the strcat will not work properly
3) You do not need to allocate memory for str pointer because the readFile function already did
char* str = malloc(sizeof(readFile(path)));
Just replaced with
char* str;
4) And also replace
char * pch = malloc(sizeof(str));
by
char * pch = str;

To start with, you don't allocate space for the terminator for the string variable. You also need to terminate it before you can use it as a destination for strcat.
To continue, when you do sizeof on a pointer, you get the size of the pointer and not what it points to. You have this problem in readStat.
You also have memory leaks, in that you call readFile twice, but never free the memory allocated in it. Oh, and one of the memory allocations in readFile is not needed at all.
And there's another memory leak in that you allocate memory for pch, but you loose that pointer when you assign the result of the strtok call. strtok returns a pointer to the string in the strtok call, so no need to allocate memory for it (which you didn't attempt to free anyway).

s=malloc(1024); should not be in loop, you should allocate the memory once and reset s with NULL before use next time in loop.
Also you should make a habit to free the memory after its usage.

Related

Segfault in C program, malloc call

I am writing a program that takes a list of path ( environmental variable), splits the paths and prints it. When compiling it I get a segfault. The following is my output on GDB :
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400eb0 in dest (name=0x7fffffffbce0 "PATH") at executables.c:100
100 dest[i] = malloc(srclen+1);
On valgrind:
==21574== 1 errors in context 2 of 3:
==21574== Use of uninitialised value of size 8
==21574== at 0x400EB0: dest (executables.c:100)
==21574== by 0x400B5B: main (main.c:9)
This is my function:
char** dest(char *name){
int i=0;
char *vp;
const char s[2]=":";
char *token;
char **dest;
name[strlen(name)-1]='\0';
vp=getenv(name);
if(vp == NULL){
exit(1);
}
token =strtok(vp,s);
while( token != NULL ){
size_t srclen = strlen(token);
dest[i] = malloc(srclen+1);
strcpy(dest[i], token);
token = strtok(NULL, s);
i++;
}
dest[i]=NULL;
return dest;
}
And this is my main:
#include "executables.h"
int main(int argc, char **argv){
char *path;
char name[BUFSIZ];
printf("enter name of environment variable:\n");
fgets(name,BUFSIZ,stdin);
char **p=dest(name);
int j=0;
while(p[j]!=NULL){
printf("%s\n",p[j]);
j++;
}
return(0);
}
Use strdup(). Saves steps (accounts for
'\0' too). You have to allocate some memory before hand for the approach you're using. Otherwise you might want a linked list and allocate packets instead of using the array pattern. When you say dest[i] = <ptr value> you're indexing to an offset of unallocated memory and storing something there, so it's a segvio.
#include <string.h>
#define MAXTOKENS 10000
char **get_dest(char *name) {
// Since dest has to be exposed/persist beyond this function all
// need dynamically allocate (malloc()) rather than stack allocate
// of the form of: char *dest[MAXTOKENS].
char *dest = malloc(MAXTOKENS * sizeof (char *)); // <--- need to allocate storage for the pointers
char *vp;
if ((vp = getenv(name)) == NULL)
exit(-1); // -1 is err exit on UNIX, 0 is success
int i = 0;
char *token = strtok(vp, ":");
while (token != NULL) {
dest[i] = strdup(token); // <=== strdup()
token = strtok(NULL, ":");
i++;
}
// dest[i] = NULL; // Why are you setting this to NULL after adding token?
return dest;
}
It's better if main() takes care of passing a proper null-terminated string to the get_dest() function because main is where the finicky fgets() is handled. Generally you want to do things locally where it makes the most sense and is most relevant. If you ever took your get_dest() function and used it somewhere where the strings were not read by fgets() it would just be a wasted step to overwrite the terminator there. So by initializing the char array to zeroes before fgets() you don't have to worry about setting the trailing byte to '\0'.
And finally probably not good to have your function name dest the same name as the variable it returns dest. In some situations having multiple symbols in your program with the same name can get you into trouble.
#include "executables.h"
int main(int argc, char **argv) {
char *path;
char name[BUFSIZ] = { 0 }; // You could initialize it to zero this way
printf("enter name of environment variable:\n");
// bzero(name, BUFSIZ); //... or you could initialize it to zero this way then
fgets(name, BUFSIZ, stdin);
char **p = get_dest(name);
int j = 0;
while(p[j] != NULL) {
printf("%s\n", p[j]);
j++;
free(p[j]); // like malloc(), strdup'd() strings must be free'd when done
}
free(p);
return 0;
}
dest[i] = malloc(srclen + 1);
You need to allocate memory for the pointer to char pointers (dest) as well as each char pointer stored in dest. In the code you provided, neither step is taken.
From the manpage of getenv:
Notes
...
As typically implemented, getenv() returns a pointer to a string
within the environment list. The caller must take care not to modify
this string, since that would change the environment of the process.
Your code violates that rule:
vp=getenv(name);
...
token =strtok(vp,s);
This is an illegal memory write operation.

Getting string with C function

I need to get strings dynamically but as I need to get more than one string, I need to use functions. So far I wrote this
(I put //**** at places i think might be wrong)
char* getstring(char *str);
int main() {
char *str;
strcpy(str,getstring(str));//*****
printf("\nString: %s", str);
return 0;
}
char* getstring(char str[]){//*****
//this part is copy paste from my teacher lol
char c;
int i = 0, j = 1;
str = (char*) malloc (sizeof(char));
printf("Input String:\n ");
while (c != '\n') {//as long as c is not "enter" copy to str
c = getc(stdin);
str = (char*)realloc(str, j * sizeof(char));
str[i] = c;
i++;
j++;
}
str[i] = '\0';//null at the end
printf("\nString: %s", str);
return str;//******
}
printf in the function is working but not back in main function.
I tried returning void, getting rid of *s or adding, making another str2 and tring to strcpy there or not using strcpy at all. Nothing seems to working. Am I misssing something? Or maybe this is not possible at all
//Thank you so much for your answers
Getting the string part can be taken from this answer. Only put a \n as input to the getline funtion.
char * p = getline('\n');
Three things :-
don't cast malloc, check if malloc/realloc is successful and sizeof is not a function.
The problem is not with the function that you are using, but with the way you try copying its result into an uninitialized pointer.
Good news is that you don't have to copy - your function already allocates a string in dynamic memory, so you can copy the pointer directly:
char *str = getstring(str);
This should fix the crash. A few points to consider to make your function better:
main needs to free(str) when it is done in order to avoid memory leak
Store realloc result in a temporary pointer, and do a NULL check to handle out-of-memory situations properly
There are two things to take away from the lesson as it stands now:
(1) You should have one way of returning the reference to the new string, either as an argument passed by reference to the function OR as a return value; you should not be implementing both.
(2) Because the subroutine your teacher gave you allocates memory on the heap, it will be available to any part of your program and you do not have to allocate any memory yourself. You should study the difference between heap memory, global memory, and automatic (stack) memory so you understand the differences between them and know how to work with each type.
(3) Because the memory is already allocated on the heap there is no need to copy the string.
Given these facts, your code can be simplified to something like the following:
int main() {
char *str = getstring();
printf( "\nString: %s", str );
return 0;
}
char* getstring(){
.... etc
Going forward, you want to think about how you de-allocate memory in your programs. For example, in this code the string is never de-allocated. It is a good habit to think about your strategy for de-allocating any memory that you allocate.
Let's simplify the code a bit:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getstring()
{
char c = 0;
int i = 0, j = 2;
char *str = NULL;
if ((str = (char*) malloc(sizeof(char))) == NULL)
return NULL;
printf("Input String: ");
while (c = getc(stdin)) {
if (c == '\n') break;
str = (char*) realloc(str, j * sizeof(char));
str[i++] = c;
j++;
}
str[i] = '\0';
printf("getstring() String: %s\n", str);
return str;
}
int main()
{
char *str = getstring();
printf("main() String: %s\n", str);
free(str);
return 0;
}
Then execute:
$ make teststring && ./teststring
cc teststring.c -o teststring
Input String: asdfasfasdf
getstring() String: asdfasfasdf
main() String: asdfasfasdf

C - list of char*'s - Memory Allocation

I am confused about how to allocate memory correctly. I am trying to make a list of char*'s from a text file. Every time I make a char* do I have to allocate memory for it? When and where are the exceptions?
#define BUFF 1000
int main(int argc, char** argv)
{
FILE* file;
file = fopen(argv[1], "r");
char* word = calloc(BUFF, sizeof(char));
char* sentence = calloc(BUFF, sizeof(char));
char** list = calloc(BUFF, sizeof(char*));
int i = 0;
while((fgets(sentence, BUFF, file)) != NULL)
{
word = strtok(sentence, " ,/.");
while(word != NULL)
{
printf("%s\n", word);
strcpy(list[i], word);
i++;
word = strtok(NULL, " ,/.");
}
}
int k;
for(k = 0; k < i; k++)
{
puts("segging here");
printf("%s\n", list[i]);
}
The rule is: you have to allocate any memory that you use.
Your problem comes in:
strcpy(list[i], word);
list[i] is currently not pointing to any allocated storage (it's probably a null pointer). You have to make it point somewhere before you can copy characters into it.
One way would be:
list[i] = strdup(word);
strdup is not an ISO C standard function, but it is equivalent to doing malloc then strcpy. You will need to free afterwards.
Also, the i++ line needs to stop when i == BUFF, and it'd be useful to add \n to the list of strtok separators .
In addition to Matt McNabb's answer, there's also a more subtle problem with your usage of strtok. That function doesn't require an output buffer; it just returns a pointer to somewhere inside the input buffer.
When you call char* word = calloc(BUFF, sizeof(char));, you allocate memory and assign word to point to the allocated memory. Then, when you call word = strtok(sentence, " ,/.");, you overwrite the value of word. That means that no pointer in your control points to the memory you've allocated. That memory is no longer useful to your code, and you can't deallocate it; it has been leaked.
You can fix this issue by writing char* word = strtok(sentence, " ,/."); Then, since you didn't allocate the memory that word points to, remember not to free it either.
your list is a char* list, with the size of BUFF, but the list[i] is what?
you do not allocate memory to it.
you need to allocate the memory for list[i] in a loop

Seg Fault while strcpy

This is the relevant piece of code I'm working on. I tokenize an input from the stdin with no issue and when I go to copy that input, I'm getting a segfault. However, I get no segfault with "strcpy(s,input)". Am I missing something fundamental here? Thank you
char *s = malloc(64 * sizeof(char));
char *token = malloc(64 * sizeof(char));
char *currstring = malloc(128 * sizeof(char));
currstring = NULL;
fgets(input,100, stdin);
strcpy(s, input);
token = strtok(s,delim);
while (token)
{
//Condition checking
strcpy(currstring,token);
}
char *currstring = malloc(128 * sizeof(char));
currstring = NULL;
You allocate memory, but then you immediately discard it and set the pointer to NULL. Get rid of the second line.
If you were trying to set it to an empty string (""), instead do:
currstring[0] = '\0';
// or
strcpy(currstring, "");
This probably isn't necessary, though. You don't need to set the string to "" if you're going to do a strcpy() later.
char *token = malloc(64 * sizeof(char));
You also do not need to allocate memory for token. strtok() will cause token to point somewhere within s, so allocating memory for token will simply leak memory once you do token = strtok(s, delim);.

Parsing a string in C with strsep (alternative methods)

I want to parse a string, and I use strsep function:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "Marco:Q:2F7PKC";
char *token1, *token2, *token3;
char *r = malloc(30);
strcpy(r, str);
token1 = strsep(&r, ":");
token2 = strsep(&r, ":");
token3 = strsep(&r, ":");
printf("tok1 = %s\n", token1);
printf("tok2 = %s\n", token2);
printf("tok3 = %s\n", token3);
free(r);
return 0;
}
The function do its job well, but If I launch valgrind, the allocated string char * r does not freed correctly (definitely lost: 30 bytes in 1 blocks).
I'd like to know why and if there are alternative way to do the same thing, maybe without call strsep.
I call valgrind with valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./a.out
strsep overwrites the target of its first (pointer-to-pointer) argument, so you lose the pointer to the malloc'd buffer's base. In fact, if you were do put a printf("%p\n", r); just before the free, you'd find out that you're freeing a null pointer, which has no effect.
The easy solution is to introduce an additional variable to keep that pointer around and free it when you're done. Idiomatic usage would be
char *r = strdup("Marco:Q:3F7PKC");
// check for errors
char *tok = r, *end = r;
while (tok != NULL) {
strsep(&end, ":");
puts(tok);
tok = end;
}
free(r);
I would like to a bit simplify a good reply from Fred Foo:
char *end, *r, *tok;
r = end = strdup("Marco:Q:3F7PKC");
assert(end != NULL);
while ((tok = strsep(&end, ":")) != NULL) {
printf("%s\n", tok);
}
free(r);
It gives the same result. But it is worth to say that strsep(3) stores next value after delimiter into end variable and returns current value (into tok variable).
The strsep function updates its first argument (so it points right after the token it found). You need to store the value returned by malloc in a separate variable and free this variable.

Resources