返回int指针? [英] Returning int pointer ?

查看:79
本文介绍了返回int指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码片段 -


int * createIncidenceMatrix(int numEdges,int numVertices){

int * p =(int *)calloc( (numEdges * numVertices),sizeof(int));

if(p == NULL){

printf(无法分配内存);

退出(0);

}

返回p;

}

void createIncidencePerView( int numEdges,int numVertices,int viewId){

int * pnew = createIncidenceMatrix(numEdges,numVertices);

}


这段代码编译得很好。我想知道这是否会像

那样运行。显然,我也试过运行代码。我的问题是 -


当createIncidenceMatrix()返回p这是本地变量时,是否会在返回呼叫时取消分配?


因此,当我在
createIncidencePerView()内部进行createIncidenceMatrix()调用时,调用createIncidenceMatrix()会尝试

为pnew指针分配解除分配的本地指针?


谢谢

Mahendra

解决方案



" Mahendra" < im ** @ cc.gatech.eduwrote in message

news:gd ********** @ news-int2.gatech.edu ...
< blockquote class =post_quotes>
>我有以下代码片段 -


int * createIncidenceMatrix(int numEdges,int numVertices){

int * p =(int *)calloc((numEdges * numVertices),sizeof(int));

if(p == NULL){

printf("无法分配内存);

退出(0);

}

返回p;

}


void createIncidencePerView(int numEdges,int numVertices,int viewId){

int * pnew = createIncidenceMatrix(numEdges,numVertices);

}


此代码编译良好。我想知道这是否会像

那样运行。显然,我也试过运行代码。我的问题是 -


当createIncidenceMatrix()返回p是本地变量时,是否会在返回呼叫时取消分配?



p是一个指针(指向您已分配的内存)。它占用的几个字节

/将在返回时释放,但不会在这些字节的副本(指针变量p的

值)之前取消分配从功能返回。


-

Bartc


Bartc写道:


>

" Mahendra" < im ** @ cc.gatech.eduwrote in message

news:gd ********** @ news-int2.gatech.edu ...
< blockquote class =post_quotes>
>我有以下代码片段 -

int * createIncidenceMatrix(int numEdges,int numVertices){
int * p =(int *) calloc((numEdges * numVertices),sizeof(int));
if(p == NULL){
printf(无法分配内存);
exit(0);
}
返回p;
}

void createIncidencePerView(int numEdges,int numVertices,int viewId){
int * pnew = createIncidenceMatrix(numEdges, numVertices);
}

这段代码编译得很好。我想知道这是否会像
那样运行。显然,我也试过运行代码。我的问题是 -

当createIncidenceMatrix()返回p这是局部变量时,是否会在返回呼叫时取消分配?



p是一个指针(指向您已分配的内存)。它需要的几个字节

up / will /将在返回时释放,但不会在这些字节的副本之前

(指针变量p的值)已经被取消分配从

函数返回。



它会复制字节 - 保存指针变量还是复制字节

持有指针变量的值?


如果按照你所描述的那样执行后者,这个内存在哪里?
分配 - 堆?这是否意味着返回 call创建堆内存副本?


谢谢

Mahendra


我得到了我正在寻找的回复来自Andrey。


谢谢所有

Mahendra


Mahendra写道:


Bartc写道:


>" Mahendra" < im ** @ cc.gatech.eduwrote in message
新闻:gd ********** @ news-int2.gatech.edu ...


>>我有以下代码片段 -

int * createIncidenceMatrix(int numEdges,int numVertices){
int * p =(int *)calloc( (numEdges * numVertices),sizeof(int));
if(p == NULL){
printf(无法分配内存);
exit(0);
}
返回p;
}

void createIncidencePerView(int numEdges,int numVertices,int viewId){
int * pnew = createIncidenceMatrix(numEdges,numVertices) ;
}
这个代码编译得很好。我想知道这是否会像
那样运行。显然,我也试过运行代码。我的问题是 -

当createIncidenceMatrix()返回p这是局部变量时,是否会在返回呼叫时取消分配?


p是一个指针(指向您已分配的内存)。它需要的几个字节在返回时将被释放/将被释放,而不是在那些字节的副本之前(指针变量p的值)已经从
返回函数。



它会复制字节 - 保存指针变量还是复制字节

持有指针变量的值?


如果你按照你所描述的那样做了后者,这个内存在哪里?
分配 - 堆?这是否意味着返回 call创建堆内存副本?


谢谢

Mahendra


I have following code snippet -

int *createIncidenceMatrix(int numEdges, int numVertices) {
int *p = (int *) calloc((numEdges*numVertices), sizeof(int));
if(p == NULL) {
printf("Could not allocate memory");
exit(0);
}
return p;
}
void createIncidencePerView(int numEdges, int numVertices, int viewId) {
int *pnew = createIncidenceMatrix(numEdges, numVertices);
}

This code compiles fine. I am trying to understand if this will run as
intended. Obviously, I tried running the code also. My question is -

When createIncidenceMatrix() return p which is local variable, will p
get deallocated at return call ?

So when i make createIncidenceMatrix() call inside
createIncidencePerView(), will the call createIncidenceMatrix() tries to
assign deallocated local pointer to the pnew pointer ?

Thanks
Mahendra

解决方案


"Mahendra" <im**@cc.gatech.eduwrote in message
news:gd**********@news-int2.gatech.edu...

>I have following code snippet -

int *createIncidenceMatrix(int numEdges, int numVertices) {
int *p = (int *) calloc((numEdges*numVertices), sizeof(int));
if(p == NULL) {
printf("Could not allocate memory");
exit(0);
}
return p;
}
void createIncidencePerView(int numEdges, int numVertices, int viewId) {
int *pnew = createIncidenceMatrix(numEdges, numVertices);
}

This code compiles fine. I am trying to understand if this will run as
intended. Obviously, I tried running the code also. My question is -

When createIncidenceMatrix() return p which is local variable, will p
get deallocated at return call ?

p is a pointer (to the memory you''ve allocated). The few bytes it takes up
/will/ be deallocated on return, but not before a copy of those bytes (the
value of the pointer variable p) has been made to return from the function.

--
Bartc


Bartc wrote:

>
"Mahendra" <im**@cc.gatech.eduwrote in message
news:gd**********@news-int2.gatech.edu...

>I have following code snippet -

int *createIncidenceMatrix(int numEdges, int numVertices) {
int *p = (int *) calloc((numEdges*numVertices), sizeof(int));
if(p == NULL) {
printf("Could not allocate memory");
exit(0);
}
return p;
}
void createIncidencePerView(int numEdges, int numVertices, int viewId) {
int *pnew = createIncidenceMatrix(numEdges, numVertices);
}

This code compiles fine. I am trying to understand if this will run as
intended. Obviously, I tried running the code also. My question is -

When createIncidenceMatrix() return p which is local variable, will p
get deallocated at return call ?


p is a pointer (to the memory you''ve allocated). The few bytes it takes
up /will/ be deallocated on return, but not before a copy of those bytes
(the value of the pointer variable p) has been made to return from the
function.

Will it copy the bytes - holding pointer variable or copy the bytes
holding the value of pointer variable ?

If it does the latter as described by you, where is this memory
allocated - heap ? Does this mean "return" call creates heap memory copy ?

Thanks
Mahendra


I got the response i was looking for from Andrey.

Thanks all
Mahendra

Mahendra wrote:

Bartc wrote:

>"Mahendra" <im**@cc.gatech.eduwrote in message
news:gd**********@news-int2.gatech.edu...

>>I have following code snippet -

int *createIncidenceMatrix(int numEdges, int numVertices) {
int *p = (int *) calloc((numEdges*numVertices), sizeof(int));
if(p == NULL) {
printf("Could not allocate memory");
exit(0);
}
return p;
}
void createIncidencePerView(int numEdges, int numVertices, int viewId) {
int *pnew = createIncidenceMatrix(numEdges, numVertices);
}

This code compiles fine. I am trying to understand if this will run as
intended. Obviously, I tried running the code also. My question is -

When createIncidenceMatrix() return p which is local variable, will p
get deallocated at return call ?

p is a pointer (to the memory you''ve allocated). The few bytes it takes
up /will/ be deallocated on return, but not before a copy of those bytes
(the value of the pointer variable p) has been made to return from the
function.

Will it copy the bytes - holding pointer variable or copy the bytes
holding the value of pointer variable ?

If it does the latter as described by you, where is this memory
allocated - heap ? Does this mean "return" call creates heap memory copy ?

Thanks
Mahendra


这篇关于返回int指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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