简体   繁体   English

gcc:为什么两个不同的变量在调用后具有相同的值?

[英]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.

相关问题 当两个.c文件具有相同符号但不同类型的全局变量时,引用如何工作? - How does referencing work when have two .c files with global variables of same symbols but different types? 通过两种不同的方式用相同的参数调用fopen后,返回值不同 - The return value is different after call fopen with same arguments by two different ways 为什么GCC根据常量的值生成不同的乘法运算? - Why does GCC generate different opcodes for multiplication based on a value of the constant? 两个具有相同名称和类型的变量,在两个不同的.c文件中,使用gcc编译 - Two variables with same name and type, in two different .c files, compile with gcc 为什么代码显示两个不同变量的相同值,temp既不是局部变量也不是全局变量,temp是什么? - Why the code is showing the same value of two different variables , temp is neither local nor global , What is temp? 为什么两个不同的枚举枚举常量具有相同的整数值? - Why can two different enum enumeration-constants have the same integer value? 为什么Borland在不同的C文件中编译同一个对象的多个定义,而GCC没有? - Why does Borland compile with multiple definitions of same object in different C files while GCC does not? 为什么gcc会长时间发出警告? - Why does gcc have a warning for long long? 为什么GCC为几乎相同的C代码生成如此完全不同的程序集? - Why does GCC generate such radically different assembly for nearly the same C code? 为什么变量在同一条语句中递增后返回前一个值? - Why variables returns previous value after incremented in same statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM