简体   繁体   中英

Why is it that I lose data in my struct when allocating variables for my struct if I allocate memory for a pointer of the struct?

So I have a struct with various variables within. My struct is:

struct Task{
    Char* taskName
    arrivalTime
    burstTime
    waitTime
}

I defined the struct Task in my header file which is included at the top of my C file. I typedef it with the line typedef struct Task Task; .

After assigning memory for:

Task** tasks = malloc(sizeof(Task*)*100);

and for as many tasks as I need, I allocate:

tasks[i] = malloc(sizeof(Task*));
tasks[i]->taskName = malloc(sizeof(char*));

The rest of variables are non-pointers, so my understanding is I do not have to manual memory allocation, even if I am using a Task**. I set the arrival time and the burst time here as well. All information is stored properly.

Now further down in my code I set the waitTime variable part of my struct, then the taskName just vanishes and becomes empty or a weird set of characters, like it only hold a piece of the data. What's happening here?

Am I supposed to allocate space for Task instead of Task* to avoid the memory loss?

You loosing data because space for the data is not allocated properly. Hope that this minimal example can give you an idea how to deal with space allocation for structures, array of pointers and strings. Of course, a real program should release the allocated memory when it is no longer needed.

/* 
 *      task.c
 * */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAXNTASK 100

typedef struct sTask {
    char* taskName;
    time_t arrivalTime;
    time_t burstTime;
    time_t waitTime;
} Task;


Task* createTask(char* task_name )
{
    Task *task = malloc(sizeof(Task)); /* allocate space for type Task */
    task->taskName = malloc(strlen(task_name)+1); /* make space for task name + string termintor */
    strcpy(task->taskName, task_name);  /* copy string task_name to structure member task->taskName */
    task->arrivalTime = time(NULL);  /* get current time */
    task->burstTime = 0;             /* just initialize burtsTime and waitTime */
    task->waitTime = 0;
    return task; 
}   


int main( int argc, char* argv[])
{
    Task** tasks = malloc(sizeof(Task*) * MAXNTASK);  /* alocate space for array of pointers to type Task */
    char task_name[32];                               /* temporary variable, holds generated name of a task */
    
    for( int i = 0; i < MAXNTASK; i++)                 /* populate array with generated tasks: */
    {
        sprintf(task_name, " Task No. %3.3d", i );     /* generate task name */
        tasks[i] = createTask(task_name);              /* create Tsk and store pointer into array */
    }
        
    for( int i = 0; i < MAXNTASK; i++)                 /* print out tasks */
       printf(" %3.3d %s %s\n", i, tasks[i]->taskName, ctime(&tasks[i]->arrivalTime));  /* function ctime converts time_t to string */
    
    
    return 0;
}

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