int* intptr ()
{
int i;
i=rand();
printf("%d_____",i);
return(&i);
}
int main()
{
int* j,k,l;
j=intptr();
k=intptr();
l=intptr();
printf("%d/n",j);printf("%d/n",k);printf("%d/n",l);
}
Here the intptr
function returns a pointer in the first call and after that all the subsequent calls return the int 2752220
这里intptr函数在第一次调用中返回一个指针,之后所有后续调用都返回int 2752220
3 个解决方案
#1
5
The other answers address undefined behavior, which is the poor coding practice your code sample exhibits. However, as shown here, the real issue is that when you define
其他答案解决了未定义的行为,这是您的代码示例所展示的不良编码实践。但是,如此处所示,真正的问题是当您定义时
int* j,k,l;
only the first variable j
is an int pointer. The others are regular integers. In order to make them all pointers, you have to define them like this:
只有第一个变量j是一个int指针。其他是常规整数。为了使它们成为所有指针,你必须像这样定义它们:
int* j;
int* k;
int* l;
By running the code by chance each pointer may point to the same deleted value from your function, but at least they'll be pointers and not integers.
通过偶然运行代码,每个指针可能指向函数中相同的已删除值,但至少它们将是指针而不是整数。
#2
6
In your code, int i;
is local to the intptr ()
function. You cannot return the address of i
and use that in the caller. it invokes undefined behaviour.
在你的代码中,int i;是intptr()函数的本地。您不能返回i的地址并在调用者中使用它。它调用未定义的行为。
To elaborate, the lifetime of i
has ended when the intptr ()
function has returned. The address you're returned (or tried to return) has become invalid. Using the return value is thus, UB.
详细说明,当intptr()函数返回时,i的生命周期已经结束。您返回(或尝试返回)的地址已失效。因此,使用返回值UB。
After that, please note
之后,请注意
-
k
andl
are of typeint
, and you're trying to store anint *
. Wrong. The result is implementation-defined (refer §6.3.2.3,C11
) and most likely that is not something you wanted in your code. -
j
being a pointer, the correct way to print the pointer address is to usej是一个指针,打印指针地址的正确方法是使用
printf("%p/n",(void *)j)
using wrong type of argument (
%d
expectsint
, notint *
) is again UB.使用错误类型的参数(%d期望int,而不是int *)再次是UB。
k和l的类型为int,并且您正在尝试存储int *。错误。结果是实现定义的(参见§6.3.2.3,C11),并且很可能在您的代码中不是您想要的。
#3
2
You are trying to return something, which is local to the function intptr()
, hence whatever output you are getting even for the first call, it is not valid. This is undefined behavior. Because after the stack unwinding happens(control returning from inptr()
to main()
, the i
disappears and any value at the address of it could not be found.
您正在尝试返回函数intptr()的本地函数,因此即使是第一次调用,您获得的输出也是无效的。这是未定义的行为。因为在堆栈展开发生后(控制从inptr()返回到main(),idis出现并且无法找到它的地址处的任何值。
You can make it static to work.
你可以让它静止工作。
int* intptr ()
{
static int i = 0;
i=rand();
printf("%d_____",i);
return(&i);
}
#1
5
The other answers address undefined behavior, which is the poor coding practice your code sample exhibits. However, as shown here, the real issue is that when you define
其他答案解决了未定义的行为,这是您的代码示例所展示的不良编码实践。但是,如此处所示,真正的问题是当您定义时
int* j,k,l;
only the first variable j
is an int pointer. The others are regular integers. In order to make them all pointers, you have to define them like this:
只有第一个变量j是一个int指针。其他是常规整数。为了使它们成为所有指针,你必须像这样定义它们:
int* j;
int* k;
int* l;
By running the code by chance each pointer may point to the same deleted value from your function, but at least they'll be pointers and not integers.
通过偶然运行代码,每个指针可能指向函数中相同的已删除值,但至少它们将是指针而不是整数。
#2
6
In your code, int i;
is local to the intptr ()
function. You cannot return the address of i
and use that in the caller. it invokes undefined behaviour.
在你的代码中,int i;是intptr()函数的本地。您不能返回i的地址并在调用者中使用它。它调用未定义的行为。
To elaborate, the lifetime of i
has ended when the intptr ()
function has returned. The address you're returned (or tried to return) has become invalid. Using the return value is thus, UB.
详细说明,当intptr()函数返回时,i的生命周期已经结束。您返回(或尝试返回)的地址已失效。因此,使用返回值UB。
After that, please note
之后,请注意
-
k
andl
are of typeint
, and you're trying to store anint *
. Wrong. The result is implementation-defined (refer §6.3.2.3,C11
) and most likely that is not something you wanted in your code. -
j
being a pointer, the correct way to print the pointer address is to usej是一个指针,打印指针地址的正确方法是使用
printf("%p/n",(void *)j)
using wrong type of argument (
%d
expectsint
, notint *
) is again UB.使用错误类型的参数(%d期望int,而不是int *)再次是UB。
k和l的类型为int,并且您正在尝试存储int *。错误。结果是实现定义的(参见§6.3.2.3,C11),并且很可能在您的代码中不是您想要的。
#3
2
You are trying to return something, which is local to the function intptr()
, hence whatever output you are getting even for the first call, it is not valid. This is undefined behavior. Because after the stack unwinding happens(control returning from inptr()
to main()
, the i
disappears and any value at the address of it could not be found.
您正在尝试返回函数intptr()的本地函数,因此即使是第一次调用,您获得的输出也是无效的。这是未定义的行为。因为在堆栈展开发生后(控制从inptr()返回到main(),idis出现并且无法找到它的地址处的任何值。
You can make it static to work.
你可以让它静止工作。
int* intptr ()
{
static int i = 0;
i=rand();
printf("%d_____",i);
return(&i);
}