0

I'm trying to add a frame to a linked list of such structures, but instead of adding a new structure to the list each time, the program adds nothing, can anyone tell me what the problem is? Or give me a better exercise method?

The structures:

typedef struct Frame
{
    char* name;
    int duration;
    char* path;

} Frame;


// Link (node) struct
typedef struct FrameNode
{
    Frame* frame;
    struct FrameNode* next;
} FrameNode;

The functions:

FrameNode* addFrame(Frame* frame)
{
    char name[100] = { 0 };
    char path[100] = { 0 };
    int dur = 0;
    Frame* p = (Frame*)malloc(sizeof(Frame));

    printf("*** Creating a new frame ***\n");
    printf("Please insert frame path:\n");
    fgets(path, 100, stdin);
    path[strcspn(path, "\n")] = 0;
    strcpy(p->path, path);

    printf("Please insert frame duration <in miliseconds>:\n");
    scanf("%d", &dur);
    getchar();
    p->duration = dur;

    printf("Please chooce a name for a new frame:\n");
    fgets(name, 100, stdin);
    name[strcspn(name, "\n")] = 0;
    strcpy(p->name, name);
    while (list != NULL)
    {
        if (strcmp(list->frame->name, p->name) == 0)
        {
            printf("The name is already taken, Please enter another name\n");
            fgets(name, 100, stdin);
            name[strcspn(name, "\n")] = 0;
            strcpy(p->name, name);
        }
        list = list->next;
    }
    free(p);
    return p;
}

FrameNode* insertAtEnd(FrameNode** list, Frame* fr)
{
    if (*list)
    {
        FrameNode* help = *list;
        FrameNode* tmp = (FrameNode*)malloc(sizeof(FrameNode));
        tmp->frame = addFrame(fr);
        while (help->next != NULL)
        {
            help = help->next;
        }
        help->next = tmp;
    }
    else
    {
        list = (FrameNode*)malloc(sizeof(FrameNode));
        FrameNode* tmp = (FrameNode*)malloc(sizeof(FrameNode));
        tmp->frame = addFrame(fr);
        list = tmp;
    }
}

I really need help, I have to submit it by Sunday. If you notice any more problems please tell me Thank you to everyone!!!!!

5
  • What is the frame parameter for? It does not seem to be used. Commented Jun 12, 2020 at 12:25
  • So delete it? And do the function get nothing? Commented Jun 12, 2020 at 12:27
  • What is the global list variable supposed to point to? The list = list->next; line changes this variable so that it eventually becomes NULL. If it was pointing to the start of a list of nodes it no longer does so. Perhaps you should use a local variable there to avoid clobbering the global variable. Commented Jun 12, 2020 at 12:27
  • My problem is that the structure can't be printed afterwards and things don't get well (I deleted the free one) and simply if I print it shows an error: 0xC0000005: Access violation reading location 0x00000003. Commented Jun 12, 2020 at 12:30
  • When you check if the chosen frame name already exists, find a match, and enter a new name, you are not checking the new name against the previous entries in the list. Commented Jun 12, 2020 at 12:33

2 Answers 2

1

A separate struct for a linked list usually adds nothing but complexity. Why not just use:

struct my_frame {
    struct my_frame *next;
    char *name;
    int duration;
    char *path;
}

It reduces complexity and simplifies memory allocation.

A few personal preferences: I suggest using a prefix to prevent generic names like "frame". I also suggest dropping the typedef. It does nothing for you. It only adds obscurity. Drop the capitals in type names. If you want to program in C, learn C don't try to make it look like C++, C# or Java.

Sign up to request clarification or add additional context in comments.

1 Comment

It's a job given to me in school, I'm committed to doing the buildings in this way and all kinds of annoying things. My problem is why does it not include the structures in the linked list?
0

At the end of the addFrame function you free p, but then you return it.

So in the line tmp->frame = addFrame(fr); in the function insertAtEnd, you assign a pointer that has been freed already, whose contents are unknown.

The solution is to simply remove the line free(p) in addFrame, but make sure that you have a free'ing mechanism later in your program for this list.

Edit: also, I now see that in the line list = tmp;, you are actually assigning a value to the local variable list, and what you should be doing is *list = tmp .

1 Comment

I deleted the line of free but it still doesn't work ... On the contrary, now there is also an error and it is impossible to absorb more than three buildings and also what we have not printed. This is the error that appears: Exception thrown at 0x00031433 in RikaLevi.exe: 0xC0000005: Access violation reading location 0x0000002B.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.