- 在线时间
- 0 小时
- 最后登录
- 2006-4-1
- 注册时间
- 2004-6-7
- 听众数
- 0
- 收听数
- 0
- 能力
- 0 分
- 体力
- 61 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 19
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1
- 主题
- 0
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   14.74% 该用户从未签到
|
< >不知道你找出的问题在那儿,我判断是alloca和malloc的误用。c++中对指针(内存)的操作总是容易出错,这些系统函数的使用,最好先仔细看看help。</P>
' G; I+ t6 K& E; J/ ~) e< >alloca:</P>4 m7 W/ C( ~: i# T* D% _
< >Allocates temporary stack space.</P>" q a% ]# R- c, B2 [& B
< >alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>1 z2 T& J2 ?4 y" E" s" `# h, J3 d
< >malloc:</P>- x3 D! M, `2 k1 t; X
< >malloc allocates a block of size bytes from the memory heap. It allows a program to allocate memory explicitly as it抯 needed, and in the exact amounts needed.</P>
) W! ?" {; h% F2 x- v< >Allocates main memory.The heap is used for dynamic allocation of variable-sized blocks of memory. Many data structures, for example, trees and lists, naturally employ heap memory allocation.</P>
4 r5 M# ]$ p% r6 [ a5 Z$ j5 w' W+ z9 Q5 S# L2 q5 ?' g
< >以下是改后的代码</P>& L" U/ |2 d% r8 p
< >//---------------------------------------------------------------------------</P>0 f: Z9 W! T% W6 o
< >#include <vcl.h>
/ w6 U9 j& G3 o2 d) U) ?#pragma hdrstop</P>
$ ` u8 z1 O0 V/ M! d2 Y, z8 w< >#include "Unit1.h"' E6 P6 D' C9 T* u# e0 `
#include <fstream.h>
( a* p: t: o% V3 I; p#include <dir.h>
" \) I/ ]" B p2 H, _7 P. C! c#include <sys\stat.h>, n% z- Q7 g5 l% N
#include <malloc.h>: U/ Q4 @3 g* E1 ]& b* `1 }6 \
//---------------------------------------------------------------------------( f! M* j- N* f- J7 V; v3 H
#pragma package(smart_init)
- s* t2 ?( g, V! g1 s7 j& S w#pragma resource "*.dfm"
& P- l6 r5 w l* ~TForm1 *Form1;& C+ u$ p( A+ U5 B, @$ [% l8 N1 O1 B
unsigned char *BIN_Byte;* }1 Q M) T; G
long BIN_Byte_Len;8 m' `3 U0 _9 i0 ?1 J2 x" x
//---------------------------------------------------------------------------
+ Z; C6 |( I5 u8 k# r: {0 g' ]__fastcall TForm1::TForm1(TComponent* Owner)
8 |/ Q! K4 z2 V4 G0 O3 N : TForm(Owner)3 S% p. B2 U" C% [0 E
{
6 e" C; W, l& N+ }}
& ]3 @7 g5 d) G @5 c1 k5 D# Z//---------------------------------------------------------------------------5 n6 h" ~' v2 {! h9 }" _1 _: ~
void __fastcall TForm1::Button1Click(TObject *Sender)
n0 F N0 A/ b' _; P7 B8 Z+ R{
. i* s0 w* u1 e' Y+ y char sPath[MAXPATH];, i9 p! ~$ [$ T- b
struct stat statbuf;2 V" g" v4 e( H
FILE *stream;</P>
+ N5 v5 O1 a; A8 s) A< > getcwd(sPath,sizeof(sPath));- T* A6 V8 f0 k/ I7 d
OpenDialog1->InitialDir=sPath;2 }6 O3 \ L& v! m4 S; c
OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
) s& L/ l* S8 s0 K3 D/ c* u OpenDialog1->FileName="fileFrom.BIN";
9 E* L- O- [ P0 q! D) p* E# p: v if(OpenDialog1->Execute())
5 r. i2 f. ]1 A. v" W+ y- C {
' }& c& m; G# a. Z7 G6 A( D( f strcpy(sPath,OpenDialog1->FileName.c_str());9 X- ]" }4 ~6 B9 t. @) E; C- ~1 B) P7 m( v
stream=fopen(sPath,"rb");
0 B0 [' t8 p, f+ ] fstat(fileno(stream),&statbuf);. ^8 X* |, z# ~
BIN_Byte_Len=statbuf.st_size;
1 U3 @$ P, S j) t( |" I BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);
) Z* }3 X% Z. n7 C for(int i=0;i<BIN_Byte_Len;i++)
9 }+ a, A d. q fscanf(stream,"%c",&BIN_Byte); //read file# p" z. Y: i5 I+ e: O$ k
fclose(stream);3 e5 @, L0 O- `- V9 m
}: T2 ~; j# [4 |* E+ G
}
2 I& i; Y5 V* R" ?0 @/ B# o; r//---------------------------------------------------------------------------
+ I; X# |# G0 N& V9 q, rvoid __fastcall TForm1::Button2Click(TObject *Sender)1 j% _( q+ S9 J% H) C' f- ^
{, U9 `9 d) s: H8 L9 I8 [3 q, ^
char sPath[MAXPATH];, O: V Q( i/ l" ~. V
FILE *stream;3 R% B; l) w' p& `( E$ l; g
getcwd(sPath,sizeof(sPath));//App.Path- {7 Z" u( Z, h& C7 ^ t* L
strcat(sPath,"\\");
/ U- n6 f$ \; x. s& M& v SaveDialog1->InitialDir = sPath;
( i' C# T9 G8 o7 i, a* ^ SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";$ O( K' B6 `. y8 n8 a& w
SaveDialog1->FileName="fileTo.BIN";
9 l" o8 c0 I9 g/ ?+ {; f if(SaveDialog1->Execute())* e% x2 j/ e! t1 _$ t2 m3 l
{
% \( I/ I5 s N: s3 X! h strcpy(sPath , SaveDialog1->FileName.c_str());; ]+ G. Y, h! X/ F5 y2 L
stream=fopen(sPath,"wb");* H# L! V: s) }, Q" K4 V$ q5 W
for(int i=0;i<BIN_Byte_Len;i++)( \3 k+ G4 Q) X$ J9 ?! A
fprintf(stream,"%c",BIN_Byte); //save file% N$ s6 W$ }% V0 b% u" z( B
fclose(stream);" i( x! w& R" `& k' V
}
7 t! m. l- ^! Y& e8 r* \ free(BIN_Byte);$ c% y2 u }. s7 h7 |' B
}
/ M$ R$ I, g2 |//--------------------------------------------------------------------------- K: |: F4 B% x l$ N0 j6 _) C
void __fastcall TForm1::FormDestroy(TObject *Sender)
5 Y2 S8 ^+ g4 y{
/ p! s. ^9 y% u( p9 S9 d/ h if (BIN_Byte!=NULL)
. C+ X! l& c, P5 N5 Q$ ? free(BIN_Byte);
$ }; n! A7 J& f! G. Q' I}
# n& g$ j' B. J( G//---------------------------------------------------------------------------
9 _# \. P( X3 ~6 `2 Q( q. L: e</P>
; p3 q+ |9 P, B< >free(sPath); free(stream);此两句也有问题。</P>
+ X) m( b5 f" U# m< >char sPath[MAXPATH];不需要释放;</P>
p' F/ t8 Q: \* X< >fclose(stream)已经起到free(stream)的作用</P>
" C$ |4 x: F# t% g" u! S$ {[此贴子已经被作者于2004-6-15 15:16:40编辑过] |
|