简体   繁体   中英

Working with linked list + struct C

Here is the linked list and the struct:

#define MAX_PATH_SIZE (256)
#define MAX_NAME_SIZE (50)
struct Frame
{
    char *name;
    unsigned int duration;
    char *path;  // may change to FILE*
};

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

typedef struct Link link_t;

And here's my function:

link_t* createFrame(char name[], int duration, char path[]){
frame_t * temp = (frame_t*)malloc(sizeof(frame_t));
temp->duration = duration;

strncpy(temp->name, name,MAX_NAME_SIZE);
strncpy(temp->path, path,MAX_PATH_SIZE);
link_t* newFrame = (link_t*)malloc(sizeof(link_t));

newFrame->frame = temp;
return newFrame;
}

The problem is that the function stop working in the line "strncpy(temp->name)..", the weird thing is that the temp->duration is working but it doesn't work with strings. Error: "Unhandled exception at 0x0F744645 (msvcr120d.dll)"

You didn't allocate memory for your name, now they point to unknown location and is undefined behaviour.

temp->name = malloc((MAX_NAME_SIZE + 1) * sizeof(*temp->name));
temp->path = malloc((MAX_PATH_SIZE + 1) * sizeof(*temp->path));
temp->name[MAX_NAME_SIZE] = 0; //Manually add null termination
temp->name[MAX_PATH_SIZE] = 0; //Manually add null termination
strncpy(temp->name, name,MAX_NAME_SIZE);

Now, memory is allocated for name and path and you are able to copy data for name and path.

Or, if you want, you can define your structure like this:

struct Frame {
    char name[MAX_NAME_SIZE + 1];
    unsigned int duration;
    char path[MAX_PATH_SIZE + 1];
};

Then you won't need to call malloc for name and path separatelly as memory will be allocated on first malloc for structure already.

You need to allocate memory to store strings with strncpy(), in your malloc you allocated only enough to store sizeof(struct Frame) bytes.

You may want to try this instead of strncpy...

temp->name = strndup(name, MAX_NAME_SIZE);
temp->path = strndup(path,MAX_PATH_SIZE);

...if you are insisting on restricting the max size of strings.

Try this :

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

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

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

typedef struct Link link_t;

link_t* createFrame(char name[MAX_NAME_SIZE], int duration, char path[MAX_PATH_SIZE]){
    frame_t * temp = (frame_t*)malloc(sizeof(frame_t));
    temp->duration = duration;

    strncpy(temp->name, name, MAX_NAME_SIZE);
    strncpy(temp->path, path, MAX_PATH_SIZE);
    link_t* newFrame = (link_t*)malloc(sizeof(link_t));

    newFrame->frame = temp;
    return newFrame;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM