struct - C Variable changes after initializing other variables -



struct - C Variable changes after initializing other variables -

i writing command-line todo list programme in c , getting weird behavior. todos stored in structs contain int priority , char[128] name. created using function takes in these parameters , returns allocated , initialized struct values.

the returned structs correct, after initializing 8 more struct variables, value of first-initialized structs begins change. guess previous values beingness overwritten new ones due not plenty memory beingness allocated. i've fixed problem temporarily increasing size fed malloc() sizeof(taskp) sizeof(int) + sizeof(char[128]). however, sure there improve way , much appreciate explanation of why malloc(sizeof(taskp) not allocating plenty memory (if case) , proper convention.

here code:

#include <stdio.h> #include <stdlib.h> #include <string.h> struct task { char description[128]; /* description of task */ int priority; /* task priority */ }; typedef struct task* task; task createtask (int priority, char *desc) { task newtask = malloc(sizeof(task)); if (newtask) { newtask->priority = priority; strcpy(newtask->description, desc); } homecoming newtask; } int main(int argc, const char * argv[]) { task tasklist[10]; task task1 = createtask(9, "task 1"); printf("task1: %d\n", task1->priority); /* set printfs */ task task2 = createtask(3, "task 2"); printf("task1: %d\n", task1->priority); /* after every phone call */ task task3 = createtask(2, "task 3"); printf("task1: %d\n", task1->priority); /* createtask() */ task task4 = createtask(4, "task 4"); printf("task1: %d\n", task1->priority); /* watch value */ task task5 = createtask(5, "task 5"); printf("task1: %d\n", task1->priority); /* of task1 alter */ task task6 = createtask(7, "task 6"); printf("task1: %d\n", task1->priority); task task7 = createtask(8, "task 7"); printf("task1: %d\n", task1->priority); task task8 = createtask(6, "task 8"); printf("task1: %d\n", task1->priority); task task9 = createtask(1, "task 9"); printf("task1: %d\n", task1->priority); task task10 = createtask(0, "task 10"); printf("task1: %d\n", task1->priority); homecoming 0; }

and here output:

task1: 9 task1: 9 task1: 9 task1: 9 task1: 9 task1: 9 task1: 9 task1: 0 task1: 1802723700 task1: 1802723700 update

i literally figured out problem seconds after posting stack overflow. how embarrassing. problem allocating plenty memory pointer (task), has size of 8 bytes, instead of memory struct task, has size of 132 bytes. give thanks patiently answered , sorry wasting time.

this problem.

task newtask = malloc(sizeof(task));

you allocating plenty memory hold pointer, not struct task.

change to:

task newtask = malloc(sizeof(*newtask));

c struct malloc sizeof

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -