@evg_96

Почему вылетает исключение в MSVC?

В чем проблема или как это можно пофиксить? В 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;
}
  • Вопрос задан
  • 126 просмотров
Пригласить эксперта
Ответы на вопрос 1
Программа падает тут
free(current);
Падает, потому что после освобождения памяти у current вы пытаетесь потом пользоваться этой памятью. Соответственно возникает Access Violation/

Рабочий вариант кода
#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;
}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы