[英]gcc: Why does two different variables have the same value after call?
I'm wondering how I can fix this. 我想知道如何解决此问题。 I admit I'm not that great at C to begin with.
首先,我承认我在C方面并不出色。 I've commented the code below to show what is happening.
我评论了下面的代码以显示正在发生的事情。 If I make the function call, then print out the values immediately after each function call, then the results are correct.
如果我进行函数调用,然后在每次函数调用后立即打印出值,那么结果是正确的。 But, if I print out the values after doing all the function calls, it only prints out the last value for both variables!
但是,如果我在执行所有函数调用后打印出这些值,那么它只会打印出两个变量的最后一个值!
Why is it doing this and how to I fix it? 为什么要这样做以及如何解决?
NOTE: I'm doing this in Linux using gcc version 4.9.2 (Debian 4.9.2-10) 注意:我正在Linux中使用gcc版本4.9.2(Debian 4.9.2-10)
/* Get first/last of string */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void strtoarr(unsigned char *input, int len, unsigned char output[128]);
char* substr(unsigned char input[128], int start_pos, int num_chars);
int main(void)
{
unsigned char myArr[128];
unsigned char *myStr = (unsigned char*)"79356e39486a556e576d6c3279443267516834376137333131387246474b3137";
char *strFirst;
char *strLast;
/* For the project I'm working on, I'm going to need this as an array of characters anyway. */
strtoarr(myStr, 64, myArr);
printf("Original String: %s\n", myArr);
printf("\n");
/* If I print the string after each call, it works.
Prints out:
Try 1 First 32: 79356e39486a556e576d6c3279443267
Try 1 Last 32: 516834376137333131387246474b3137
*/
strFirst = substr(myArr, 0, 32);
printf("Try 1 First 32: %s\n", strFirst);
strLast = substr(myArr, 32, 32);
printf("Try 1 Last 32: %s\n", strLast);
printf("\n");
/* FIXME: Why does it print the exact same thing if I print out after both calls?
Prints out:
Try 2 First 32: 516834376137333131387246474b3137
Try 2 Last 32: 516834376137333131387246474b3137
*/
strFirst = substr(myArr, 0, 32);
strLast = substr(myArr, 32, 32);
printf("Try 2 First 32: %s\n", strFirst);
printf("Try 2 Last 32: %s\n", strLast);
return 0;
}
char* substr(unsigned char input[128], int start_pos, int num_chars) {
char *result = calloc(num_chars * 2 + 1, sizeof(char*));
int i;
for(i = start_pos; i < (num_chars + start_pos); i++) {
result[i - start_pos] = input[i];
}
result[(i - start_pos) + 1] = '\0';
free(result);
return result;
}
/* Convert string to array of characters */
void strtoarr(unsigned char *input, int len, unsigned char output[128]) {
int i;
for(i = 0; i < len + 1; i++) {
output[i] = input[i];
}
output[i + 1] = '\0';
}
Returning a pointer to freed memory (then reading from said memory) is undefined behaviour. 将指针返回到释放的内存(然后从该内存中读取)是未定义的行为。
Change your substr
to not free the memory: 更改您的
substr
以不释放内存:
char* substr(unsigned char input[128], int start_pos, int num_chars) {
char* result = calloc(num_chars + 1, sizeof(char));
strncpy(result, (char*)input + start_pos, num_chars);
return result;
}
Remember to free
this memory on the caller level. 请记住在调用者级别
free
此内存。
In your substr
function, you alloc some memory and copy data in it which is fine. 在您的
substr
函数中,您可以分配一些内存并在其中复制数据。 But then you free it and return a pointer to freed memory. 但是,然后您释放它并返回一个指向释放的内存的指针。 From that on, using that pointer is undefined behaviour!
从那以后,使用该指针是不确定的行为!
What happens under the hood for your implementation and in that context is that the same pointer value is returned by both calls to malloc, what explains that both pointers show same last value. 对于您的实现,在这种情况下,发生的情况是两次malloc调用都返回了相同的指针值,这说明了两个指针都显示相同的最后一个值。 But you could also could have got an error or any other display.
但是您也可能会出现错误或任何其他显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.