Page 1 of 1

Can't find mistake that causes Segmentation Violation.

Posted: Wed Jan 07, 2015 2:53 pm
by SteVeBrOwn
Hello,
I wanted to write a programm, that splits up a string into substrings. This should happen every time a certain character (for example a whitespace) is in the string.
The created substrings should be saved into an array of strings.
I know that i could have used the strchr function but at first i didn't want to use the string.h library.

I know, that the error is a segmentation fault even though codelite doesn't say so, because i ran it on another computer.

Hopefully someone can help me ;)

Code: Select all

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

char** splitString(char index, char* str)
{
  char** ret_str;
  char* buff_str;
  int loop_count1;
  int loop_count2;
  
  
  buff_str = (char*)malloc(sizeof(char) * strlen(str) + sizeof(char));
  ret_str = (char**)malloc(sizeof(char) * strlen(str) + sizeof(char));
  
  for(loop_count1 = 0; str[loop_count1] != '\0'; loop_count1++)
  {
    buff_str[loop_count1] = str[loop_count1];
    if(str[loop_count1] == index)
    {
      buff_str[loop_count1] = '\0';
    }
    if(loop_count1 == strlen(str))
    {
      buff_str[loop_count1 + 1] = '\0';
      break;
    }
  }
  
  int index_count = 0;
  
  for(loop_count2 = 0, loop_count1 = 0; loop_count1 <= strlen(str);
      loop_count2++, loop_count1++)
  {
    ret_str[index_count][loop_count2] = buff_str[loop_count1];
    
    if(buff_str[loop_count1] == '\0')
    {
      index_count++;
      loop_count2 = 0;
    }
    if(loop_count1 == strlen(str))
      ret_str[index_count][loop_count2 + 1] = '\0';
  }
  
  free(buff_str);
  return ret_str;
}

int main(int argc,char* argv[])
{
char a = ' ';
char* str = "Today is monday.";
char** str_arr= (char**)malloc(sizeof(char**));

str_arr = splitString(a, str);
printf("%s\n", str_arr[0]);
printf("%s\n", str_arr[1]);
printf("%s\n", str_arr[2]);

free(str_arr);
return 0;
}

Re: Can't find mistake that causes Segmentation Violation.

Posted: Wed Jan 07, 2015 5:36 pm
by Jarod42
Unrelated to Codelite.

Re: Can't find mistake that causes Segmentation Violation.

Posted: Thu Jan 08, 2015 5:22 am
by Gibbon1
Looks like home work...

sizeof(char**) on my Arm Cortex M0+ is like 4 bytes.

So this
char** str_arr= (char**)malloc(sizeof(char**));

Allocates 4 bytes of space for your array of string pointers which is probably not what you want.

Re: Can't find mistake that causes Segmentation Violation.

Posted: Fri Jan 09, 2015 2:31 am
by SteVeBrOwn
Gibbon1 wrote:Looks like home work...

sizeof(char**) on my Arm Cortex M0+ is like 4 bytes.

So this
char** str_arr= (char**)malloc(sizeof(char**));

Allocates 4 bytes of space for your array of string pointers which is probably not what you want.
It isn't homework. It's from an Exam at Uni.
We have to write the Code for something like that on paper :( (in 20 mins or so)

I know now, that this is not what i want. Got a version that works from another student.
The Problem with the other version is, that it just works as long as the string befor the charakter we search for is not bigger than a buffer (100 bytes).

Code: Select all

// SPLITSTRING

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

int strlen(char *randomString)
{
  int i = 0;
  for(i = 0; randomString[i] != '\0'; i++);
  return i;
}


char **splitString(char chara, char *string)
{
  int i = 0; //index
  int j = 0; //zeile
  int count = 0; // ziel index
  int size = strlen(string);


  int num_lines = 1; // get
                     // number of lines
  for(i = 0; i < size; i++)
  {
    if(string[i] == chara)
    {
      num_lines++;
    }
  }
  
  i = 0;

  char **buffer = malloc(num_lines*sizeof(char*)); // allocate
                                                   // memory for lines
  for(j = 0; j < num_lines; j++)
  {
    buffer[j] = malloc(100);
  }
  
  j = 0;


  for(i = 0; i < size; i++) // write
                            // lines into array
  {
    if(string[i] != chara)
    {
      buffer[j][count] = string[i];
      count++;
    }
    else //string[i] == chara
    {
      buffer[j][count] = '\0';
      j++;
      count = 0;
    }
  }
  buffer[j][count] = '\0';
  
  return buffer;
}




int main()
{
  char **array = splitString(' ', "Today is Monday");
  int i;
  
  for(i = 0; i < 3; i++)
  {
    printf("%s\n", array[i]);
    free(array[i]);
  }
  
  free(array);
  return 0;
}