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 Frank_Ibem 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 Frank_Ibem 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
  • Hi,

    Sorry for opening an old thread. I am getting this error Unhandled exception at 0x777F9D71 (ntdll.dll) when I try to call C++ function from C#.

    Is there a way to figure out what's wrong without looking inside that dll? It is developed by my colleagues and I can find the source code if needed, but I better spare myself if possible.

    UPDATE. After spending last night and the whole day today in various tests trying to make it work we finally seem to determine that the problem is indeed in the latest version of the dll. I was able to make it work with the older version of the dll.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    • Edited by Naomi N Wednesday, October 18, 2017 10:33 PM
    Wednesday, October 18, 2017 4:21 AM