是否释放其被分配到一个char *(由`malloc`分配)一个int *未定义行为? [英] Does freeing an int* which was assigned to a char* (allocated by `malloc`) invoke Undefined Behavior?

查看:211
本文介绍了是否释放其被分配到一个char *(由`malloc`分配)一个int *未定义行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能混淆。假设 STR 是按分配的指针的malloc。 PTR ,类型为int * ,分配给它,如由下面的code片段被释放

 的char *海峡=的malloc(64);
为int * PTR = str中;免费(PTR);

我试图编译上面的code。它只是给出了一个警告:

  source_file.c:在函数'主':
source_file.c:10:16:警告:初始化从兼容的指针类型
     为int * PTR = str中;
                ^

请问上述code未定义行为?结果
难道上述code段释放由分配的内存的malloc STR


解决方案

  

请问上述code未定义行为?


这要看情况。

从C11草案6.3.2.3/7:


  

一个指针对象类型可被转换成一个指针到一个不同的对象类型。如果
  结果指针没有正确对齐)为引用类型,行为
  不确定的。


对于对齐字符可能是从 INT ,这可能是限制较少,分配不同的char * PC INT * PI 可能导致 PI 不对齐。

不过,对于在具体由OP给出的例子:

 的char * PC =的malloc(64);
INT * PI = PC;

行为将被定义为(见的 阿尔特曼 的的<一个href=\"http://stackoverflow.com/questions/30000240/does-freeing-a-pointer-which-was-assigned-to-another-pointer-of-another-type-al#comment48119661_30000348\">comment) 的malloc()保证返回的内存对齐块。

从C11草案7.22.3:


  


  指针返回[按aligned_alloc,释放calloc,的malloc和realloc]如果分配成功适当对齐,使得它可以被分配给
  的指针的任何类型的对象与基本对齐要求...


其中的导致不确定的行为,由于错位的一个例子是:

 的char * PC =的malloc(64);
INT * PI = PC + 1;



  

请问上述code段释放由malloc为STR分配的内存?


在情况下,前者的分配将出台未定义行为这个问题是无关紧要的,因为什么事情都可能与UB发生已经已经调用。

如果别人事先分配就不会调用UB,调用免费()将完全取消分配的内存中引用的块,作为指针转换回从为int * 无效* ,最初由提供的malloc(),是很好的界定。

从C11草案6.3.2.3/7(续/):


  

否则,当再次转换回,结果应比较等于
  原来的指针


从C11草案6.3.2.3/1:


  

要无效的指针可被转换成或从一个指针到任何对象类型。一个指向
  任何对象类型可被转换成一指向void,然后再返回;结果应
  比较等于原始指针


The title maybe confusing. Suppose str is a pointer allocated by malloc. ptr, of type int*, is assigned to it and is freed as shown by the code snippet below:

char* str = malloc(64);
int* ptr = str;

free(ptr);

I've tried to compile the above code. It just gives a warning:

source_file.c: In function ‘main’:
source_file.c:10:16: warning: initialization from incompatible pointer type
     int* ptr = str;
                ^

Does the above code invoke Undefined Behavior?
Does the above code snippet free the memory allocated by malloc for str?

解决方案

Does the above code invoke Undefined Behavior?

It depends.

From C11 draft 6.3.2.3/7:

A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned) for the referenced type, the behavior is undefined.

As the alignment for a char might be different from an int, that is probably less restrictive, assigning a char * pc to an int * pi might lead to pi being misaligned.

However for the specific example given by the OP:

char * pc = malloc(64);
int * pi = pc;

the behaviour would be defined, as (See Alter Mann's comment) malloc() is guaranteed to return a block of memory properly aligned.

From C11 draft 7.22.3:

The pointer returned [by aligned_alloc, calloc, malloc, and realloc] if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement ...

An example which would lead to undefined behaviour, due to misalignment, is:

char * pc = malloc(64);
int * pi = pc + 1;


Does the above code snippet free the memory allocated by malloc for str?

In case the former assignment would have introduced undefined behaviour this question is irrelevant, as anything could happen with UB having been invoked already.

If else the prior assignment wouldn't have invoked UB, the call to free() would perfectly de-allocate the block of memory referenced, as converting back the pointer value from int * to void *, as originally provided by malloc(), is well defined.

From C11 draft 6.3.2.3/7 (cont/):

Otherwise, when converted back again, the result shall compare equal to the original pointer

and

From C11 draft 6.3.2.3/1:

A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer

这篇关于是否释放其被分配到一个char *(由`malloc`分配)一个int *未定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆