程序是否适用于所有情况? [英] does a program work in all cases?

查看:67
本文介绍了程序是否适用于所有情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这个程序在使用gdb测试时有效(见结果1)但是在更大的程序中使用

,其中12变为1500时存在

释放内存时的问题(参见结果2)。谁能在这里给出任何建议?

----------------------------------- ---------------

#include< stdlib.h>

#include< stdio.h>


int main(无效){

int i;

char ** arr;


arr = malloc(sizeof(char *)* 12);

for(i = 0; i< 12; i ++){

arr [i] = malloc (sizeof(char)* 5);

}

if(!(arr)){

printf("无法分配内存) \ n");

返回0;

}

for(i = 0; i< 12; i ++){

strcpy(arr [i]," Thi");

printf(" Arr [%d] \ t%s \ n",i,arr [ i]);

}

for(i = 0; i< 12; i ++){

free(arr [i]) ;

}

免费(arr);

返回1;

}

---------------------------------------------


结果1:


%gdb a.out

GNU gdb 6.0-2mdk(Mandrake Linux)

版权所有2003自由软件基金会,公司

GDB是免费软件,由GNU通用公共许可证涵盖,并且

你是

欢迎更改它和/或在某些

条件下分发它的副本。

键入show copying查看条件。

GDB绝对没有保修。输入show warranty for

详情。

此GDB配置为i586-mandrake-linux-gnu...使用主机

libthread_db库" ; /lib/tls/libthread_db.so.1"。

(gdb)运行

开始程序:a.out

Arr [0] Thi

Arr [1] Thi

Arr [2] Thi

Arr [3] Thi

Arr [4] Thi

Arr [5] Thi

Arr [6] Thi

Arr [7] Thi
Arr [8] Thi

Arr [9] Thi

Arr [10] Thi

Arr [11] Thi


程序已退出,代码为01.

(gdb)


结果2:

free():无效指针0x40426814!

free():无效指针0x40442c74!

free():无效指针0x40426874!

free():无效指针0x40442c54!

free():无效指针0x404268d4!

free():无效指针0x40442cb4!


这个问题使我无法理解。任何帮助表示赞赏。


/ S

Hi,

This program works when tested with gdb ( see results 1) but when used
in a larger program where 12 becomes 1500 there exists a problem when
freeing the memory ( see results 2). Can anyone give any advise here?
--------------------------------------------------
#include <stdlib.h>
#include <stdio.h>

int main(void) {
int i;
char** arr;

arr = malloc(sizeof(char *)*12);
for (i = 0; i < 12; i++) {
arr[i] = malloc(sizeof(char)*5);
}
if (!(arr)) {
printf("Failed to allocate memory\n");
return 0;
}
for (i = 0; i < 12; i++) {
strcpy(arr[i],"Thi");
printf("Arr[%d]\t%s\n",i, arr[i]);
}
for (i = 0; i < 12; i++) {
free(arr[i]);
}
free(arr);
return 1;
}
---------------------------------------------

Result 1:

% gdb a.out
GNU gdb 6.0-2mdk (Mandrake Linux)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i586-mandrake-linux-gnu"...Using host
libthread_db library "/lib/tls/libthread_db.so.1".

(gdb) run
Starting program: a.out
Arr[0] Thi
Arr[1] Thi
Arr[2] Thi
Arr[3] Thi
Arr[4] Thi
Arr[5] Thi
Arr[6] Thi
Arr[7] Thi
Arr[8] Thi
Arr[9] Thi
Arr[10] Thi
Arr[11] Thi

Program exited with code 01.
(gdb)

Results 2:

free(): invalid pointer 0x40426814!
free(): invalid pointer 0x40442c74!
free(): invalid pointer 0x40426874!
free(): invalid pointer 0x40442c54!
free(): invalid pointer 0x404268d4!
free(): invalid pointer 0x40442cb4!

This problem eludes me. Any help is appreciated.

/S

推荐答案

" Sheldon" < sh ****** @ gmail.comwrites:
"Sheldon" <sh******@gmail.comwrites:

int i;

char ** arr;
int i;
char** arr;


arr = malloc(sizeof(char *)* 12);

for(i = 0; i< 12; i ++){

arr [i] = malloc(sizeof(char)* 5);

}

if(!(arr) )){

printf(无法分配内存\ n);

返回0;

}
arr = malloc(sizeof(char *)*12);
for (i = 0; i < 12; i++) {
arr[i] = malloc(sizeof(char)*5);
}
if (!(arr)) {
printf("Failed to allocate memory\n");
return 0;
}



可能不是你的问题的原因,

但是你应该立即检查''arr'的分配失败

尝试分配,当然在你访问arr [i]之前。


-

克里斯。


Probably not the cause of your problem,
but you should check for allocation failure of ''arr'' immediately after you
attempt the allocation, and certainly before you access arr[i].

--
Chris.

2006-12-17,Sheldon< sh ****** @ gmail.comwrote:
On 2006-12-17, Sheldon <sh******@gmail.comwrote:




这个程序在使用gdb测试时有效(见结果1)但是当在12个变为1500的大型程序中使用

时,存在一个问题

释放记忆(见结果2)。谁能在这里给出任何建议?
Hi,

This program works when tested with gdb ( see results 1) but when used
in a larger program where 12 becomes 1500 there exists a problem when
freeing the memory ( see results 2). Can anyone give any advise here?



您确定已将所有12更改为1500吗?


我希望该程序工作正常,虽然我已经在下面做了一两个

的评论你没有要求:)

Are you sure you changed all the "12"s to "1500"s?

I would expect the program to work OK, although I''ve made one or two
comments below you didn''t ask for :)


--------------------------------------------------

#include< stdlib.h>

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



还需要< string.hfor strcpy

Also need <string.hfor strcpy


>

int main(void){

int i;

char ** arr;


arr = malloc(sizeof(char *) )* 12);

for(i = 0; i< 12; i ++){

arr [i] = malloc(sizeof(char)* 5);
>
int main(void) {
int i;
char** arr;

arr = malloc(sizeof(char *)*12);
for (i = 0; i < 12; i++) {
arr[i] = malloc(sizeof(char)*5);



你没有检查这些分配......

You don''t check these allocations...


}

if(!(arr)){

printf(无法分配内存\ n);
}
if (!(arr)) {
printf("Failed to allocate memory\n");



....即使你确实检查了这个,但只有在使用它之后,这是

a有点迟了。


你也应该去if(arr!= NULL)因为!NULL不是

在所有平台上都必须如此。


你可以考虑将错误打印到stderr而不是stdout。

....even though you do check this one, but only after using it, which is
a bit late.

You also should really go if (arr != NULL) because !NULL is not
necessarily true on all platforms.

And you could consider printing the error to stderr rather than stdout.


return 0;
return 0;



通常会返回0表示成功,而不是失败。

Usually 0 is returned for success, not failure.


}

for(i = 0; i< 12; i ++){

strcpy(arr [i]," Thi");

printf(" Arr [ %d] \t%s \ n",i,arr [i]);

}

for(i = 0; i< 12; i ++ ){

免费(arr [i]);

}

免费(arr);

返回1 ;
}
for (i = 0; i < 12; i++) {
strcpy(arr[i],"Thi");
printf("Arr[%d]\t%s\n",i, arr[i]);
}
for (i = 0; i < 12; i++) {
free(arr[i]);
}
free(arr);
return 1;



通常会返回0表示成功,而不是失败。


为了便于携带,我想我已经在这里阅读了你应该只从主要回报0,

EXIT_SUCCESS或EXIT_FAILURE。

Usually 0 is returned for success, not failure.

And for portability, I think I''ve read here you should only return 0,
EXIT_SUCCESS or EXIT_FAILURE from main.




Chris McDonald skrev:

Chris McDonald skrev:

" Sheldon" < sh ****** @ gmail.comwrites:
"Sheldon" <sh******@gmail.comwrites:

int i;

char ** arr;
int i;
char** arr;


arr = malloc(sizeof(char *)* 12);

for(i = 0; i< 12; i ++){

arr [i] = malloc(sizeof(char)* 5);

}

if(!(arr) )){

printf(无法分配内存\ n);

返回0;

}
arr = malloc(sizeof(char *)*12);
for (i = 0; i < 12; i++) {
arr[i] = malloc(sizeof(char)*5);
}
if (!(arr)) {
printf("Failed to allocate memory\n");
return 0;
}




可能不是你问题的原因,

你应该在你之后立即检查''arr'的分配失败>
尝试分配,当然在你访问arr [i]之前。


-

克里斯。



Probably not the cause of your problem,
but you should check for allocation failure of ''arr'' immediately after you
attempt the allocation, and certainly before you access arr[i].

--
Chris.



我以为我是这样做的:

if(!(arr)){

printf(" ;无法分配内存\ n");

返回0;

}

这与
$不一样b $ b if(arr == NULL){

printf(无法分配内存\ n);

返回0;

}




/ S

I thought I did that with:
if (!(arr)) {
printf("Failed to allocate memory\n");
return 0;
}
Is this not the same as
if(arr==NULL) {
printf("Failed to allocate memory\n");
return 0;
}
?

/S


这篇关于程序是否适用于所有情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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