Problems handling pointers in a dynamic matrix
I'm fighting with pointers :(. I try to make a function which do something similar to what argv returns. I mean, I want it to divide a order into some strings, each one pointed by a different pointer in order to refer them through square brackets. (ie: order[0], order[1]).
I want to do it for any number of words so I use dinamic memory. In order to divide the order I use strtok and it works correctly. The problem is to handle the pointers:
In resul** I reserve memory with realloc for the pointers which point each word, therfore, while there are tokens I do the following:
- I increase the size of resul
- I reserve memory for the pointer to the token
- I asign the token
(text to stop markdown freakout)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
char linea[] = "send UDP 4500 50";
char **resul=NULL;
int numTokens,conta2;
char *sep=" ";//character which divides the orders
int conta=0; //counter of the number of tokens found
char *saveptr,*token;
for(token=strtok_r(linea,sep,&saveptr); token!=NULL; token=strtok_r(NULL,sep,&saveptr)){
printf("token: %s\n",token);
printf("size: %i\n",(conta+1)*sizeof(char*));开发者_如何学运维
resul = (char **)realloc(resul,(conta+1)*sizeof(char*)); //Increase the size for the array of pointers
resul[conta]= malloc(sizeof(char*)); //Reserve size for the pointer
resul[conta]=token; //Asign word to the pointer
conta++;
}
//Print the results
for(conta2=0;conta2<conta;conta2++)
printf("resul: \"%s\"\n",resul[conta2]);
//free memory
for(conta2=0;conta2<conta;conta2++){
printf("liberando: %i\n",conta2);
free(resul[conta2]);
}
free(resul);
return(0);
}
The output is:
$ ./lectura_consola
token: send
size: 4
token: UDP
size: 8
token: 4500
size: 12
token: 50
size: 16
resul: "send"
resul: "UDP"
resul: "4500"
resul: "50"
liberando: 0
*** glibc detected *** ./lectura_consola: free(): invalid pointer: 0xbffb7f1b ***
If I run it with valgrind in order to see the failures in memory, in the free(resul[0]) step happens that:
liberando: 0
==8372== Invalid free() / delete / delete[]
==8372== at 0x40257ED: free (vg_replace_malloc.c:366)
==8372== by 0x80489FA: main (lectura_consola.c:92)
==8372== Address 0xbee8e28b is on thread 1's stack
With this code, the program save and print the words correctly but at the moment of freeing the memory it explotes and when it tries to free resul[0] says: Invalid pointer.
Could you tell me where the error is? I'm completely lost and any help would be useful
The problem here is that you're trying to free memory that you didn't allocate. You have this code in your first loop:
resul[conta]= malloc(sizeof(char*)); //Reserve size for the pointer
resul[conta]=token; //Asign word to the pointer
The second statement overwrites the result from the first statement so that when you call free(resul[conta])
, you're trying to free the token
(which is a pointer into a string) rather than the pointer that you allocated.
You don't need that first statement in any case. Your realloc
call reserves space for an array of pointers. That call to malloc
is just allocating sizeof(char*)
bytes to no good effect (i.e. wasting memory). And since the value is simply being overwritten, you'd end up leaking that memory.
Just remove the call to malloc
and your code should work.
As far as I remember strtok doesn't allocate memory just insterts \0 between tokens and this means that you free read only code memory - send UDP 4500 50" is such
精彩评论