0

So I basically get a run-time error with the first strncpy that is encountered: "access violation reading location" and I'm not sure why since I did allocate memory for the "addedFrame".

code:

 void addFrame(link_t **list)
{
    bool validFrame = true;
    char frameName[MAX_NAME_SIZE] = { 0 };
    char framePath[MAX_PATH_SIZE] = { 0 };
    link_t* currentFrame = *list;

    link_t* addedFrame = (link_t*)malloc(sizeof(link_t));
    addedFrame->frame = (frame_t*)malloc(sizeof(frame_t));
    // Checking if malloc was succesfull
    if (!addedFrame->frame)
    {
        printf("Couldn't allocate memory\n");
        exit(-1);
    }

    // If in case of the head being null
    if (*list)
    {

        do
        {
            printf("Enter frame name: ");
            fgets(frameName, MAX_NAME_SIZE, stdin);

            // Resetting current frame back to the head
            currentFrame = *list;

            while (currentFrame->next != NULL)
            {
                if (!strcmp(frameName, currentFrame->next->frame->name))
                {
                    printf("A frame with the entered name already exists\n");
                    validFrame = false;
                }
                currentFrame = currentFrame->next;
            }

        } while (validFrame == false);

        currentFrame->next = addedFrame;

    }
    else
    {
        printf("Enter frame name: ");
        fgets(frameName, MAX_NAME_SIZE, stdin);
        frameName[strcspn(frameName, "\n")] = 0; // Removing the "\n" character and adding the terminating null
        *list = addedFrame;
    }

    strncpy(addedFrame->frame->name, frameName, MAX_NAME_SIZE);

    printf("Enter frame duration (in miliseconds): ");
    scanf("%d", &addedFrame->frame->duration);
    getchar(); // Clearing the buffer


    printf("Enter frame path: ");
    fgets(framePath, MAX_PATH_SIZE, stdin);
    framePath[strcspn(framePath, "\n")] = 0;
    strcpy(addedFrame->frame->path, framePath);
    printf("\n");


    addedFrame->next = NULL;
}

The above function is supposed to insert a new node at the end of the list with user inputted values.

EDIT Frame.h:

#ifndef FRAME_H
#define FRAME_H

#include <stdio.h>

struct Frame
{
    char            *name;
    unsigned int    duration;
    char            *path;  // may change to FILE*
};

typedef struct Frame frame_t;


#define MAX_PATH_SIZE (256)
#define MAX_NAME_SIZE (50)

#endif //FRAME_H

And linkedList.h:

#ifndef LINKEDLISTH
#define LINKEDLISTH


#include "Frame.h"

struct Link
{
    frame_t *frame;
    struct Link *next;
};

typedef struct Link link_t;
#endif
Daniel
  • 3
  • 2
  • Great. If it is the first `strncpy` then it is easy to reproduce and debug. What does your debugger tell you? If you didn't use it by now, now it is a perfect time to start using it. – Gerhardh Jun 22 '17 at 10:18
  • You shouldn't cast return value from malloc` but you should check if it is `NULL`. In general you don't check return values sufficiently. – Gerhardh Jun 22 '17 at 10:21
  • What's the type of `addedFrame->frame->name`? – SHG Jun 22 '17 at 10:21
  • As I mentioned in the question the strncpy couldn't access the memory of the addedFrame->frame->name however I can't understand why that happens since I did malloc the memory for that node... – Daniel Jun 22 '17 at 10:22
  • 2
    Please provide a [mcve]. You `name` field is probably incorrectly declared but we can't tell because you have not shown the definition of `list` and other important structures. – kaylum Jun 22 '17 at 10:23
  • The type of currentFrame->next->frame->name is char* – Daniel Jun 22 '17 at 10:25
  • Then it's wrong. You need to allocate memory and assign it to `name` first. Or declare `name` as a `char` array. – kaylum Jun 22 '17 at 10:25
  • Extra: `strncpy(addedFrame->frame->name, frameName, MAX_NAME_SIZE);` will not yield a nul-terminated string if `(strlen(frameName) >= MAX_NAME_SIZE)` solution: avoid strncpy() – joop Jun 22 '17 at 12:10

1 Answers1

0

According to what you said in the comment, the type of addedFrame->frame->name is char *. That's the reason for your error.

A char * has to be allocated, and until it is it's just a pointer that points to nothing.

You can either:

  • Allocate memory to it using malloc

    link_t* addedFrame = malloc(sizeof(link_t));
    addedFrame->frame = malloc(sizeof(frame_t));
    addedFrame->frame->name = malloc(MAX_NAME_SIZE);     // <---
    
  • Define it as a characters array instead of char * in struct Frame

    char *name; ---> char name[MAX_NAME_SIZE];
    
SHG
  • 2,516
  • 1
  • 14
  • 20