本文介绍了返回int指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下代码片段 - 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 解决方案 p是一个指针(指向您已分配的内存)。它占用的几个字节 /将在返回时释放,但不会在这些字节的副本(指针变量p的 值)之前取消分配从功能返回。 - Bartc p是一个指针(指向您已分配的内存)。它需要的几个字节 up / will /将在返回时释放,但不会在这些字节的副本之前 (指针变量p的值)已经被取消分配从 函数返回。 它会复制字节 - 保存指针变量还是复制字节 持有指针变量的值? 如果按照你所描述的那样执行后者,这个内存在哪里?分配 - 堆?这是否意味着返回 call创建堆内存副本? 谢谢 Mahendra 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 asintended. Obviously, I tried running the code also. My question is -When createIncidenceMatrix() return p which is local variable, will pget deallocated at return call ?So when i make createIncidenceMatrix() call insidecreateIncidencePerView(), will the call createIncidenceMatrix() tries toassign deallocated local pointer to the pnew pointer ?ThanksMahendra 解决方案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 (thevalue of the pointer variable p) has been made to return from the function.--Bartcp is a pointer (to the memory you''ve allocated). The few bytes it takesup /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 thefunction.Will it copy the bytes - holding pointer variable or copy the bytesholding the value of pointer variable ?If it does the latter as described by you, where is this memoryallocated - heap ? Does this mean "return" call creates heap memory copy ?ThanksMahendrap is a pointer (to the memory you''ve allocated). The few bytes it takesup /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 thefunction.Will it copy the bytes - holding pointer variable or copy the bytesholding the value of pointer variable ?If it does the latter as described by you, where is this memoryallocated - heap ? Does this mean "return" call creates heap memory copy ?ThanksMahendra 这篇关于返回int指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 19:46