-1

I need help with allocating memory to my variables. I'm looking for specific lines that are causing the crash and how to fix them. I've tried reallocating and malloc in many places and in different ways. I have a rough idea about which lines are problematic (my realoc lines?).



int main() {
 char * myString;// myString uesd to hold users input as entered
 myString = input1(); //Get users input and stor it in myString
 int sentence_count = 0; //To keep track of the total number of lines entered
 sentence * sentences; //array of sentences to hold all the sentences as an instance of sentence
 sentences = NULL;//initialize all values
 myprint(sentence_count, sentences, myString);//Print word and lines
 search(myString, sentence_count, sentences);//Search for word in sentences
 free(myString);//free the memory set aside 

 return 0;
}


char * input1() {
   char * line;// temporary variable to one hold input line
   char * myString;//myString uesd to hold users input as entered
   line = (char * ) malloc(1024 * sizeof(char));
   myString = malloc(1024);
   myString[0] = '\0';
   line[0] = '\0';
   while (line[0] != '\n') {
       printf(" Please input a line : ");
       fgets(line, 1000, stdin);
       strcat(myString, line);//add line to mystring
   }
   free(line);
   return myString;
}

I run it on Linux on PowerShell and get the following error

 Segmentation fault (core dumped)
  • 5
    Use a debugger. At a minimum that will tell you the exact line of code that triggers the seg fault and the stack trace leading up it. You should collect that info for yourself and also to post here. – kaylum Nov 17 '20 at 06:08
  • 4
    `strcat(myString, line)` that results in undefined behaviour (with seg fault being one possibility). Both parameters need to be strings. But `myString` contents are uninitialised. Init with `myString[0] = '\0';` – kaylum Nov 17 '20 at 06:10
  • I can't use a debugger. I can explain my circumstances if your interested. – hitomi layla Nov 17 '20 at 06:10
  • 1
    You have many useless allocations. A segmentation fault often happens because you're reaching out of an array. Learn how to debug by adding `printf` with indices and lengths. – Matthieu Nov 17 '20 at 06:12
  • @kaylum so I should add the line myString[0] = '\0'; as an initializer before using it? – hitomi layla Nov 17 '20 at 06:12
  • yes, I'm try my best with my debug skills. Thanks for the advice! – hitomi layla Nov 17 '20 at 06:14
  • could you please tell me which are the useless allocations? at lest the ones you've spot – hitomi layla Nov 17 '20 at 06:18
  • 2
    `line = realloc(line, strlen(line) + 1);` Well that's useless for starters. Why do you think you need a new buffer for each loop? And you are going to be in alot of pain if you can't use a debugger. Your code has bugs everywhere and it's not something a single post can fix. For example:`int search_size; search = (char * ) malloc(search_size * sizeof(char));` What do you think the value of `search_size` is when the `malloc` is called considering it hasn't been set to anything? – kaylum Nov 17 '20 at 06:22
  • I see. I’ll try my best to fix all you’ve mentioned. I’ll have the code updated first thing in the morning. Thanks a lot @kaylum let me know if there’s anything else you’ve noticed. Thanks again – hitomi layla Nov 17 '20 at 06:26
  • 2
    My advice: Apply best practice coding. Don't write all the code before going back to test it. Write a small function or even a small part of a function. Then write tests to verify that is working. Then go add a little bit more code. And so on. Add code to test that. Keep running all tests every time a small amount of code is added. – kaylum Nov 17 '20 at 06:30
  • @Matthieu if you read the comments you’ll understand why I’m asking if someone could meet over zoom. I really need one on one help. I have no clue where to look. I can’t afford paid help that’s why I ask here. I understand though it’s a learning community but my question never even get answers so no one benefits any way. Though I do understand why you removed that part. – hitomi layla Nov 17 '20 at 06:31
  • Thanks for the advice @kaylum I am trying to improve as much as possible. Your advice is much appreciated – hitomi layla Nov 17 '20 at 06:33
  • StackOverflow is made to help *other* people learn from *your* experience and discussion with experts. Having a private conversation with a single individual completely defeats that purpose. – Matthieu Nov 17 '20 at 07:42
  • I've updated the code! – hitomi layla Nov 17 '20 at 14:54

1 Answers1

2
 line = (char * ) malloc(1024 * sizeof(char));
 myString = malloc(1024);
 while (line[0] != '\n')

It is very clear here that, you did not initialize line character array. During first iteration, line[0] is unknown. Reading uninitialized variables is undefined behavior and can result in unexpected program behavior.

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
  • thanks I've fixed the code to include that but do I use malloc correctly? Is there a better way to use it? I'm a beginner and I hope to learn from all of you. I've updated the code, I appreciate any advice – hitomi layla Nov 17 '20 at 14:54
  • No need to cast the result of malloc. See this link for malloc allocations :https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc#:~:text=If%20you%20use%20malloc%20in,will%20return%20a%20void*%20type. – Krishna Kanth Yenumula Nov 17 '20 at 15:01
  • it gives this message when I try to "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – hitomi layla Nov 17 '20 at 16:55
  • @hitomi There is a tick button (below vote button), you need to press it to accept answer. You will get +2 reputation for accepting answer . Yes, 15 reputation needed to upvote. – Krishna Kanth Yenumula Nov 18 '20 at 03:09