简单的malloc()问题 [英] simple malloc() problem

查看:57
本文介绍了简单的malloc()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向C新手解释为什么这不起作用,因为我希望它可以工作吗? (期望清楚地概述在

主程序中的printf声明中)


操作系统:Linux 2.4.26

海湾合作委员会:2.95。 4

void modify_pointer(char *);


int main(int argc,char * argv []){

char * ptr_to_string;

modify_pointer(ptr_to_string);

printf(但为什么这不是你好?%s \ n,ptr_to_string };

}


void modify_pointer(char * the_ptr){

the_ptr =(char *)malloc(strlen(") ;你好));

strcpy(the_ptr," hello");

printf(" this say hello:%s \ n",the_ptr);

}


-

输出:


这样说你好:你好

但为什么这不是打招呼? p�


---

Can someone explain to a C newbie why this doesn''t work as I expect it
to work? (expectations clearly outlined in the printf statement in
main routine)

OS: Linux 2.4.26
GCC: 2.95.4

void modify_pointer(char *);

int main(int argc, char *argv[]) {
char *ptr_to_string;
modify_pointer(ptr_to_string);
printf("but why doesn''t this say hello? %s\n", ptr_to_string);
}

void modify_pointer(char *the_ptr) {
the_ptr = (char *) malloc(strlen("hello"));
strcpy(the_ptr, "hello");
printf("this says hello: %s\n", the_ptr);
}

--
output:

this says hello: hello
but why doesn''t this say hello? p�

---

推荐答案



" hermit_crab67" <他*********** @ yahoo.com>在留言新闻中写道:52 ************************** @ posting.google.c om ...

"hermit_crab67" <he***********@yahoo.com> wrote in message news:52**************************@posting.google.c om...
有人可以向C新手解释为什么这不起作用,因为我期望它工作? (在主程序的printf语句中清楚地概述了期望)


问题不在于malloc(除了这里的内存泄漏)。

ptr_to_string已按值传递,因此分配


the_ptr =(char *)malloc(strlen(" hello")); / *不要施放* /


是modify_pointer()的本地。请执行以下操作:


操作系统:Linux 2.4.26
GCC:2.95.4

void modify_pointer(char *);


void modify_pointer(char **);
int main(int argc,char * argv []){
char * ptr_to_string;
modify_pointer (ptr_to_string);


modify_pointer(& ptr_to_string);

printf(但为什么这不是你好吗?%s \ n,ptr_to_string) ;


> modify_pointer(char * the_ptr){
the_ptr =(char *)malloc(strlen(" hello)));
strcpy(the_ptr) ,你好;;
printf(这说你好:%s \ n",the_ptr);
}
Can someone explain to a C newbie why this doesn''t work as I expect it
to work? (expectations clearly outlined in the printf statement in
main routine)

The problem isn''t with the malloc (except the memory leakage here).
ptr_to_string has been passed by value, so the assigment

the_ptr = (char *) malloc(strlen("hello")); /* don''t cast */

is local to the modify_pointer(). Do as follows:

OS: Linux 2.4.26
GCC: 2.95.4

void modify_pointer(char *);
void modify_pointer(char **);
int main(int argc, char *argv[]) {
char *ptr_to_string;
modify_pointer(ptr_to_string);
modify_pointer(&ptr_to_string);
printf("but why doesn''t this say hello? %s\n", ptr_to_string);
}

void modify_pointer(char *the_ptr) {
the_ptr = (char *) malloc(strlen("hello"));
strcpy(the_ptr, "hello");
printf("this says hello: %s\n", the_ptr);
}




void modify_pointer(char ** the_ptr){

* the_ptr = malloc(sizeof(" hello)));

strcpy(* the_ptr," hello");

printf(" this say hello:%s \ n",* the_ptr);

}


这可能得到一些帮助:


在C中,唯一真正的参数传递方法称为按值调用。

以下示例演示了最后声明:


#include< stdlib.h>


void

raisin(float x){ / * process x here * /}


void

grape(float * xp){/ * process xp here * /}

int

main(void)< br $>
{

浮动f;

浮动* fp =& f;


/ *。 .. * /

raisin(f);

grape(fp);

返回EXIT_SUCCESS;

}


当参数声明(f和fp)与相应参数(x和xp)的声明

匹配时,参数已经被通过

值。并且,当一个参数按值传递时,所调用的所谓的

函数对参数的任何修改都不会反映在调用函数中。所以,

对xp的任何修改都不会反映在fp中,但是,对* xp

的任何修改都反映在f中。


-

Vijay Kumar R Zanvar

指针和数组 - http://www.geocities.com/vijoeyz/art...dex.html#seq21


在''comp.lang.c''中,他****** *****@yahoo.com (hermit_crab67)写道:
In ''comp.lang.c'', he***********@yahoo.com (hermit_crab67) wrote:
有人可以向C新手解释为什么这不起作用,因为我期待它
操作系统:Linux 2.4.26
GCC:2.95.4

void modify_pointer( char *);


开始很糟糕。如果要修改对象,则必须传递

的地址。请记住,在C中,只传递值。


void modify_pointer(char ** pp);


您的问题由常见问题:

http ://www.eskimo.com/~scs/C-faq/q4.8.html

int main(int argc,char * argv []){
char * ptr_to_string;


此指针未初始化。

modify_pointer(ptr_to_string);


将未初始化的值传递给函数会调用未定义的行为。

任何事情都可能发生。一个体面且配置良好的编译器应该警告你

你这个。


因为你使用的是gcc(注意:gcc不是GCC),我建议你把它添加到

你的命令行:


-W -Wall -O2


及以下内容另外如果你想遵守C标准:


-ansi -pedantic

printf(但为什么这不是打招呼?% s \ n",ptr_to_string);


请确保< stdio.h>已经包括了。 printf()需要一个原型。这是

强制性。

}

void modify_pointer(char * the_ptr){
the_ptr =(char *)malloc (strlen的(QUOT;你好"));


修改参数的值通常表示设计不好。


无需演员。请确保您已包含< stdlib.h>。 malloc()可以

失败。您必须针对NULL测试返回的值。


strlen()想要< string.h>。它返回字符串的长度,而不是它的大小为b
。你是一个人。


char * the_ptr = malloc(strlen(" hello))+ 1);


或,在这个涉及字符串文字的特殊情况下:


char * the_ptr = malloc(sizeof" hello");

strcpy(the_ptr," hello" );


但建议避免重复这种情况。

- 并非所有编译器都足够智能合并类似的字符串文字,因此
$ b $浪费空间。

- 它让维护成为一个地狱


我推荐一个初始化的const char数组:


char const s [] =" hello";

char * the_ptr = malloc(strlen(S)+ 1);


if(the_ptr != NULL)

{

strcpy(the_ptr,S);

< ...>

}

printf(" this say hello:%s \ n",the_ptr);
}
Can someone explain to a C newbie why this doesn''t work as I expect it
to work? (expectations clearly outlined in the printf statement in
main routine)

OS: Linux 2.4.26
GCC: 2.95.4

void modify_pointer(char *);
Starts badly. If you want to modify an object, you must pass the address of
it. Remember, in C, only the values are passed.

void modify_pointer(char **pp);

Your problem is covered by the FAQ:

http://www.eskimo.com/~scs/C-faq/q4.8.html
int main(int argc, char *argv[]) {
char *ptr_to_string;
This pointer is uninitialized.
modify_pointer(ptr_to_string);
Passing an uninitialized value to a function invokes an undefined behaviour.
Anything can happen. A decent and well-configured compiler should have warned
you about that.

Because you are using gcc (note: gcc is not GCC), I suggest you add this to
your command line:

-W -Wall -O2

and the following in addition if you want to comply to the C standard:

-ansi -pedantic
printf("but why doesn''t this say hello? %s\n", ptr_to_string);
Be sure that <stdio.h> is included. printf() needs a prototype. It''s
mandatory.
}

void modify_pointer(char *the_ptr) {
the_ptr = (char *) malloc(strlen("hello"));
Modifiying the value of a parameter is often the indication of a bad design.

No need for the cast. Be sure that you have included <stdlib.h>. malloc() can
fail. You must test the returned value against NULL.

strlen() wants <string.h>. It returns the length of the string, not it''s
size. You''re off by one.

char * the_ptr = malloc(strlen("hello") + 1);

or, in this special case involving a string literal:

char * the_ptr = malloc(sizeof "hello");
strcpy(the_ptr, "hello");
But it is recommended to avoid such repetitions.
- Not all compilers are smart enough to merge similar string literals, hence
a waste of space.
- It makes maintenance a hell

I recommend an initialized array of const char:

char const s[] = "hello";
char * the_ptr = malloc(strlen(S) + 1);

if (the_ptr != NULL)
{
strcpy(the_ptr, S);
<...>
}
printf("this says hello: %s\n", the_ptr);
}




Not要提一下''the_ptr''的值现在必须通过新的

char ** pp参数返回(当然检查pp不是NULL之后)。


if(pp!= NULL)

{

* pp = the_ptr;

}


-

-ed-在这里收到我的电子邮件: http://marreduspam.com/ad672570

C语言FAQ: http://www.eskimo.com/~scs/C-faq/top.html

C-reference: http://www.dinkumware。 com / manuals / reader.aspx?lib = c99

FAQ de fclc: http://www.isty-info.uvsq.fr/~rumeau/fclc/



Not to mention that the value of ''the_ptr'' must now be returned via the new
char ** pp parameter (after having checked that pp is not NULL, of course).

if (pp != NULL)
{
*pp = the_ptr;
}

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/


Emmanuel Delahaye写道:
Emmanuel Delahaye wrote:
修改参数的值通常表明设计不好。
Modifiying the value of a parameter is often the indication of a bad design.




我对我感兴趣你在这个陈述背后的想法。


案例



I''m interested in your thoughts behind this statement.

Case


这篇关于简单的malloc()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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