2

I am a novice programmer and I hagly appreciate any advice with my problem here.

I've made a procedure that gets a string in buffer and parses it in three cunks, separated by the first 2 ";".

What I tried to do is to pass 3 char pointers in where I will store my parsed string. But all I got in the calling function is memory garbage. What am I doing wrong?

void parseomensaje(char buf[256], char **idNodo, char **idMensaje, char **mensaje){

    char *temp; 
    temp=(char *)malloc(sizeof(buf));


    strcpy(temp, buf);
    printf("\ntemp adentro de la funcion = %s\n", temp);
    idNodo = strtok (temp,";");
    idMensaje = strtok (NULL, ";");
    mensaje = strtok (NULL, "\0");

    printf("\nADENTRO\nidNodo: %s\nidMensaje: %s\nmensaje: %s", idNodo, idMensaje, mensaje);

}

this function is called this way:

     char *idnod=NULL;
     char *idmen=NULL;
     char *men=NULL;

     idnod=(char *)malloc(sizeof(buffer));
     idmen=(char *)malloc(sizeof(buffer));
     men=(char *)malloc(sizeof(buffer));

     parseomensaje (buffer, &idmen, &idnod, &men);

after parseomensaje is executed buffer contains its original string, but idmen, idnod and men are blank.

I was reading from tutorials that pointers names are pointers itself, so it is the same thing as passing a parameter by reference, but in case of a string I need to pass the pointer address to a pointer to pointer... I was reading it from here, but I'm still trying to figure it out.

PD: I apologize for my English, please feel free to point any mistakes in my writing. :)

Community
  • 1
  • 1
Zeke
  • 21
  • 2

2 Answers2

3

This is incorrect:

char *temp; 
temp=(char *)malloc(sizeof(buf));

as the array will degrade to a char* within the function, so only sizeof(char*) bytes are being allocated (typically 4 or 8 bytes). If the actually length of buf is greater than 4 or 8 bytes then the program has undefined behaviour as the strcpy() will be writing beyond the bounds of the array. Basically:

void parseomensaje(char buf[256], char **idNodo, char **idMensaje, char **mensaje){

is equivalent to:

void parseomensaje(char* buf, char **idNodo, char **idMensaje, char **mensaje){

If you are, as you say, a novice programmer I would recommend avoiding dynamic memory allocation until you get more comfortable with the language. Modify the program to use fixed sized arrays instead of dynamically allocated memory (and prevent writing beyond the bounds of the arrays). Once you have that working and fully understand it then attempt to use dynamically allocated memory.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Thanks for the reply, I am indeed unconforrtable with malloc, but if I comment that sentence I got a SegFault error. But I am sure that I am fully responsible for that :) – Zeke Feb 13 '14 at 16:03
  • @user3306636, if you just uncomment the `malloc` then `temp` is an unitialized pointer. Memory is required to copy into: `char temp[256];` for example. – hmjd Feb 13 '14 at 16:05
0

First of all you have multiple memory leaks in your program. Consider freeing every single memory chunk you've allocated thanks to malloc once you don't need them anymore.

As regard your function:

void parseomensaje(char buf[256], char **idNodo, char **idMensaje, char **mensaje)

Why do you pass char** pointers on your function? Pass char* instead. Because strtok is declared this way:

char *strtok(char *str, const char *delim);

Moreover, you don't need to allocate any memory before calling parseomensaje since strtok returns a pointer on your own memory, not new allocated one.

Geoffrey R.
  • 1,907
  • 1
  • 19
  • 32