Hey everyone,
I was messing around with pointers trying to get a better understanding of them, and I came across something that I haven't been able to solve in regards to freeing a double pointer I malloced.
Code:
int main(int argc, char** argv) {
char** temp = (char**)malloc(argc * sizeof(char*));
//free(temp); <-------------------------------------------- free's fine here
char** temp2;
int i;
for (i = 0; *argv != NULL; i++)
*(temp + i) = *argv++;
*(temp + argc) = NULL;
temp2 = temp;
while (*temp != NULL)
printf("%s", *temp++);
free(temp2); <--------------------------------------------- doesn't free fine here (SIGABRT)
return (EXIT_SUCCESS);
}
I don't understand why it free's fine right after I malloc and then when I add strings to the double pointer, print them, and attempt to assign the char** temp to char** temp2 (equivalently storing the value of temp, which was malloced, into temp2 because I increment temp in the while) I run into an error when I free what I malloced...
This might be because I'm ignorant on the operation of malloc, but if someone could set me straight, I'd appreciate it. Thanks.