Home > database >  Why am I losing data in C programming when assigning to variables in my struct?
Why am I losing data in C programming when assigning to variables in my struct?

Time:11-27

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

struct Task{
    Char* taskName
    arrivalTime
    burstTime
    waitTime
    …

}

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. Likewise, I have two other variables in the struct (which I didn't list) startTime and endTime, which when they are set, the taskName also vanishes. What's happening here?

Am I supposed to allocate space for Task instead of Task*?

UPDATE

As per request in the comments. I did use a typedef in my header file.

typedef struct Task Task;

This header file is included in my c file, and there is no compiler errors. I'm still pretty new to the language, but I've always done it this way and has worked in the past.

CodePudding user response:

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;
}
  • Related