E
E
evg_962018-04-18 09:46:24
C++ / C#
evg_96, 2018-04-18 09:46:24

Why is an exception thrown in MSVC?

What is the problem or how can it be fixed? Everything works fine in gcc.
5ad6e960c834a020143645.png

#define _CRT_SECURE_NO_WARNINGS

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

#define TSIZE 45

struct film
{
    char title[TSIZE];
    int rating;
    struct film * next;
};

char * s_gets(char * str, int n);

int main(int argc, char * argv[])
{
    struct film * head = NULL;
    struct film * prev, * current;
    char input[TSIZE];

    puts("Input first name of movie: ");

    while (s_gets(input, TSIZE) != NULL && input[0] != '\0')
    {
        current = (struct film *) malloc(sizeof(struct film));

        if (head == NULL)
            head = current;
        else
            prev->next = current;

        current->next = NULL;

        strcpy(current->title, input);

        puts("Input your rating: ");
        scanf("%d", &current->rating);

        while (getchar() != '\n')
            continue;

        puts("Input the next name of movie: ");

        prev = current;
    }

    if (head == NULL)
        printf("Data not found");
    else
        printf("List of movies:\n");

    current = head;

    while (current != NULL)
    {
        printf("Movie: %s, rating: %d\n", current->title, current->rating);

        current = current->next;
    }

    current = head;

    while (current != NULL)
    {
        free(current);

        current = current->next;
    }

    printf("The programm is completed.\n");

    _getch();

    return 0;
}

char * s_gets(char * str, int n)
{
    char * value;
    char * find;

    value = fgets(str, n, stdin);

    if (value)
    {
        find = strchr(str, '\n');

        if (find)
            *find = '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return value;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2018-04-18
@SaNNy32

The program crashes here
Crashes because after you free the memory of current you try to use that memory later. Accordingly, there is an Access Violation /
Working version of the code

#define _CRT_SECURE_NO_WARNINGS

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

#define TSIZE 45

struct film
{
  char title[TSIZE];
  int rating;
  struct film * next;
};

char * s_gets(char * str, int n);

int main(int argc, char * argv[])
{
  struct film * head = NULL;
  struct film * prev, *current;
  char input[TSIZE];

  puts("Input first name of movie: ");

  while (s_gets(input, TSIZE) != NULL && input[0] != '\0')
  {
    current = (struct film *) malloc(sizeof(struct film));

    if (head == NULL)
      head = current;
    else
      prev->next = current;

    current->next = NULL;

    strcpy(current->title, input);

    puts("Input your rating: ");
    scanf("%d", &current->rating);

    while (getchar() != '\n')
      continue;

    puts("Input the next name of movie: ");

    prev = current;
  }

  if (head == NULL)
    printf("Data not found");
  else
    printf("List of movies:\n");

  current = head;

  while (current != NULL)
  {
    printf("Movie: %s, rating: %d\n", current->title, current->rating);

    current = current->next;
  }

  current = head;

  while (current != NULL)
  {
    auto next = current->next;
    free(current);


    current = next;
  }

  printf("The programm is completed.\n");

  _getch();

  return 0;
}

char * s_gets(char * str, int n)
{
  char * value;
  char * find;

  value = fgets(str, n, stdin);

  if (value)
  {
    find = strchr(str, '\n');

    if (find)
      *find = '\0';
    else
      while (getchar() != '\n')
        continue;
  }

  return value;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question