This is my code snippet (the header file is trivial so not appear here).
#include<string>
#include<list>
#include<optional>
#include<malloc.h>
#include "include/common.h"
using std::list;
using std::string;
using std::optional;
optional<list<string>> split(string content, const char *delimiter) {
char *buf = (char *)malloc(content.length());
memcpy(buf, content.c_str(), content.length());
const char *head = strtok(buf, delimiter);
const char *foll = buf; // follow ptr
list<string> l;
if(head == NULL){
return std::nullopt;
}
while(head != nullptr) {
DBG("%s", head);
// DBG("%s", foll);
// foll = head;
head = strtok(NULL, delimiter);
}
}
int main(){
string s("123 12312 123213a as dasd as asd");
split(s, " ");
}
and compile it with clang -o String String.cpp -g -std=c 17 -I../ -lstdc
But it will crash when I execute it.
squ in