传递一个未初始化的指针 [英] passing an uninitialize pointer

查看:87
本文介绍了传递一个未初始化的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我只是有一个快速的,可能是愚蠢的问题....

是否可以执行以下操作:


int func(){

int *指针;


foo(指针);

}


int foo(int *指针){

指针=(int *)malloc(10 * sizeof(int));

}


我只是语法错了或这是非法的?


提前致谢!

Joe

解决方案

joe写道:

大家好!我只是有一个快速的,可能是愚蠢的问题......
是否有可能做到以下几点:

int func(){
int * pointer;

foo(指针);
由于C中的参数由* value *传递,因此变量指针'

此时仍未初始化。 int foo(int * pointer){
pointer =(int *)malloc(10 * sizeof(int));
更糟糕的是,以上所有行都会引入内存泄漏。我只是语法错误或者这是非法的?



语法很好。你甚至不会调用未定义的行为。

不幸的是,在内存泄漏之外,它不会做*任何事情*。


一个函数不能副作用参数(按值传递)但它可以

副作用参数*指向*。


在这种情况下类似:


void foo(int **指针){

*指针= malloc(10 * sizeof **指针);

}


int main(void){

int * pointer;

foo(& pointer);

/ *指针现在包含

malloc()在foo()中返回的值* /

...

}


HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀


他们指责你 - 是他们的计划。


" ; Artie Gold < AR ******* @ austin.rr.com>在消息中写道

news:2k ************ @ uni-berlin.de ...

joe写道:

大家好!我只是有一个快速的,可能是愚蠢的问题......
是否有可能做到以下几点:

int func(){
int * pointer;

foo(指针);


由于C中的参数是由* value *传递的,因此变量`pointer''
此时保持未初始化。

}

int foo(int * pointer){
pointer =(int *)malloc(10 * sizeof(int));


更糟糕的是,所有以上行确实引入了内存泄漏。

}

我只是语法错误或者这是非法的吗?


语法是精细。你甚至不会调用未定义的行为。




所以传递一个未初始化的指针(这必须意味着要按顺序访问它

来制作一个没有调用未定义的行为?


Alex


joe写道:

你好所有!我只是有一个快速的,可能是愚蠢的问题......
是否有可能做到以下几点:

int func(){
int * pointer;

foo(指针);
}
int foo(int * pointer){
pointer =(int *)malloc(10 * sizeof(int));
}

我只是语法错误或这是非法的?




这在FAQ中完全涵盖。事实上你从malloc投了回报

以及问这个问题表明你没有跟随新闻组而没有关注新闻组而没有查看常见问题解答。那个

是一个usenet罪。


#include< stdlib.h>

#include< stdio.h>


#define MAGICNUMBER 10

int * foo(int **指针)

{

*指针= malloc(MAGICNUMBER * sizeof **指针);

返回*指针;

}


int main(void)

{

int * pointer = 0;

printf("在调用foo之前,指针==%p \\ \\ n \\ n,(void *)指针);

if(!foo(& pointer)){

fprintf(stderr," malloc call failed\ n");

退出(EXIT_FAILURE);

}

printf("调用foo后,指针==%p \ n" ;,(void *)指针);

免费(指针);

返回0;

}


Hi all! I just have quick, possibly stupid question....
is it possible to do the following:

int func(){
int *pointer;

foo(pointer);
}

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int));
}

do I just have the syntax wrong or is this illegal?

Thanks in advance!
Joe

解决方案

joe wrote:

Hi all! I just have quick, possibly stupid question....
is it possible to do the following:

int func(){
int *pointer;

foo(pointer); Since arguments in C are passed by *value*, the variable `pointer''
remains uninitialized at this point. }

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int)); Even worse, all the above line does is introduce a memory leak. }

do I just have the syntax wrong or is this illegal?


The syntax is fine. You don''t even invoke undefined behavior.
Unfortunately, outside of the memory leak, it doesn''t do *anything*.

A function cannot side effect a parameter (pass-by-value) but it can
side effect what a parameter *points to*.

In this case something like:

void foo(int **pointer) {
*pointer = malloc(10 * sizeof **pointer);
}

int main(void) {
int *pointer;
foo(&pointer);
/* pointer now contains the value returned by
malloc() in foo() */
...
}

HTH,
--ag

--
Artie Gold -- Austin, Texas

"What they accuse you of -- is what they have planned."


"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:2k************@uni-berlin.de...

joe wrote:

Hi all! I just have quick, possibly stupid question....
is it possible to do the following:

int func(){
int *pointer;

foo(pointer);


Since arguments in C are passed by *value*, the variable `pointer''
remains uninitialized at this point.

}

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int));


Even worse, all the above line does is introduce a memory leak.

}

do I just have the syntax wrong or is this illegal?


The syntax is fine. You don''t even invoke undefined behavior.



So passing an uninitialised pointer (which must mean accessing it in order
to make a copy) doesn''t invoke undefined behaviour?

Alex


joe wrote:

Hi all! I just have quick, possibly stupid question....
is it possible to do the following:

int func(){
int *pointer;

foo(pointer);
}

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int));
}

do I just have the syntax wrong or is this illegal?



This is covered fully in the FAQ. The fact that you cast the return
from malloc as well as asking this question demonstrates that you have
neither followed the newsgroup before posting not checked the FAQ. That
is a usenet sin.

#include <stdlib.h>
#include <stdio.h>

#define MAGICNUMBER 10

int *foo(int **pointer)
{
*pointer = malloc(MAGICNUMBER * sizeof **pointer);
return *pointer;
}

int main(void)
{
int *pointer = 0;
printf("before calling foo, pointer == %p\n", (void *) pointer);
if (!foo(&pointer)) {
fprintf(stderr, "the malloc call failed\n");
exit(EXIT_FAILURE);
}
printf("after calling foo, pointer == %p\n", (void *) pointer);
free(pointer);
return 0;
}


这篇关于传递一个未初始化的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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