-1

i'm facing a error right now on C, using Visual Studio and working with Linked Lists for the first time, i'm trying to make the project a little more structed, and because of that i'm facing some challenges. The error that i'm facing is, when i try to insert an element at a linked list my progam just crashes and give me this error :

Exception generated at 0x500CC944 (ucrtbased.dll) in Lesson_LinkedLists.exe: 0xC0000005: access violation while writing to local 0xCDCDCDCD.

I'm confused with this error, because it really does not support me with enough information so i can search over the internet.

Here are my structs, memory alocation and insert function :

typedef struct Word
{
    char Text[Max_Letters];
}*pt_Word;

typedef struct Node_Element
{
    pt_Word Word_Data;
    struct Node_Element * Next_Node_Element;
}*pt_Node_Element;

typedef struct List
{
    char Language[2];
    int Number_Words_List;
    pt_Node_Element Header;
}*pt_List;

pt_List List_Dynamic_Memory();

pt_Node_Element Node_Dynamic_Memory();

void Insert_Element_Begin(pt_Node_Element Node_Element, pt_List List_Pointer);

int main()
{
    int option;
    pt_Node_Element Node;
    pt_List List;
    List = List_Dynamic_Memory();
    Node = Node_Dynamic_Memory();

    do
    {
        option = menu();
        switch (option)
        {
            case 1: 
            {
                printf("Diga-me uma palavra : \n");
                //  fflush(stdin);
                gets_s(Node->Word_Data->Text);
                Insert_Element_Begin(Node, List);
                break;
            }   
            case 2: 
            {
                Display_List(Node, List); break;
            }
        }
    } while (option != 0);
    //system("Pause");
}

pt_List List_Dynamic_Memory()
{
    pt_List List_Pointer;

    List_Pointer = (pt_List)malloc(sizeof(struct List));

    List_Pointer->Header = NULL;
    List_Pointer->Number_Words_List = 0;

    return List_Pointer;

};

pt_Node_Element Node_Dynamic_Memory()
{
    pt_Node_Element  Node_Pointer;

    Node_Pointer = (pt_Node_Element)malloc(sizeof(struct Node_Element));

    Node_Pointer->Next_Node_Element = NULL;

    return Node_Pointer;
};

void Insert_Element_Begin(pt_Node_Element Node_Element, pt_List List_Pointer)
{
    List_Pointer->Number_Words_List++;

    if (List_Pointer->Header == NULL)
    {
        List_Pointer->Header = Node_Element;
        Node_Element->Next_Node_Element = NULL;

        return;
    }

    Node_Element->Next_Node_Element = List_Pointer->Header;
    List_Pointer->Header = Node_Element;
};

I'm wondering if it's something related to memory alocation, i'm a beginer so i'm sorry if it's a stupid error. Thanks for the help in advance.

  • 3
    Unrelated, try not using typedefs to hide pointer types. In all but two specific cases (neither of which apply here), it does more harm than good, and makes the code *less* readable and maintainable. C programmers *want* to see those asterisks when pointers are being used; it's their calling card. Also, [don't cast malloc in C programs](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – WhozCraig Apr 27 '18 at 18:12
  • you did not initialize Node_Element.Word_Data ever – pm100 Apr 27 '18 at 18:14
  • 1
    (I think it is very related.) In `Node_Element` you have a pointer to `struct Word`. But in `Node_Dynamic_Memory` you never `malloc` it. So you pass an uninitialized pointer to `get_s`(). – 001 Apr 27 '18 at 18:14
  • In Node_Element , Word_Data pointer is un initialized. I dont see memory being allocated. – NishanthSpShetty Apr 27 '18 at 18:17
  • My problem is, how i'm suposed to initialize a string? Thanks for the answers by the way. Sorry i'm a noob. – Diogo Oliveira Apr 27 '18 at 18:19
  • The man page for `char *gets_s(char *buffer, size_t sizeInCharacters);` shows you have a missing size argument in `gets_s(Node->Word_Data->Text);`. What did the compiler tell you about that? – Weather Vane Apr 27 '18 at 18:22
  • @DiogoOliveira [Google it](https://stackoverflow.com/questions/4142745/initialize-a-string-in-c-to-empty-string) – Tormund Giantsbane Apr 27 '18 at 18:23
  • I got no error on the compiler, when i run the code tho it gives me this Exception generated at 0x500CC944 (ucrtbased.dll) in Lesson_LinkedLists.exe: 0xC0000005: access violation while writing to local 0xCDCDCDCD. – Diogo Oliveira Apr 27 '18 at 18:23

1 Answers1

0

As mentioned in the comments, you never allocate space for Word_Data. To fix that, you can use malloc():

pt_Node_Element Node_Dynamic_Memory()
{
    pt_Node_Element  Node_Pointer;

    Node_Pointer = (pt_Node_Element)malloc(sizeof(struct Node_Element));

    // Alloc mem for this too
    Node_Ponter->Word_Data = malloc(sizeof(struct Word));

    // You should also always check the value returned from malloc

    Node_Pointer->Next_Node_Element = NULL;

    return Node_Pointer;
};

It is also mentioned in the comments about hiding the fact that all those types are pointers. Doing that makes it easy to forget you are dealing with pointers. I advise against it.

Lastly, I see no reason Word_Data has to be a pointer. If it wasn't you would not have had this error:

struct Node_Element
{
    struct Word Word_Data;  // Does not need malloc
    struct Node_Element * Next_Node_Element;
};

And don't forget to free everything you malloc.

001
  • 13,291
  • 5
  • 35
  • 66