[英]Memory allocation and leaking issue?
I am working on the print_selected(int x)
function which, takes x
then extracts bits 5, 6, 7, and 8 (starting from bit 0 at the rightmost) then print them in both binaries then in hexadecimal. 我正在使用print_selected(int x)
函数,该函数使用x
然后提取位5、6、7和8(从最右边的位0开始),然后在两个二进制文件中以十六进制形式打印它们。
There is a helper function, int2bin(int n)
within the print_selected(int x)
and int2bin(int)
function returns a char type array to s
. 有一个辅助函数, int2bin(int n)
中的print_selected(int x)
和int2bin(int)
函数将char类型的数组返回给s
。
The problem is that I can print out the binary number of bits of 5, 6, 7, and 8bits correctly, however, the hexadecimal number returns somehow weird number(due to the memory leaking issue?). 问题是我可以正确打印出5、6、7和8位的二进制数,但是十六进制数会以某种方式返回怪异的数字(由于内存泄漏问题?)。 I strongly doubt that char* s = int2bin(x)
and free(s)
within print_selected(int)
might be the problem but I do not know which part I should make a change to correctly print out the right hexadecimal number. 我强烈怀疑print_selected(int)
中的char* s = int2bin(x)
和free(s)
可能是问题,但我不知道应该更改哪一部分以正确打印正确的十六进制数。
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
// takes integer and returns char array with 32 binary number.
char* int2bin(int n)
{
int nbits = sizeof(n) * 8;
char *s = malloc(nbits + 1);
s[nbits] = '\0';
unsigned int u = *(unsigned int*)&n;
int i;
unsigned int mask = 1 << (nbits - 1);
for (i = 0; i < nbits; i++, mask >>= 1)
s[i] = ((u & mask) != 0) + '0';
return s;
}
// takes an integer and print in "binary" and "hexadecimal".
void print_selected(int x)
{
int hex[4]; // to store the 4 bits of (5,6,7,8)
int i;
char *s = int2bin(x); // I think this part makes problem?
printf("bits 5 to 8 in bin: ");
for (i = 23; i <= 26; i++)
{
printf("%d", s[i] - '0');
hex[i] = s[i] - '0';
}
free(s); // I think this part makes problem?
printf("\n");
int j = 3; // since we already know the number of bits,
int sum = 0;
for (i = 0; i <= 3; i++)
{
sum = sum + (hex[i] * pow(2, j));
j--;
}
printf("in hex: 0x%x\n", sum);
}
int main(void)
{
int a = 278;
print_selected(a);
}
The problem is here: 问题在这里:
for (i = 23; i <= 26; i++)
{
printf("%d", s[i] - '0');
hex[i] = s[i] - '0';
}
hex
indexes go from 0
to 3
, so assigning to hex[23]
causes undefined behavior. hex
索引从0
到3
,因此分配给hex[23]
会导致未定义的行为。 Then you try to print the elements of hex
, which you never actually filled in. 然后,您尝试打印hex
的元素,而您从未真正填写过。
You need: 你需要:
hex[i - 23] = s[i] - '0';
however, the hexadecimal number returns somehow weird number(due to the memory leaking issue?). 但是,十六进制数会以某种方式返回怪异的数字(由于内存泄漏问题?)。
no, you access to non initialized memory in 不,您访问的是未初始化的内存
for (i = 0; i <= 3; i++) { sum = sum + (hex[i] * pow(2, j)); j--; }
because hex is only initialized in 因为十六进制仅在
for (i = 23; i <= 26; i++) { printf("%d", s[i] - '0'); hex[i] = s[i] - '0'; }
so for the indexes 23 to 26, not for the indexes 0 to 3 因此对于索引23到26,而不是索引0到3
note you also write out of hex , with an undefined bahevior, you need to use the index i -23
to write hex 请注意,您还用未定义的行为写出了hex ,还需要使用索引i -23
来编写hex
so the value of sum is undefined 所以总和的值是不确定的
I encourage you to use valgrind to detect your memory problems, I used it to answer you 我鼓励您使用valgrind来检测您的内存问题,我用它来回答您
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.