Sergei
Sergei

Reputation: 1

Segmentation fault (core dumped) - how to fix my code?

The program finds a char with the smallest ascii code in a string and outputs it. My problem is in message: Segmentation fault(core dumped). Why and Where does it occur? Thanks for attention.

#include <stdio.h>    
#include <string.h>    
#include <stdlib.h>    

int main(void) {    

char* str = NULL;    
int* mincode = NULL;    
int* count = NULL;    
char* mincodeChar = NULL;


 str = (char *) malloc(50 * sizeof(char));
 mincode = (int *) malloc(1 * sizeof(int));
 count = (int *) malloc(1 * sizeof(int));

 if (NULL == str || NULL == mincode || NULL == count){
        printf("Alloc error");
        return EXIT_FAILURE;
    }

fgets(str, 50, stdin);

printf("your string: ");
puts(str);

*mincode = (int)(str[*count]);
*mincodeChar = *(str + *count);

 for (*count = 0; str[*count] != '\0'; (*count)++) {

    if( (int)str[*count] < (*mincode)) {
    (*mincode) = (int)str[*count];
    mincodeChar = (str + *count);
    printf("%c", *mincodeChar);
    }
}

printf("your character: ");
printf("%c", *mincodeChar);

free(str);
free(mincode);
free(count);

return EXIT_SUCCESS;
}

Upvotes: -1

Views: 1127

Answers (3)

Creative Liberties
Creative Liberties

Reputation: 26

*mincodeChar = *(str + *count);

You're dereferencing a NULL pointer, instead of initializing it to NULL at the beginning, do

char* mincodeChar = new char();

or

char* mincodeChar = malloc(sizeof(char));

also count is allocated but not initialized, when you set mincode with it, it could be anywhere from 0 to the 32 bit integer limit When you're allocating pointers with malloc() it does not initialize a value, so make sure to assign a value soon after allocation, before use, because it could be completely junk values that could mess up your program, I always make sure to either set the value soon after, or initialize all of the memory to 0s with memset so I know it is good to use. Doesn't matter here but once you start to use them in bigger programs or do more complex things with them, it can be an issue.

Upvotes: 0

jurhas
jurhas

Reputation: 653

Didn't check but

*mincode = (int)(str[*count]);
*mincodeChar = *(str + *count);

count contains a value not initialized. So it can be 0 like, more probably, something like 492892911039... never understood the randomness of the not initialized variables. And in this case you are attempting to open the position at 492892911039.... SIGSEGV? if you want 0 or explicitely set it to 0 or rather then malloc() call calloc(). Then the same as the others... why you allocate a single variable? ... It can happen, but is for foreign constraints, an example is when you call some API functions that only accept a void *... but when is possible to avoid is better avoid. Not only for lazyness. Most implementations of malloc are list.So more you allocate more long becomes this list that at each new allocation must be scored. So a bit it slows down your malloc(). Not of course in this case... but ever better allocate few big block of memory rather than a lot small

Upvotes: 0

char* mincodeChar = NULL;
....
*mincodeChar = *(str + *count);

You dereference a NULL pointer.

The lessons to take from this are:

  1. Always initialize you variables to a valid value as soon as possible.
  2. Check pointers before dereferencing.

Upvotes: 4

Related Questions