简体   繁体   中英

Segmentation fault or Suspicious pointer-to-pointer conversion (area too small)

long keyIntValue;
uint8_t *value; 

sscanf(buffer, " %*[^\"\n]\"%9[^;\"\n]", keyStringValue);
keyIntValue = strtol(keyStringValue, NULL, 16);
*value = *(uint8_t*)keyIntValue;
printf("The value is 0x%x \n", *value);

I get segmentation fault for the above code with GCC compiler

*(long *)value = keyIntValue;
 printf("The value is 0x%x \n", *(long *)value);

The above code works with gcc and gets the correct output but I get Suspicious pointer-to-pointer conversion (area too small) with bitbake compiler

How to solve this?

*value = *(uint8_t*)keyIntValue;

取消引用未初始化的指针会导致未定义的行为,从而导致分段错误。

If you would like to treat a portion of keyIntValue as a uint8_t , you can do it by taking an address of keyIntValue , like this:

unsigned char *value;
value = (unsigned char*)(&keyIntValue);
printf("The value is 0x%x \n", *value);

Note: The reason I changed the type of value to a pointer to unsigned char is to avoid violating the strict aliasing rule. Pointers to character types are allowed to alias to anything, so changing the type fixes the aliasing problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM