Codice: Seleziona tutto
char **explode(const char *string, const char *delimiter) {
}
Codice: Seleziona tutto
char **explode(const char *string, const char *delimiter) {
}
Codice: Seleziona tutto
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
const char *string = "ciao|come|stai";
char *tempString = malloc(sizeof(char) * (strlen(string) + 1));
char **matrix = malloc(sizeof(char *) * 3);
int i = 0;
strcpy(tempString, string);
char *token = strtok(tempString, "|");
printf("indirizzo token = %p, indirizzo tempString = %p\n", token, tempString);
while(token != NULL) {
matrix[i] = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(matrix[i++], token);
//free(token); come posso deallocare i token ? se decommento il programma non va a buon fine
token = strtok(NULL, "|");
printf("indirizzo token = %p, indirizzo tempString = %p\n", token, tempString);
}
while(i-- > 0)
printf("stringa = %s numero = %d\n", matrix[i], i);
printf("stringa originale temp = %s\n", tempString);
free(tempString);
return 0;
}
Ciao, guarda che antex ha gia' risposto alla tua domanda e vbextreme e' stato ulteriormente chiaro.ienaplinsky [url=http://forum.ubuntu-it.org/viewtopic.php?p=4561109#p4561109][img]http://forum.ubuntu-it.org/images/icons/icona-cita.gif[/img][/url] ha scritto:Si si ho letto la documentazione quindi i token non li devo deallocare basta che dealloco tempString?
Codice: Seleziona tutto
#define MAX_TOKEN 1024
int main()
{
char string[] = "ciao|come|stai";
char* vtk[MAX_TOKEN];
memset(vtk,0,sizeof(char*)*MAX_TOKEN);
int i = 0;
vtk[i] = strtok(string, "|");
for (; i < MAX_TOKEN && vtk[i]; vtk[++i] = strtok(NULL, "|"));
for (i = 0; i < MAX_TOKEN && vtk[i]; i++)
{
printf("token[%d]:%s\n",i,vtk[i]);
}
return 0;
}
Codice: Seleziona tutto
#define MAX_TOKEN 1024
int main()
{
char string[] = "ciao|come|stai";
char* tmptoken;
tmptoken = strtok(string, "|");
int countk = 1;
for (; (tmptoken = strtok(NULL, "|")); countk++ );
int i;
for (tmptoken = string, i = 0; i < countk; i++)
{
printf("token[%d]:%s\n",i,tmptoken);
tmptoken += strlen(tmptoken) + 1;
}
return 0;
}
Ho solo 256 MB di RAM e una lettura al minuto non posso occupare tutta questa memoria, per questo ogni elemento della matrice viene allocato in base alla lunghezza del tokenvbextreme ha scritto:Giusto per essere chiari,il tuo codice potrebbe diventare:Codice: Seleziona tutto
#define MAX_TOKEN 1024 int main() { char string[] = "ciao|come|stai"; char* vtk[MAX_TOKEN]; memset(vtk,0,sizeof(char*)*MAX_TOKEN); int i = 0; vtk[i] = strtok(string, "|"); for (; i < MAX_TOKEN && vtk[i]; vtk[++i] = strtok(NULL, "|")); for (i = 0; i < MAX_TOKEN && vtk[i]; i++) { printf("token[%d]:%s\n",i,vtk[i]); } return 0; }
si in effetti è giusto poi credo che basti deallocare solo il primo elemento della matrice che teoricamente dovrebbe puntare al primo carattere della stringa originariaantex ha scritto:Ma se devi memorizzare tutti i token, perché non eviti proprio di allocarli singolarmente e tieni solamente una copia (se ti serve) della stringa originaria? Questo ti fa anche risparmiare risorse computazionali (le malloc non sono velocissime, invece di averne N ne hai una sola).
Codice: Seleziona tutto
char **explode(const char *string, const char *delimiter) {
char *tempString = malloc(sizeof (char) * (strlen(string) + 1)); // questa la devo copiare per forza il parametro string appartiene a una struttura che sarà deallocata
char *token = tempString;
int row = 0;
char **result = NULL;
strcpy(tempString, string);
row = contElementSplit(tempString, delimiter);
result = malloc(sizeof (char *) * (row + 1));
for(; i < row; i++) {
result[i] = token;
token += strlen(token) + 1;
}
result[i] = NULL;
return result;
}
int contElementSplit(char *string, const char *delimiter) {
int i = 0;
char *token = strtok(string, delimiter);
for (; (token = strtok(NULL, "|")); i++ );
return i;
}
Codice: Seleziona tutto
==5190== HEAP SUMMARY:
==5190== in use at exit: 0 bytes in 0 blocks
==5190== total heap usage: 2 allocs, 2 frees, 27 bytes allocated
==5190==
==5190== All heap blocks were freed -- no leaks are possible
==5190==
==5190== For counts of detected and suppressed errors, rerun with: -v
==5190== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Visualizzano questa sezione: 0 utenti iscritti e 13 ospiti