我的计划好吗? [英] Is My Program OK?

查看:60
本文介绍了我的计划好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!


我正在学习C而且我想出了这个程序来测试我的知识。


#include< stdio .h>

#include< stdlib.h>

int main(无效)

{

char s [128];

void * p;

sprintf(s,"%p",malloc(0x10));

puts (s);

sscanf(s,%p,& p);

printf("%p \ n",p);

免费(p);

返回0;

}


可以吗?即使malloc返回NULL?


我不确定的一件事是s的大小应该是多少。 128

足够吗?还是太多了?我怎么知道它有多大?


提前谢谢 - 安迪!

解决方案

2月23日上午10点26分,Andy G. < andyg ... @ spamcop.netwrote:


问候!


我正在学习C而我想出了这个程序来测试我的知识。


#include< stdio.h>

#include< stdlib.h>

int main(void)

{

char s [128];

void * p;

sprintf (s,%p,malloc(0x10));

puts(s);

sscanf(s,"%p",& p) ;

printf("%p \ n",p);

免费(p);

返回0;


}


可以吗?即使malloc返回NULL?



是的,除了sprintf()部分。


我不确定的一件事是s的大小应该是多少。 128

足够吗?还是太多了?我怎么知道它有多大?



您无法知道它是否合适。

您应该使用snprintf()并检查返回值。

另外,将指针保存在某处,以防它不适合

缓冲区,以便你可以释放它。

你不知道它是否适合。

你确定吗?这应该可以解决这个问题:


int mod = -1;

int len =((mod = sizeof(void *)%sizeof(char)) == 0)? sizeof(void *)/

sizeof(char):sizeof(void *)/ sizeof(char)+1;

char s [len];




" Vitillo Roberto" < r。******* @ gmail.com写信息

新闻:1a ********************** ************ @ p73g2000 hsd.googlegroups.com ...


>你不能知道它是否适合。



你确定吗?这应该可以解决这个问题:


int mod = -1;

int len =((mod = sizeof(void *)%sizeof(char)) == 0)? sizeof(void *)/

sizeof(char):sizeof(void *)/ sizeof(char)+1;

char s [len];



我认为如果我的生活依赖于绝对正确的len,我宁愿

抓住len = 128的机会。


特别是因为你的代码似乎给len = 4 :-)


-

Bart


Greetings!

I am learning C and I came up with this program to test my knowledge.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char s[128];
void *p;
sprintf(s, "%p", malloc(0x10));
puts(s);
sscanf(s, "%p", &p);
printf("%p\n", p);
free(p);
return 0;
}

Is it OK? Even if malloc returns NULL?

One thing I am not sure of is what the size of s should be. Is 128
enough? Or too much? How should I know how big to make it?

Thanks in advance -- Andy!

解决方案

On Feb 23, 10:26 am, "Andy G." <andyg...@spamcop.netwrote:

Greetings!

I am learning C and I came up with this program to test my knowledge.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char s[128];
void *p;
sprintf(s, "%p", malloc(0x10));
puts(s);
sscanf(s, "%p", &p);
printf("%p\n", p);
free(p);
return 0;

}

Is it OK? Even if malloc returns NULL?

Yes, except the sprintf() part.

One thing I am not sure of is what the size of s should be. Is 128
enough? Or too much? How should I know how big to make it?

You cannot know whether it will fit or not.
You should instead use snprintf() and check the return value.
Also, save the pointer somewhere, in case it does not fit in the
buffer so you can free it.


You cannot know whether it will fit or not.
Are you sure? This should do the trick:

int mod = -1;
int len = ((mod = sizeof(void*) % sizeof(char)) == 0) ? sizeof(void*)/
sizeof(char) : sizeof(void*)/sizeof(char)+1;
char s[len];



"Vitillo Roberto" <r.*******@gmail.comwrote in message
news:1a**********************************@p73g2000 hsd.googlegroups.com...

>You cannot know whether it will fit or not.

Are you sure? This should do the trick:

int mod = -1;
int len = ((mod = sizeof(void*) % sizeof(char)) == 0) ? sizeof(void*)/
sizeof(char) : sizeof(void*)/sizeof(char)+1;
char s[len];

I think if my life depended on getting len absolutely right, I would rather
take my chances with len=128.

Especially as your code seems to give len=4 :-)

--
Bart


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

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