简体   繁体   English

如果我为结构的指针分配内存,为什么在为结构分配变量时会丢失结构中的数据?

[英]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.我在我的头文件中定义了 struct Task,它包含在我的 C 文件的顶部。 I typedef it with the line typedef struct Task Task;我使用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.现在,在我的代码中,我设置了我的结构的 waitTime 变量部分,然后 taskName 就消失了,变成空的或一组奇怪的字符,就像它只包含一段数据一样。 What's happening here?这里发生了什么事?

Am I supposed to allocate space for Task instead of Task* to avoid the memory loss?我是否应该为Task而不是Task*分配空间以避免内存丢失?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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