0

我有一个功能如下

int* foo() {
int temp;
return(&temp); //address of temp variable.

编写相同的函数有什么问题

int foo() {
    int temp;
    return(&temp); //address of temp variable.
    }

因为“&”运算符返回的内存位置的地址是一个整数。

4

7 回答 7

1

编译器不按数字逻辑。对于它来说,&temp是一个指针,只是temp一个整数,并且是不同的类型,即使两者都是下面的数字流。

只有强制转换才能让编译器相信你的逻辑。

就像是

return (int)&temp
于 2012-04-18T09:37:41.923 回答
1

int * 不是 int 并且 c 是一种强类型语言。您知道内存中的所有内容都可以读取为数字,但您不能将所有内容都解释为 int。

顺便说一句,返回本地(堆栈)变量的地址似乎没用。

于 2012-04-18T09:50:01.620 回答
0

第一个示例将返回一个指向 temp 的指针(这没有意义,因为 temp 超出了范围,因此被删除)

第二个例子将把 temp 的地址作为一个整数值放在函数的返回值中,这只有在你想出于某种目的打印地址值时才有用。

所以写第二个例子是错误的,因为你需要将 int 转换为 int* 才能像指针一样工作。

于 2012-04-18T09:36:00.180 回答
0

这是真的,但是编译器对表示地址的整数是严格的,它不会让您将它们隐含地视为整数。而是尝试return ((int)&temp);

于 2012-04-18T09:37:00.027 回答
0

Both functions as you give them are fundamentally wrong since you return the address of a local variable. Such an address is invalid as soon as you finish the execution of your function. Just don't do that, this is undefined behavior, anything can happen if you try to access that object.

于 2012-04-18T11:18:55.223 回答
0

int(integer type) and int*(address) are both integer values but can be of different size. Say if int occupies a byte in memory and if we returned a 32 bit address there would be a memory overflow.

于 2012-04-18T11:29:22.560 回答
0

Because a pointer value is not just an integer. Yes, you can return an integer representation of a pointer value, but that's not the same thing as returning a pointer, because the semantics of integers and pointers are different. You can't dereference an integer type, for example, and pointer arithmetic is not the same as integer arithmetic.

For example, take this program (compiled as C99):

#include <stdio.h>

int main(void)
{
  int       x      = 0;
  char    *cp      = 0;
  int     *ip      = 0;
  double (*ap)[10] = 0;

  printf("sizeof  x = %zu,  x = %d,  x + 1 = %d\n", sizeof x, x, x + 1);
  printf("sizeof cp = %zu, cp = %d, cp + 1 = %d\n", sizeof cp, (int) cp, (int) (cp + 1));
  printf("sizeof ip = %zu, ip = %d, ip + 1 = %d\n", sizeof ip, (int) ip, (int) (ip + 1));
  printf("sizeof ap = %zu, ap = %d, ap + 1 = %d\n", sizeof ap, (int) ap, (int) (ap + 1));

  return 0;
}

Now, if you think of pointers as just integer values, you'd expect to see the same results for all those print statements. However, here's the output on my system:

sizeof  x = 4,  x = 0,  x + 1 = 1
sizeof cp = 4, cp = 0, cp + 1 = 1
sizeof ip = 4, ip = 0, ip + 1 = 4
sizeof ap = 4, ap = 0, ap + 1 = 80

Even though all the pointers have the same size and representation as an integer (at least on my system), the results of adding 1 to each pointer value gives me a different result based on the pointer type. cp + 1 will give me the location of the next char value, while ip + 1 will give me the location of the next int value, and ap + 1 will give me the location of the next double [10] (10-element array of double) value.

Not to mention the logic of both your examples is flawed; when you return from the function, temp no longer exists and the pointer value you return is no longer valid.

EDIT

Just remembered something else; you'll get a diagnostic since you're attempting to convert a pointer value to an integer without an explicit cast, which is a constraint violation. Chapter and verse:

6.5.16.1 Simple assignment

Constraints

1 One of the following shall hold:96)

— the left operand has qualified or unqualified arithmetic type and the right has arithmetic type;

— the left operand has a qualified or unqualified version of a structure or union type compatible with the type of the right;

— both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

— one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

— the left operand is a pointer and the right is a null pointer constant; or

— the left operand has type _Bool and the right is a pointer.

...
6.8.6.4 The return statement
...
3 If a return statement with an expression is executed, the value of the expression is returned to the caller as the value of the function call expression. If the expression has a type different from the return type of the function in which it appears, the value is converted as if by assignment to an object having the return type of the function.139)
于 2012-04-18T11:42:46.520 回答