动态mem alloc q [英] dynamic mem alloc q

查看:54
本文介绍了动态mem alloc q的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#include< errno.h>

#define BUF_MAX 80

int main(void){

char buf [ BUF_MAX];

unsigned long l;

size_t s;

printf(给我一个不到三十亿的数字:\ n");

while(fgets(buf,BUF_MAX,stdin)!= NULL){

if((s = strspn(buf," 0123456789 \ t) \ n"))== strlen(buf)){

errno = 0;

l = strtoul(buf,NULL,10);

if(errno == ERANGE){

printf(数字超出范围!\ n);

}否则{


/ *在这里添加代码* /


}

}否则{

而(s-- )

putchar('''');

putchar(''^'');

printf(" invalid character\ n");

}

}


返回0;

}


/ * int * dynarray;

dynarray = malloc(l * sizeof(long)); * /

/ * end source * /


我试图用

数组动态分配一组无符号长整数的内存大小由键盘上的无符号长度确定。随着现在存在的程序

控制,似乎我会完成业务

我已经标记了

/ *添加代码这里* /

我是否需要重新设计程序控制以避免泄漏大小的德克萨斯州?b $ b? Joe

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BUF_MAX 80

int main (void) {
char buf[BUF_MAX];
unsigned long l;
size_t s;
printf("give me a number less than a third billion: \n");
while(fgets(buf, BUF_MAX, stdin) != NULL) {
if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
errno = 0;
l = strtoul(buf, NULL, 10);
if (errno == ERANGE) {
printf("number out of range!\n");
} else {

/* add code here */

}
} else {
while(s--)
putchar('' '');
putchar(''^'');
printf(" invalid character\n");
}
}

return 0;
}

/* int *dynarray;
dynarray = malloc(l * sizeof(long)); */
/* end source */

I seek to dynamically allocate memory of an array of unsigned longs with
array size determined from an unsigned long from the keyboard. With program
control as it exists now, it would seem that I would get the business done
where I have marked
/* add code here */
Do I need to redesign program control in order not to have a leak the size
of Texas? Joe

推荐答案

Joe Smith schrieb:
Joe Smith schrieb:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
#include< errno.h>

#define BUF_MAX 80

int main (void){
char buf [BUF_MAX];
unsigned long l;
size_t s;
printf(给我一个不到三十亿的数字:\ n ;);
while(fgets(buf,BUF_MAX,stdin)!= NULL){
if((s = strspn(buf," 0123456789 \t\ n"))== strlen (buf)){
errno = 0;
l = strtoul(buf,NULL,10);
if(errno == ERANGE){
printf(" number out)范围!\ n");
} else {


注意:l表示潜在错误的值为0

和ULONG_MAX;因为你想为0

分配存储,也不为ULONG_MAX数组元素分配存储空间(至少如果用十亿分之一你/ b $ b意味着1e9),你可以只测试> ; 0和< = 333333333UL(假设

你的意思是三分之一和三分之一)。

只有你有一个在那里失败,我会检查使用strspn()或

重复stroul()调用,前面是errno = 0.

这更简单,更完整。

/ *在这里添加代码* /

}
}其他{
而(s--)
putchar('''' );
putchar(''^'');
printf(无效字符\ n);
}
}
返回0;
}
/ * int * dynarray;
dynarray = malloc(l * sizeof(long)); * /
/ *结束源* /

我寻求动态分配无符号长整数数组的内存,其数组大小由键盘上的无符号长整数确定。现在有了程序
控制权,似乎我会完成业务
我已经标记了
/ *在这里添加代码* /
我是否需要重新设计程序控制为了不泄漏德克萨斯州的大小?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BUF_MAX 80

int main (void) {
char buf[BUF_MAX];
unsigned long l;
size_t s;
printf("give me a number less than a third billion: \n");
while(fgets(buf, BUF_MAX, stdin) != NULL) {
if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
errno = 0;
l = strtoul(buf, NULL, 10);
if (errno == ERANGE) {
printf("number out of range!\n");
} else {
Note: The values of l indicating potential errors are 0
and ULONG_MAX; as you want to allocate storage neither for 0
nor for ULONG_MAX array elements (at least if by "billion" you
mean 1e9), you can just test for >0 and <= 333333333UL (assuming
you mean "a third of a" by "third") in the first place.
Only if you have a failure there, I''d check using strspn() or
repeat the strtoul() call preceded by errno = 0.
This is more straightforward and entirely sufficient.

/* add code here */

}
} else {
while(s--)
putchar('' '');
putchar(''^'');
printf(" invalid character\n");
}
}

return 0;
}

/* int *dynarray;
dynarray = malloc(l * sizeof(long)); */
/* end source */

I seek to dynamically allocate memory of an array of unsigned longs with
array size determined from an unsigned long from the keyboard. With program
control as it exists now, it would seem that I would get the business done
where I have marked
/* add code here */
Do I need to redesign program control in order not to have a leak the size
of Texas?




好​​吧,别忘了免费()分配存储:-)

除此之外,只有忘记检查l!= 0可能是一个

问题。


干杯
Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



Well, don''t forget to free() allocated storage :-)
Apart from that, only forgetting to check against l != 0 can be a
problem.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.




" Michael Mair"编写:

"Michael Mair" penned:
Joe Smith schrieb:
Joe Smith schrieb:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
#include< errno.h>

#define BUF_MAX 80
int main(void){
char buf [BUF_MAX ];
unsigned long l;
size_t s;
printf(给我一个不到三十亿的数字:\ n);
while(fgets(buf) ,BUF_MAX,stdin)!= NULL){
if((s = strspn(buf," 0123456789 \t\ n"))== strlen(buf)){
errno = 0 ;
l = strtoul(buf,NULL,10);
if(errno == ERANGE){
printf(数字超出范围!\ n);
} else {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BUF_MAX 80

int main (void) {
char buf[BUF_MAX];
unsigned long l;
size_t s;
printf("give me a number less than a third billion: \n");
while(fgets(buf, BUF_MAX, stdin) != NULL) {
if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
errno = 0;
l = strtoul(buf, NULL, 10);
if (errno == ERANGE) {
printf("number out of range!\n");
} else {



注意:l表示潜在错误的值为0
和ULONG_MAX;因为你想既不为0
也不为ULONG_MAX数组元素分配存储空间(至少如果用十亿你或者意味着1e9),你可以只测试> 0和< = 333333333UL(首先假设
你的意思是三分之一或三分之一。
只有当你在那里失败时,我才会检查使用strspn()或
重复以errno = 0开头的strtoul()调用。
这更直接且完全足够。



Note: The values of l indicating potential errors are 0
and ULONG_MAX; as you want to allocate storage neither for 0
nor for ULONG_MAX array elements (at least if by "billion" you
mean 1e9), you can just test for >0 and <= 333333333UL (assuming
you mean "a third of a" by "third") in the first place.
Only if you have a failure there, I''d check using strspn() or
repeat the strtoul() call preceded by errno = 0.
This is more straightforward and entirely sufficient.




''m''和'b ''都是英文的唇。用德语''onnen''和''arden''

不要深入思考那些使用过这些词的很多人的想法,即关于较大的数字。我原以为

零输入会屈服于ERANGE标志。它看起来对我来说,但是,b $ b,你暗示我的狙击代码需要在这里进行调整。



''m'' and ''b'' are both labials in english. ''onnen'' and ''arden'' in german
don''t penetrate the stupor of thought that a lot of persons who have who use
these words, i.e. regarding largish numbers. I would have thought that a
zero input would have succumbed to the ERANGE flag. It seeems to me,
however, that you''re hinting my sniped code needs a tune-up here.


/ *在这里添加代码* /

}
} else {
while(s--)
putchar('''');
putchar(''^'');
printf(" invalid character \\\
");
}


返回0;
}
/ * int * dynarray;
dynarray = malloc(l * sizeof(long)); * /
/ *结束源* /

我寻求动态分配无符号长整数数组的内存,其数组大小由键盘上的无符号长整数确定。随着现在存在的程序控制,似乎我会在我已经标记
/ *添加代码的地方完成
业务* /
我是否需要重新设计程序控制为了不泄漏德克萨斯州的大小?

/* add code here */

}
} else {
while(s--)
putchar('' '');
putchar(''^'');
printf(" invalid character\n");
}
}

return 0;
}

/* int *dynarray;
dynarray = malloc(l * sizeof(long)); */
/* end source */

I seek to dynamically allocate memory of an array of unsigned longs with
array size determined from an unsigned long from the keyboard. With
program control as it exists now, it would seem that I would get the
business done where I have marked
/* add code here */
Do I need to redesign program control in order not to have a leak the
size of Texas?



好吧,别忘了免费()分配存储:-)
除此之外,只有忘记检查l!= 0可能是一个问题。



Well, don''t forget to free() allocated storage :-)
Apart from that, only forgetting to check against l != 0 can be a
problem.




我今晚会给它地狱。 Joe



I''ll give it hell tonight. Joe


Joe Smith写道:
Joe Smith wrote:
" Michael Mair"编写:
"Michael Mair" penned:
Joe Smith schrieb:
Joe Smith schrieb:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
#include< errno.h>

#define BUF_MAX 80
int main(void){
char buf [BUF_MAX ];
unsigned long l;
size_t s;
printf(给我一个不到三十亿的数字:\ n);
while(fgets(buf) ,BUF_MAX,stdin)!= NULL){
if((s = strspn(buf," 0123456789 \t\ n"))== strlen(buf)){
errno = 0 ;
l = strtoul(buf,NULL,10);
if(errno == ERANGE){
printf(数字超出范围!\ n);
} else {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BUF_MAX 80

int main (void) {
char buf[BUF_MAX];
unsigned long l;
size_t s;
printf("give me a number less than a third billion: \n");
while(fgets(buf, BUF_MAX, stdin) != NULL) {
if ((s = strspn(buf, "0123456789 \t\n")) == strlen(buf)) {
errno = 0;
l = strtoul(buf, NULL, 10);
if (errno == ERANGE) {
printf("number out of range!\n");
} else {



注意:l表示潜在错误的值为0
和ULONG_MAX;因为你想既不为0
也不为ULONG_MAX数组元素分配存储空间(至少如果用十亿你或者意味着1e9),你可以只测试> 0和< = 333333333UL(首先假设
你的意思是三分之一或三分之一。
只有当你在那里失败时,我才会检查使用strspn()或
重复以errno = 0开头的strtoul()调用。
这更直接且完全足够。



Note: The values of l indicating potential errors are 0
and ULONG_MAX; as you want to allocate storage neither for 0
nor for ULONG_MAX array elements (at least if by "billion" you
mean 1e9), you can just test for >0 and <= 333333333UL (assuming
you mean "a third of a" by "third") in the first place.
Only if you have a failure there, I''d check using strspn() or
repeat the strtoul() call preceded by errno = 0.
This is more straightforward and entirely sufficient.



''''和b都是英语中的labials。德语中的'onnen'和''arden'
没有穿透思想的昏迷,很多人使用这些词语,即关于大数字。我原本以为
零输入会屈服于ERANGE标志。它看起来对我来说,然而,你暗示我的狙击代码需要在这里进行调整。



''m'' and ''b'' are both labials in english. ''onnen'' and ''arden'' in german
don''t penetrate the stupor of thought that a lot of persons who have who use
these words, i.e. regarding largish numbers. I would have thought that a
zero input would have succumbed to the ERANGE flag. It seeems to me,
however, that you''re hinting my sniped code needs a tune-up here.




以下是我列出的相应警告我的代码当我最初发布它时:


"如果输入只包含空格或减号

转换后的值将为0.


减号部分当然不适用于您的版本。

字包含本来应该是包含。


我试图给你留下深刻的印象,即代码被写入

符合一个非常具体的需求,究竟有什么需求*你*正在尝试

以适应?


Robert Gamble



Below is the appropriate caveat I listed with the code when I
originally posted it:

"If the input contains only of whitespace or the minus sign the
converted value will be 0. "

The minus sign part does not, of course, apply to your version. The
word "contains" was supposed to be "consists".

I have tried to impress upon you the fact that the code was written to
meet a very specific need, what exactly are the needs *you* are trying
to accomidate?

Robert Gamble

这篇关于动态mem alloc q的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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