none
Unhandled exception at 0x77692FD8 (ntdll.dll)

    Question

  • I wrote the following loop to read in lines from a text file

    // Reads in the title of a movie
    char* ReadTitle()
    {
    	char* str = calloc(sizeof(char), 80);		// Allocate space for 80 characters
    
    	getchar();									
    	ReadLine(str, 79);
    	
    	return str;
    }
    // Reads a line of input. Discards leading spaces and rest of line
    // if line exceeds length n
    int ReadLine(char str[], int n)
    {
    	int ch, i = 0;
    
    	// Skip beginning spaces
    	while ((ch = getchar()) == ' ')
    	{
    	}
    
    	while (ch != '\n' && ch != EOF)
    	{
    		if (i < n)
    			str[i++] = ch;
    
    		ch = getchar();
    	}
    	str[i] = '\0';
    
    	return i;
    }

    And in main, I called the functions as follows in a while loop:

    // Attempt to read in the list
    	while (scanf("%d", &year) != EOF)
    	{
    		list_insert(list, NULL, makeNode(ReadTitle(), year));
    	}

    Note: To keep this short, i will not include the code for makeNode. It returns a struct with two fields: a year and the string returned by ReadTitle(). makeNode works properly. When run, I always get the following error:

    Unhandled exception at 0x77692FD8 (ntdll.dll) in Test.exe: 0xC0000374: A heap has been corrupted (parameters: 0x776AD350).

    I checked my code and cannot identify the source of this corruption. Any help will be appreciated. Thank you

    Just in case, this is the code for list_insert(): (List represents a linked list and next and prev point to the next element)

    int list_insert(List* list, ListElmt* element, const Data data)
    {
    	ListElmt* newOne = malloc(sizeof(newOne));
    
    	// If space could not be allocated, return false
    	if (newOne == NULL)
    		return 0;
    
    	// Memory was allocated, assign data to new element
    	newOne->data.title = data.title;
    	newOne->data.year = data.year;
    	newOne->prev = NULL;
    	newOne->next = NULL;
    
    	// Handle insertion at the beginning
    	if (element == NULL)
    	{
    		if (list->size == 0)
    		{
    			list->tail = newOne;
    			list->head = newOne;
    		}
    
    		else
    		{
    			list->head->prev = newOne;
    			newOne->next = list->head;
    			list->head = newOne;
    		}
    	}
    
    	// Insertion somewhere else
    	else
    	{
    		newOne->next = element->next;
    
    		if (element->next == NULL)
    			list->tail = newOne;
    		else
    			element->next->prev = newOne;
    
    		element->next = newOne;
    		newOne->prev = element;
    	}
    
    	// Adjust the size of the list to account for the inserted element
    	list->size++;
    
    	return 1;
    }



    Frank A

    Monday, February 10, 2014 7:57 PM

Answers

  • Change malloc(sizeof(newOne)) to malloc(sizeof(ListElmt)) and see if this is enough.

    • Marked as answer by Solja Monday, February 10, 2014 8:30 PM
    Monday, February 10, 2014 8:23 PM

All replies

  • Change malloc(sizeof(newOne)) to malloc(sizeof(ListElmt)) and see if this is enough.

    • Marked as answer by Solja Monday, February 10, 2014 8:30 PM
    Monday, February 10, 2014 8:23 PM
  • Thanks a lot. Hours of being angry over this hid that error from me.

    Frank A

    Monday, February 10, 2014 8:30 PM