c - 这段 C 代码有漏洞吗?

标签 c malloc

这个C代码有漏洞吗?
虽然答案是正确的,但只是想知道这样写是否可以:

// _mm_adds_epi16 : Adds the 8 signed 16-bit integers in a to the 8 signed 
//16-bit integers in b and saturates
__m128i t7=_mm_adds_epi16(  t5 ,t6  );
unsigned short *p= (unsigned short *)malloc(8);
p=(unsigned short *)&t7;
for(int i=0;i<8;i++)
{
    printf("%d\n", p[i]);
}

已更新

所以现在我更新如下:

// _mm_adds_epi16 : Adds the 8 signed 16-bit integers in a to the 8 signed 
//16-bit integers in b and saturates
__m128i t7=_mm_adds_epi16(  t5 ,t6  );
unsigned short *p= (unsigned short *)malloc(8);
p=(unsigned short *)&t7;
for(int i=0;i<8;i++)
{
    printf("%d\n", p[i]);
}

 free(p);

我还有漏气吗?

打印t7的正确方法是什么

最佳答案

是的,有泄漏。您立即覆盖存储动态分配内存地址的指针。这样就无法再访问该内存。

关于c - 这段 C 代码有漏洞吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065921/

相关文章:

c - 查找 C 库中的函数列表

c - 通过管道发送变量时丢失数据的风险?

c - MagickCore 将图像数据写入标准输出而不是文件名

c - 在分配的字符串上使用 strtok()?

c - 函数中的 malloc — 段错误

c - 试图理解 XV6 上的 UNIX 系统调用

c - C语言接受单个字符的菜单

c - 高效内存重新分配问题

c - 我究竟做错了什么?如何将 read 函数中分配的内存传递给 disp 函数?

c - 如何在函数外传递二维指针,使指针指向的内存可以访问?