- 在线时间
- 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>* O [ Y3 Z w1 u& _
< >alloca:</P>- U- |$ O: }2 g9 k" ?
< >Allocates temporary stack space.</P>
& I$ n# A' D+ p/ _! e, Z8 M) a< >alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>' M& s6 g& T+ I+ H3 X6 p
< >malloc:</P>7 |3 ]* X5 J+ O0 e+ Y8 C6 x4 [
< >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 [. D8 {! t" j- R< >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>
; ]+ A. w4 h9 N/ r- O! [* z5 Y, i! i" A4 K
< >以下是改后的代码</P>7 X( c1 C5 y& G) n
< >//---------------------------------------------------------------------------</P>7 u6 b! ^5 l3 }( p% f6 N H
< >#include <vcl.h>
" L8 y- M7 V9 Q6 a' T9 k* @! N#pragma hdrstop</P>
* P4 s G8 [8 q+ [0 m< >#include "Unit1.h"* L7 E+ k9 G& C) b
#include <fstream.h>1 ~/ a8 n8 X. A
#include <dir.h>; u5 Q, V! Q* N% W
#include <sys\stat.h>
3 J' x, @& s! A2 j8 w#include <malloc.h>5 V, A2 H2 J6 `/ ]: D0 `
//---------------------------------------------------------------------------
6 B( L. }, H. X/ n* _0 I! U#pragma package(smart_init)
) o: Y$ R( w Y5 w#pragma resource "*.dfm"
% p$ B, E ]6 E0 W/ ~TForm1 *Form1;
! F) j6 @' c, w unsigned char *BIN_Byte;9 ^* V5 E2 I/ z8 K
long BIN_Byte_Len;
2 ^6 R- j4 ?4 p# G' l//---------------------------------------------------------------------------% d4 |% g! C* J6 Z, B
__fastcall TForm1::TForm1(TComponent* Owner)! v; h; C+ y" @; s3 F4 M' O, u
: TForm(Owner), |# _* r1 |3 h! q- r- W7 q4 X
{
) V* G( I4 E1 _" p/ I}. _' e& t, h3 D7 ^! B2 J: f- K( t
//---------------------------------------------------------------------------
- Z' n S" _* a# [' ^. e) p3 @2 Bvoid __fastcall TForm1::Button1Click(TObject *Sender) E/ D5 T" [; z4 c3 q
{
' C8 X* A: ~( o( K4 J' a( ^; R char sPath[MAXPATH];
; g! S3 Z1 h, `: U7 a$ ^ struct stat statbuf;
# y3 z3 v+ ^+ j/ u! U1 s' x4 B8 F FILE *stream;</P>
0 K+ C3 r/ Z7 f0 ~< > getcwd(sPath,sizeof(sPath));
6 m# U# A* E) A$ i$ d3 v- g OpenDialog1->InitialDir=sPath;
( \, I9 S( Q [; n& `: ]! { OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
8 b* Q' g: b! H OpenDialog1->FileName="fileFrom.BIN";
! b. l/ a3 r9 p% [# E if(OpenDialog1->Execute())
6 X' O G( B# p o {
6 Z/ g; s4 Q3 L' P9 q8 M strcpy(sPath,OpenDialog1->FileName.c_str());
2 u" i1 Q3 ~/ G: t3 ?: U2 q* B1 W7 Q stream=fopen(sPath,"rb");, P) k! f) c! f
fstat(fileno(stream),&statbuf);
) n1 B0 D. ~ O T5 g: E BIN_Byte_Len=statbuf.st_size;& s6 e. k5 D2 v$ i T
BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);
/ p6 [" ~+ a! E2 F% C. X3 n for(int i=0;i<BIN_Byte_Len;i++)1 K2 f* t7 y+ B! [; b; B5 ]6 e
fscanf(stream,"%c",&BIN_Byte); //read file M* F" n" t4 I6 J1 l. ^
fclose(stream);+ x+ {+ B$ i- v1 X7 j/ L& ]& k# t
}
, A; Y: {" K4 m4 v: k w# _}
- z6 T5 T1 A" q6 X6 m8 B//---------------------------------------------------------------------------
, ?6 S5 N8 L" D' d" }( P1 M# cvoid __fastcall TForm1::Button2Click(TObject *Sender)$ k6 @7 V1 Z4 F( p9 G
{
3 }0 P @: I2 q* c0 J" x$ n char sPath[MAXPATH];9 c }1 r+ [8 Z- W o$ h# R
FILE *stream;6 H+ _ J0 g1 A$ k- W- @9 p# G
getcwd(sPath,sizeof(sPath));//App.Path
3 N' l( f# O) V! u% B# { strcat(sPath,"\\");$ Q* p2 }7 A. Z# }
SaveDialog1->InitialDir = sPath;& z3 J8 _9 p+ _* T8 K7 y' u8 H
SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";9 q$ h3 y: R0 ]* d- O( n
SaveDialog1->FileName="fileTo.BIN";
g$ Q u# h9 a/ k7 I1 d if(SaveDialog1->Execute())9 k8 T9 R$ j3 T2 b2 o% d
{) `. ~- ^7 l( R
strcpy(sPath , SaveDialog1->FileName.c_str());" l0 L W2 h0 G
stream=fopen(sPath,"wb");, x# G9 E$ T! ] p
for(int i=0;i<BIN_Byte_Len;i++)
6 C8 I3 z! w' M6 \ fprintf(stream,"%c",BIN_Byte); //save file* j9 g+ m% c2 x+ ]' B7 y
fclose(stream);" V: |% P( x& G* D* E$ |
}, Y" A0 M- |3 f- j2 M
free(BIN_Byte);( `# p3 z( o9 v# o. G- U
}4 }% K( a. N+ @2 n" K3 i8 L; P/ I
//---------------------------------------------------------------------------
+ {& D2 R6 h: Bvoid __fastcall TForm1::FormDestroy(TObject *Sender)4 \1 T9 z0 J! y8 n
{
$ q' ~- E' t0 k9 j% x# @3 l if (BIN_Byte!=NULL); v* m. R( D1 S$ t, V; [
free(BIN_Byte);# f& @. ?8 M5 R( n x& s7 s; A
}
) O+ O( J' X3 Y. Y9 N//---------------------------------------------------------------------------
, d0 W% o2 a8 i</P>) g/ \; h' v6 S2 ~
< >free(sPath); free(stream);此两句也有问题。</P>6 x9 ~5 z' ^- Q% ]9 e7 e
< >char sPath[MAXPATH];不需要释放;</P>, k8 v( x3 u, a. M
< >fclose(stream)已经起到free(stream)的作用</P>
# O2 j2 \0 Y* R% L4 L[此贴子已经被作者于2004-6-15 15:16:40编辑过] |
|