- 在线时间
- 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>& t1 d. M$ X% N
< >alloca:</P>
1 v+ b* o x3 }< >Allocates temporary stack space.</P>. a/ U1 H: {. H! N( E, m
< >alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>3 U( F7 k- r+ m* p' g- o. e
< >malloc:</P>
4 D$ ]( |' L8 f' U4 O< >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>
2 z% R: \& d# r# ~ n; y< >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>
0 [# R, n+ H9 |! Y# [7 I' f2 L0 m4 ^
< >以下是改后的代码</P>. s! ?7 U: m+ t+ S1 j' f/ I
< >//---------------------------------------------------------------------------</P>
) [ |4 o* w1 e# I g2 X< >#include <vcl.h>9 [# V* ^4 j0 ]' `+ c2 b% p
#pragma hdrstop</P>7 A2 A% l" n# h8 w9 q6 o8 J
< >#include "Unit1.h"$ n( j3 b- {, h& Y. Z
#include <fstream.h>
9 {; q8 j: P5 f6 c5 X! v; l2 U#include <dir.h>6 y) ^! f- \. J! O! E
#include <sys\stat.h>
3 n% P* s' J9 Q9 m, q+ ?#include <malloc.h>/ D3 V; y* x3 S7 B
//---------------------------------------------------------------------------. c Z& ~+ f+ Z y! |: F
#pragma package(smart_init)
/ b) z, E& U5 t9 J3 ^#pragma resource "*.dfm"
: U7 S5 x1 B: j$ b6 q# GTForm1 *Form1;
4 [+ f' A( y Q( z$ l2 k unsigned char *BIN_Byte;
/ j; a1 ]) L; r0 e long BIN_Byte_Len;5 M% q7 }% T; {5 I$ |! c; @
//---------------------------------------------------------------------------4 `0 T9 G( j m2 _; ^2 w, n F
__fastcall TForm1::TForm1(TComponent* Owner)
1 F x7 o( f; b2 q* g : TForm(Owner)
5 B0 u5 }/ x% H. L; g$ D4 z! G{
) V( K. i2 T* z R}5 i2 ]. H3 V3 @% m; ?
//---------------------------------------------------------------------------
- F7 X1 \# a' P: W! Ivoid __fastcall TForm1::Button1Click(TObject *Sender)
& r! g. w7 V' @8 f4 R' H# a+ F, y{: R- ^# W; R9 @% `; Q* X2 _3 _# t! X8 O
char sPath[MAXPATH];
$ j# k" W: s$ }+ k" Q struct stat statbuf;& J( X) i( D: Z
FILE *stream;</P>2 H7 \5 y+ Z0 h* R& w* d& b# p
< > getcwd(sPath,sizeof(sPath));' T& o) p4 w% ?( z8 U
OpenDialog1->InitialDir=sPath;
2 Z' l$ \) F: r3 N# A OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
6 D( q, L ?: c P2 ^! U- g6 S OpenDialog1->FileName="fileFrom.BIN";
0 v& C4 n5 s V0 f3 S2 H. x2 }1 o' u if(OpenDialog1->Execute()), \7 i/ y; H3 ]4 ]
{5 A# U& ~1 }' P j: L& u5 _ C4 v
strcpy(sPath,OpenDialog1->FileName.c_str());1 O3 x _7 q) @- N3 f3 n6 q
stream=fopen(sPath,"rb");" @6 s( a* R; b
fstat(fileno(stream),&statbuf);; b, G( `, A3 b, g- n
BIN_Byte_Len=statbuf.st_size;
- u8 ~7 V2 a r) @ BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);7 @1 t" A2 ~ }0 `9 z/ f' Y: n
for(int i=0;i<BIN_Byte_Len;i++)
' \9 {, X0 L+ X& J: \% Y6 D6 g fscanf(stream,"%c",&BIN_Byte); //read file
4 N# ~$ V8 k: B" {5 S fclose(stream);" u- d D* v, F( |* c
}
$ j- Y* B) m1 `9 ~6 H: y}
6 E& _3 X0 u1 ^6 k) v+ g$ j//---------------------------------------------------------------------------/ b8 p4 I: s* x. ]/ @& l$ D
void __fastcall TForm1::Button2Click(TObject *Sender)
! u2 W: |8 h9 o{
9 P% k2 E5 R- O9 h char sPath[MAXPATH];* U* O0 C+ I) Q% b2 p: M% j
FILE *stream;" m! ?0 l& h) N
getcwd(sPath,sizeof(sPath));//App.Path/ T0 B& W/ y, k7 h6 t; ]+ _7 J( b
strcat(sPath,"\\");0 W1 \& A6 s2 V# i* h
SaveDialog1->InitialDir = sPath;( H) \) b6 w) l* q7 j1 ^7 q7 X: E
SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
! c( r& j. O6 e! P7 w SaveDialog1->FileName="fileTo.BIN";
3 S" q# Q) r0 J if(SaveDialog1->Execute())3 m8 z% B5 e2 _' T( o+ L/ ]( y
{3 d* g; N+ S5 r
strcpy(sPath , SaveDialog1->FileName.c_str());# Y# G* k$ Y% `2 o& ]
stream=fopen(sPath,"wb");
( W6 I2 O3 I0 Y% Z l: U for(int i=0;i<BIN_Byte_Len;i++)6 Q7 E# r. U' F8 h, D+ _* a1 ^+ u
fprintf(stream,"%c",BIN_Byte); //save file
) @# k& g2 q% T fclose(stream);/ c/ R5 {/ t+ c; @8 i! M. s
}
9 T9 U$ @6 l& ]4 e w5 V free(BIN_Byte);) O0 n1 E3 U- y. I2 w! A2 A
}, P) u9 z: H3 Q: ?! \+ `+ f
//---------------------------------------------------------------------------
/ p+ w# P a& g' gvoid __fastcall TForm1::FormDestroy(TObject *Sender)
0 c0 n% D, B+ z2 W+ X{
7 d; N. ]$ z, A; h if (BIN_Byte!=NULL)8 D A& V6 O- J* ]/ f, i4 ]) ], }
free(BIN_Byte);# r% r" d4 a, i
}
+ j# W5 b j0 r. y/ ]+ |- Q* i8 T//---------------------------------------------------------------------------
" }4 c8 k# c! Z</P>: N" c" ]! Z3 y% U* j/ Q! d
< >free(sPath); free(stream);此两句也有问题。</P>+ Z$ ]8 G; G4 B
< >char sPath[MAXPATH];不需要释放;</P>7 M! C" V! n: j' X+ p! q
< >fclose(stream)已经起到free(stream)的作用</P>
1 m5 Z+ h0 b1 e' f8 K: x[此贴子已经被作者于2004-6-15 15:16:40编辑过] |
|