Skip to content
Advertisement

Why are the values in my dynamic 2D char array being overwritten?

I’m trying to build a process logger in C on Linux, but having trouble getting it right. I’d like it to have 3 colums: USER, PID, COMMAND. I’m using the output of ps aux and trying to dynamically append it to an array. That is, for every line ps aux outputs, I want to add a row to my array.

This is my code. (To keep the output short, I only grep for sublime. But this could be anything.)

JavaScript

The output of the for loop at the end looks like this.

JavaScript

But, from the puts(line) I get that the array should actually contain this:

JavaScript

So, apparently all the values are being overwritten and I can’t figure out why… (Also, I don’t get where the value 7194 in processes[0] = 7194 and processes[4] = 7194 comes from).

What am I doing wrong here? Is it somehow possible to make the output look like the output of puts(line)?

Any help would be appreciated!

Advertisement

Answer

The return value of strtok is a pointer into the string that you tokenise. (The token has been made null-terminated by overwriting the first separator after it with ''.)

When you read a new line with fgets, you overwrite the contents of this line and all tokens, not just the ones from the last parsing, point to the actual content of the line. (The pointers to the previous tokens remain valid, but the content at these locations changes.)

The are several ways to fix this.

  • You could make the tokens you save arrays of chars and strcpy the parsed contents.
  • You could duplicate the parsed tokens with (the non-standard) strdup, which allocates memory for the strings on the heap.
  • You could read an array of lines, so that the tokens are really unique.
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement