Pergunta

I'm trying to make a program that basically picks a specific piece of source code and adds some other specific code into it. The program is just to big to put it all inside my question, but basically I have this "actors" struct:

typedef struct actors_s {
    int num;
    char *src_path;
    char *project_path;
    int *papify;
    char *actor_path[];
} actors_s;

As you can see these are almost all pointers and the last one is an array of strings. This needs to be done this way because the number of "actor elements" depends on the input every time.

The problem:In an specific test case, I have a case with 'num' members in the actor_path array. Then I first call malloc only once this way:

*actors->actor_path = malloc(actors->num);

My logic tells me I shouldn't be using the '*' operator here but without it I get an error, this is possibly where the problem is. So, a function is called that allocates a new memory space for every new member (never going further of 'num' members):

int size = strlen(name)+strlen(actors->project_path)+strlen("/src/")+strlen(".c")+4;
actors->actor_path[i] = malloc(size);

(The malloc calls are properly tested if successful in the actual program) This is called inside a function that is called for every "actor_path" element. In this test example I have three actors.

Mysteriously enough, on the third call of this malloc, the src_path element of the struct, which was properly allocated and set to a string once in the beginning of the program (and never touched again) is freed (I think so, at least it is changed into random numbers and symbols if I watch it in debug mode).

Anyone has any idea how and why is this possible? How do I fix this?

Thanks in advance.

EDIT:

Here are some screenshots from the debug watch window: https://i.stack.imgur.com/6VArW.jpg

First call to malloc: all OK.

Second call to malloc: all OK.

Third call to malloc: src_path gets erased!!

Foi útil?

Solução

[] in latest array element is called flexible array member. It means structure have an array that starts just after structure itself, and its size is unspecified. You have to allocate memory for this manually. E.g.

actors_s *actor = malloc(sizeof(*actor) + sizeof(char*) * num);

Then just assign at most num elements into actor_path (each element is pointer to char).

Outras dicas

  • about *actors->actor_path = malloc(actors->num);

    actors->actor_path is an array of pointers, so *actors->actor_path is the first pointer in actors->actor_path, i.e. actors->actor_path[0].

    When doing this, you actually allocate actors->num bytes memory for actors->actor_path[0].

    Now, accessing actors->actor_path[0] is OK, while accessing actors->actor_path[1], actors->actor_path[2], actors->actor_path[3],... may cause problems, say, rewrite src_path...

  • about the solution

    @keltar is right. In this way, the resource for actors->actor_path[0], actors->actor_path[1], actors->actor_path[2], ..., actors->actor_path[num - 1] is correctly allocated.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top