为什么malloc()会修改此输出的行为? [英] Why would malloc() modify the behavior of this output?

查看:52
本文介绍了为什么malloc()会修改此输出的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果符合以下条件:


#include< stdlib.h>


void * test_me(int a){

int * m;

m =& a;

}


main(void){

char * j;


j = test_me(1);


printf(" j的值)是:%x,* j);


}


我得到:

j的值是:1

然而,当我添加malloc()


#include< stdlib.h>


void * test_me(int a){

int * m;

m =& a;

}

main(无效){

char * p;

char * j;


p = malloc(10 );

j = test_me(1);


printf(" p的值是:%x,* p);

printf(" j的值是:%x,* j);


免费(p);

}


我得到以下内容:

p的值为:0

j的值为:ffffffc4


为什么p变为零?

If go like the following:

#include <stdlib.h>

void *test_me(int a) {
int *m;
m = &a;
}

main(void){
char *j;

j = test_me(1);

printf("The value of j is : %x", *j);

}

I get:
The value of j is : 1
However, when I add malloc()

#include <stdlib.h>

void *test_me(int a) {
int *m;
m = &a;
}

main(void){
char *p;
char *j;

p = malloc(10);
j = test_me(1);

printf("The value of p is : %x", *p);
printf("The value of j is : %x", *j);

free(p);
}

I get the following:
The value of p is : 0
The value of j is: ffffffc4

Why does p become zero?

推荐答案

grocery_stocker写道:
grocery_stocker wrote:
如果如下所示:

#include< stdlib.h>

void * test_me(int a){
int * m;
m =& a;


请注意,您没有从此功能返回任何内容,

虽然其签名表示将返回指向void的指针。

未定义的行为。所有投注都已关闭。

}

main(无效){
char * j;

j = test_me(1);


再次,由于test_me()无法返回任何内容,因此`j''的值为

不确定。
printf(& j的值是:%x,* j);

}
我得到:
j的值是:1

但是,当我添加malloc()时
#include< stdlib.h>

void * test_me(int a){
int * m;
m =& a;
}

main(无效){
char * p;
char * j;

p = malloc(10);
j = test_me(1);

printf(" p的值是:%x,* p);
printf(" j的值是:%x,* j);

免费(p);
}

我得到以下内容: br /> p的值是:0
j的值是:ffffffc4

为什么p变为零?
If go like the following:

#include <stdlib.h>

void *test_me(int a) {
int *m;
m = &a;
Please notice that you''re not returning anything from this function,
though its signature indicates a pointer to void will be returned.
Undefined behavior. All bets are off.
}

main(void){
char *j;

j = test_me(1);
Again, since test_me() fails to return anything, the value of `j'' is
indeterminate.
printf("The value of j is : %x", *j);

}

I get:
The value of j is : 1
However, when I add malloc()

#include <stdlib.h>

void *test_me(int a) {
int *m;
m = &a;
}

main(void){
char *p;
char *j;

p = malloc(10);
j = test_me(1);

printf("The value of p is : %x", *p);
printf("The value of j is : %x", *j);

free(p);
}

I get the following:
The value of p is : 0
The value of j is: ffffffc4

Why does p become zero?



未定义的行为意味着结果可能是*任何东西* - 甚至鼻子

恶魔可能随之而来。


HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子12/5)
http://www.cafepress.com/goldsays


Undefined behavior means the results could be *anything* -- even nasal
demons might ensue.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


grocery_stocker写道:
grocery_stocker wrote:
如果符合以下条件:

#include< stdlib.h>
void * test_me(int a){
int * m;
m =& a;
}

main(void){
char * j;

j = test_me(1);

printf(" j的值为:%x",* j);

}

我得到:
j的值是:1

然而,当我添加malloc()时

#包括< stdlib.h>

void * test_me(int a){
int * m;
m =& a;
}

main(void){
char * p;
char * j;

p = malloc(10);
j = test_me(1) ;

printf(" p的值是:%x,* p);
printf(" value) j是:%x,* j);

免费(p);
}

我得到以下内容:
价值p是:0
j的值是:ffffffc4

为什么p变为零?
If go like the following:

#include <stdlib.h>

void *test_me(int a) {
int *m;
m = &a;
}

main(void){
char *j;

j = test_me(1);

printf("The value of j is : %x", *j);

}

I get:
The value of j is : 1
However, when I add malloc()

#include <stdlib.h>

void *test_me(int a) {
int *m;
m = &a;
}

main(void){
char *p;
char *j;

p = malloc(10);
j = test_me(1);

printf("The value of p is : %x", *p);
printf("The value of j is : %x", *j);

free(p);
}

I get the following:
The value of p is : 0
The value of j is: ffffffc4

Why does p become zero?



我的猜测是在调用test_me之后堆栈已损坏,

因为test_me返回未指定的值,因此p可能由

printf修改。


My guess is that after test_me has been called the stack is corrupt,
since test_me returns an unspecified value, so p is probably modified by
printf.


换句话说,在函数test_me()中,我应该使用

''static''?

So in other words, when in the function test_me(), I should have used
''static''?


这篇关于为什么malloc()会修改此输出的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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