I am currently working on a program with the Dante *lyn II board from audinate. I am trying to use a void return function from the Dante API - foo() - that needs to have 3 various structs passed in as well as 2 strings. I have another function get_value_by_key() which returns the string value that I need. I initially tried calling on the program like so:
我目前正在与audinate的Dante *lyn II董事会合作开展一项计划。我试图使用Dante API中的void返回函数 - foo() - 需要传入3个不同的结构以及2个字符串。我有另一个函数get_value_by_key(),它返回我需要的字符串值。我最初尝试这样调用该程序:
foo(struct, struct, struct, get_value_by_key(key1), get_value_by_key(key2);
When I run it this way, the program twists the returned strings of get_value_by_key() and crashes the entire program. I have, however, found an alternative that works, but it is longer and I would like to not waste the extra processing power. The alternative is here:
当我以这种方式运行时,程序会扭曲返回的get_value_by_key()字符串并使整个程序崩溃。但是,我发现了一种可行的替代方案,但它更长,我不想浪费额外的处理能力。替代方案是:
char value1[15], value2[15];
strcpy(value1, get_value_by_key(key1));
strcpy(value2, get_value_by_key(key2));
foo(struct, struct, struct, value1, value2);
I cant seem to decipher why the *lyn board shuts down with the first command, and not the second. Any help at all would be much appreciated. This is the code for get_value_key(). Pretty simple logic here:
我似乎无法破译为什么布鲁克林董事会用第一个命令关闭,而不是第二个命令。任何帮助都将非常感激。这是get_value_key()的代码。这里很简单的逻辑:
char * get_value_by_key(command_t command, char key[50]){
int i;
char value[50];
for(i = 0; i < NUM_PARAMETERS; i++){
if(strcmp(user_command.command_parameter[i].key, key) == 0){
strcpy(value, user_command.command_parameter[i].value);
}
}
return value;
}
2 个解决方案
#1
2
Your get_value_by_key
function is broken -- it is returning a pointer to a local variable (value
) which is destroyed when the function returns. So you get undefined behavior when you try to read the string -- it might still be there but might have been overwritten by something else.
你的get_value_by_key函数被破坏 - 它返回一个指向局部变量(值)的指针,该函数在函数返回时被销毁。因此,当您尝试读取字符串时,您会得到未定义的行为 - 它可能仍然存在,但可能已被其他内容覆盖。
#2
1
This looks like a mistake:
这看起来像是一个错误:
char * value1, value2; /* value1 is a char*, value2 is a char */
strcpy(value1, get_value_by_key(key1));
strcpy(value2, get_value_by_key(key2));
You are copying data into value1
and value2
, but you have not allocated memory for the strings - just the pointer and the char. NOTE: only value1
is a char*
, and value2
is a single char
. If you want to declare both a char*
, you need to write:
您正在将数据复制到value1和value2,但您没有为字符串分配内存 - 只是指针和char。注意:只有value1是char *,value2是单个char。如果你想声明两个char *,你需要写:
char *value1, *value2;
The code below should work without crashing (assuming get_value_by_key() returns a char*
), but I don't know how you want to allocate the space. On the stack, the heap or in static RAM ?
下面的代码应该不会崩溃(假设get_value_by_key()返回一个char *),但我不知道你想如何分配空间。在堆栈上,堆还是在静态RAM中?
char value1[128], value2[128];
strcpy(value1, get_value_by_key(key1));
strcpy(value2, get_value_by_key(key2));
EDIT: (edited after new info in the comments)
编辑:(在评论中的新信息后编辑)
Please post the code for get_value_by_key
.
请发布get_value_by_key的代码。
I am betting it uses a static buffer to hold the string/char-array it returns a pointer to. That would mean the content gets destroyed after the second call and that could be a problem.
我打赌它使用一个静态缓冲区来保存字符串/ char-array,它返回一个指针。这意味着内容在第二次调用后被破坏,这可能是个问题。
EDIT2:
EDIT2:
If that is indeed the real source, then you are returning a pointer to something allocated on the stack - strictly verboten!!
如果那确实是真正的来源,那么你将返回一个指向堆栈上分配的东西的指针 - 严格的verboten !!
See returning a local variable from function in C
请参阅从C中的函数返回局部变量
#1
2
Your get_value_by_key
function is broken -- it is returning a pointer to a local variable (value
) which is destroyed when the function returns. So you get undefined behavior when you try to read the string -- it might still be there but might have been overwritten by something else.
你的get_value_by_key函数被破坏 - 它返回一个指向局部变量(值)的指针,该函数在函数返回时被销毁。因此,当您尝试读取字符串时,您会得到未定义的行为 - 它可能仍然存在,但可能已被其他内容覆盖。
#2
1
This looks like a mistake:
这看起来像是一个错误:
char * value1, value2; /* value1 is a char*, value2 is a char */
strcpy(value1, get_value_by_key(key1));
strcpy(value2, get_value_by_key(key2));
You are copying data into value1
and value2
, but you have not allocated memory for the strings - just the pointer and the char. NOTE: only value1
is a char*
, and value2
is a single char
. If you want to declare both a char*
, you need to write:
您正在将数据复制到value1和value2,但您没有为字符串分配内存 - 只是指针和char。注意:只有value1是char *,value2是单个char。如果你想声明两个char *,你需要写:
char *value1, *value2;
The code below should work without crashing (assuming get_value_by_key() returns a char*
), but I don't know how you want to allocate the space. On the stack, the heap or in static RAM ?
下面的代码应该不会崩溃(假设get_value_by_key()返回一个char *),但我不知道你想如何分配空间。在堆栈上,堆还是在静态RAM中?
char value1[128], value2[128];
strcpy(value1, get_value_by_key(key1));
strcpy(value2, get_value_by_key(key2));
EDIT: (edited after new info in the comments)
编辑:(在评论中的新信息后编辑)
Please post the code for get_value_by_key
.
请发布get_value_by_key的代码。
I am betting it uses a static buffer to hold the string/char-array it returns a pointer to. That would mean the content gets destroyed after the second call and that could be a problem.
我打赌它使用一个静态缓冲区来保存字符串/ char-array,它返回一个指针。这意味着内容在第二次调用后被破坏,这可能是个问题。
EDIT2:
EDIT2:
If that is indeed the real source, then you are returning a pointer to something allocated on the stack - strictly verboten!!
如果那确实是真正的来源,那么你将返回一个指向堆栈上分配的东西的指针 - 严格的verboten !!
See returning a local variable from function in C
请参阅从C中的函数返回局部变量