My search here got many results, but none quite like so that I could find a solution to my problem.
我在这里的搜索得到了很多结果,但没有一个像我能找到解决问题的方法。
I'm creating a Mathcad UserEFI DLL in C using Visual Studio 2013. I don't want to use strings, only char*.
我正在使用Visual Studio 2013在C中创建Mathcad UserEFI DLL。我不想使用字符串,只使用char *。
Now, I want to emulate the console main function, which uses char* argv[] to access the parameters of a called executable. Mathcad will call the DLL with a string like "-T=3 z h 13". All I want is to parse this text into an array of char*, just like argv[] would be if I called an executable with this added parameters. I hope I expressed this in an understandable way. I use strtok_s to parse the text and one has to consider, that every token can have a different size.
现在,我想模拟控制台main函数,它使用char * argv []来访问被调用的可执行文件的参数。 Mathcad将使用类似“-T = 3 z h 13”的字符串调用DLL。我想要的只是将这个文本解析成一个char *数组,就像argv []一样,如果我用这个添加的参数调用一个可执行文件。我希望我以一种可以理解的方式表达这一点。我使用strtok_s来解析文本,并且必须考虑每个令牌可以具有不同的大小。
The error must lie in the following function:
错误必须在以下函数中:
typedef struct tArgReturnType {
int ACount;
char** Argus;
} ARGRETURN;
ARGRETURN ParseStringToArgs(char* text) {
char *token = NULL;
char *nextToken = NULL;
int argCount = 0;
char* temptext = NULL;
strcpy(temptext, text);
char** uebergabe = (char**)malloc(sizeof(char**));
token = strtok_s(temptext, " ", &nextToken);
while (token != NULL) {
argCount++;
uebergabe = (char**)realloc(uebergabe, sizeof(uebergabe)+sizeof(token));
uebergabe[argCount - 1] = token;
token = strtok_s(NULL, " ", &nextToken);
}
ARGRETURN ReturnVar;
ReturnVar.ACount = argCount;
ReturnVar.Argus = (char**)malloc(sizeof(uebergabe));
memcpy(ReturnVar.Argus, uebergabe, sizeof(uebergabe));
free(uebergabe);
return ReturnVar;
}
I'm sure that this is a complete mishmash of heap memory allocation failures (as indicated by the error the mathcad compiler gives me), since I modified this code multiple times while trying to find a solution. I'm just utterly confused now.
我确信这是堆内存分配失败的完全混乱(由mathcad编译器给出的错误表示),因为我在尝试查找解决方案时多次修改此代码。我现在完全糊涂了。
Updated Code:
typedef struct tArgReturnType {
int ACount;
char** Argus;
} ARGRETURN;
ARGRETURN ParseStringToArgs(char* text) {
char *token = NULL;
char *nextToken = NULL;
int argCount = 0;
char* temptext = malloc(strlen(text) + 1);
strcpy(temptext, text);
char** uebergabe = malloc(sizeof(char**));
token = strtok_s(temptext, " ", &nextToken);
while (token != NULL) {
argCount++;
uebergabe = (char**)realloc(uebergabe, sizeof(uebergabe)+sizeof(token));
uebergabe[argCount - 1] = token;
token = strtok_s(NULL, " ", &nextToken);
}
ARGRETURN ReturnVar;
ReturnVar.ACount = argCount;
ReturnVar.Argus = malloc(sizeof(uebergabe));
memcpy(ReturnVar.Argus, uebergabe, sizeof(uebergabe));
free(uebergabe);
free(temptext);
return ReturnVar;
}
1 个解决方案
#1
ARGRETURN ParseStringToArgs(const char* text) {
ARGRETURN ReturnVar = { 0 };
int n = 0;
char temp[100];
while (sscanf(text += n, "%99s%n", temp, &n) == 1) {
ReturnVar.Argus = realloc(ReturnVar.Argus, ++ReturnVar.ACount*sizeof(*ReturnVar.Argus));
strcpy(ReturnVar.Argus[ReturnVar.ACount - 1] = malloc(strlen(temp) + 1), temp);
}
return ReturnVar;
}
- 'sizeof(text)' is a great mistake
- don't use ugly strtok(_s), it's not reentrant, destroys the string, ...
- use sscanf instead
'sizeof(text)'是一个很大的错误
不要使用丑陋的strtok(_s),它不是可重入的,破坏字符串,...
请改用sscanf
#1
ARGRETURN ParseStringToArgs(const char* text) {
ARGRETURN ReturnVar = { 0 };
int n = 0;
char temp[100];
while (sscanf(text += n, "%99s%n", temp, &n) == 1) {
ReturnVar.Argus = realloc(ReturnVar.Argus, ++ReturnVar.ACount*sizeof(*ReturnVar.Argus));
strcpy(ReturnVar.Argus[ReturnVar.ACount - 1] = malloc(strlen(temp) + 1), temp);
}
return ReturnVar;
}
- 'sizeof(text)' is a great mistake
- don't use ugly strtok(_s), it's not reentrant, destroys the string, ...
- use sscanf instead
'sizeof(text)'是一个很大的错误
不要使用丑陋的strtok(_s),它不是可重入的,破坏字符串,...
请改用sscanf