last day (15 days later) » 

8:46 PM
4
A: Splitting a String and returning an array of Strings

brunoA solution is : #include <stdlib.h> #include <string.h> #include <stdio.h> char ** split(const char * str, const char * delim) { /* count words */ char * s = strdup(str); if (strtok(s, delim) == 0) /* no word */ return NULL; int nw = 1; while (strtok(NULL, delim) != 0) ...

 
@alk I added an other solution, but to be frank for me to verify these is enough memory in that kind of application is mainly a loss of time, the day you have not enough memory be sure you will have other problems ^^
@alk a pointer is a pointer, while the goal is to check the equality that works. But return &str; while str is a parameter ? Are you serious ?
@alk if the result is not check to consider -1 or the char * as a char*[] will have the same consequence no ? I just wanted to separate no word and no memory without adding an out parameter
ok, to not take the risk be killed by other guys I edit to return NULL also if no memory, that's all !
@alk except is the C norm explicitly says an address is never equals (whatever the conversion signed/unsigned) to -1 it is wrong to use -1 as an invalid address
No matter finally, we're probably talking to the walls and this answer is once again wasted, the OP did not react
 
@bruno We're celebrating my birthday today, and after passing -multiple- hours on that single piece of code yesterday while trying to learn C to answer part of an assignment, I must say I did not feel like reacting in some non-thoughtful way. I will spend my Sunday analyzing the answers to this thread and see how it goes. Sorry for the delay! I've been somewhat infuriated at how non-trivial and hard this function has proven to be while it is a simple .split in Java. =)
 
@payne sorry for my bad mood, it's not personal but a certain weariness. Happy birthday. I hope my answer will be useful
 
@bruno Why did you need to allocated memory with strdup() at the beginning instead of simply using strcpy() ? It forced you to free() before the end of the function. I'm also wondering why you have used char ** v = malloc((nw + 1) * sizeof(char *)); instead of doing like some other user that suggested char **result = malloc((strlen(str)+1) * sizeof(char *));. Counting the number of words is to be able to reduce the amount of allocated memory? Or maybe to use a for loop instead of a while ?
@bruno also, more precisely, about the char ** v = malloc((nw + 1) * sizeof(char *)); line: how does it work exactly? My understanding is that you are declaring a pointer to a pointer of char, and that the amount of memory allocated is somehow to be magically deducted from sizeof(char *) which is basically the amount of bits taken in memory for a pointer to a char? I'm having trouble understanding where the bridge is made between allocating those pointers and being able to use arrays directly (such as v[0]). Wouldn't v be a pointer to pointers of char[] rather than char ?
 
@payne I work on a copy to not modify the original, strtok modify its first parameter, and I use it 2 times. The malloc((strlen(str)+1) * sizeof(char *)); is a poor way and allocates too much memory, the lazy way was to do malloc((strlen(str)+1) / 2));supposing the worst case where each word has only one leter, but again this is a poor way. I don't like poor programming ;-) On a 32 bits machine a pointer uses 32bits.
 
8:46 PM
@bruno Would you mind telling me what is wrong exactly with this newer version of my code ? Somehow the code ends up properly allocating result[0] but the following while loop does not do anything, and the returned result variable seems empty because trying to validate with strcmp(returned_var[0], "first_word") == 0 doesn't pass the test. And why did you need to call strdup on your assignation lines using strtok ?
 
@payne what do you not understand in the sentence "I work on a copy to not modify the original, strtok modify its first parameter, and I use it 2 times" ?
 
@bruno I believe I do understand it properly
I understand most of what you've done
 
hello
 
I'm trying not to copy-paste and to replicate a simpler version though, to ensure I understand properly
 
strtok introduces null char when it find a separator
 
8:48 PM
indeed
 
and 1) I don't want to modify the input string because it is not mine (coming from user)
 
my question is about the necessity of strdup after the strtok
 
and 2) I have to call 2 times strtok so I need to not mofigy the original to strcpy it before the second time to remove the null chars inroduced
 
result[0] = strtok (copied_input, delim); // obtaining first word
is there something wrong with that line?
 
this is wrong because strtok doesn't return a copy of the substring but an offert of the original string
so strtok will resukt several offset from the original string
if I save each after the caller cannot free them
because only the original strdup can be freed
 
8:51 PM
(English isn't my first language)
what do you mean by "offert" ?
 
but may be even the first result of strtok is not the address of the original string if it starts by a eparator
sorry "offset"
 
but I thought strtok worked on the actual string itself (hence why we need to take care of copying it since it modifies that actual string)
so strtok modifies the input string, but returns copies of the substrings (based on delimiter) ?
 
if the original string O is "a aze ab" strtok will return O then O+2 then O+7
the user cannot free these address, so it is needed to strdup each return string to allow later the user to free them
strtok both modify the original string and return offet in it
it doesn't return copy
this is why I need to do the copies
 
generally speaking, if I have a pointer-to-pointers-of-char, freeing the pointer will not automatically free the pointers it contained? (hence your while(i--) ?)
 
if the original string is O="a aze ab" strtok will return O then O+2 then O+7
free have to get the result of malloc or equivalent, not an othet inside
*an offset inside
 
8:56 PM
it returns pointers (aka offsets in our case)
 
yes
you understand now ?
 
hence why within my function, calling printf("%s", return[3]); at the end would still display properly the stored word
but then, exiting the function and looking with the debugger how the char** variable outputted by the function was affected, it didn't contain words, but rather weird things
 
because you look at freed memory
 
@bruno I'll take another glance at your code to ensure I do
@bruno because it was on the stack rather than the heap?
I didn't expect that. I thought since I wasn't freeing the memory, the things would remain there. Didn't think about heap/stack stuff.
 
my version work, look at it, all the access are correct in it, and there is no memory leaks, I used valgrind to check all
 
8:59 PM
ya, I've tried your version, it is perfect indeed
I was just trying to understand why mine didn't. :D
I initially had been trying to output char[]* from the function since my Java background told me "oh yeah, you want to output an array of pointers to strings"
 
because you save address of memory blocks whose life is too short
from heap or from stack
 
but then a user commented about char** and I realized that is equivalent (though my char[]* doesn'T actually exist, as far as I know)
may I ask you another question?
I also have trouble understanding the difference between char *bar[] = malloc(50); and char *bar[50];
 
the one is on the heap, the second on the stack if a local var
 
"if a local var", what if not ?
 
please look at a book about C, this is the bases, and I have to go ;)
 
9:02 PM
thank you very much for your time, buddy! :D
 
if not is in the heap of in global memory, how can I guess where you placed bar ? can be in a struct itself somewhere etc
you know there is no magic, all is simple in fact, and logic
look at article about the heap (malloc etc)
you will understand, I did so you will lol
have a good night or days, depending where yoiu are ;)
 
9:15 PM
ty sir
 

last day (15 days later) »