- 在线时间
- 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>* |1 O; U7 u2 }6 l; N9 B! }( t! A
< >alloca:</P>
2 V. D2 T5 D/ X/ G4 g: i9 U< >Allocates temporary stack space.</P>, [8 P2 [. z ?$ m" E
< >alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>' c1 A3 T& t5 a* Q( M
< >malloc:</P>9 }1 b/ D" X7 @3 O, F
< >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>
}- R& u/ u q5 L+ l( s< >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>- R7 V/ w- l; Z% @8 M- a
! w" W; D2 Y6 v! M- W< >以下是改后的代码</P>+ `6 i" x! T5 {0 r
< >//---------------------------------------------------------------------------</P>- t- m$ G0 E4 S" F, X# w* o( }
< >#include <vcl.h>1 {0 J d" W( c
#pragma hdrstop</P>+ h$ D9 P; V. u3 K4 [' {' K4 n
< >#include "Unit1.h"
/ `# ~. a/ a0 X& t, N#include <fstream.h>; Z# q4 \4 M4 l5 U: f: O$ S1 N
#include <dir.h>
5 Z3 x6 k$ r: m2 m#include <sys\stat.h>/ D, L# ], v# A2 _
#include <malloc.h>1 B2 _8 g# Z% @& ?! n0 b
//---------------------------------------------------------------------------4 @. w+ e$ w' o. M
#pragma package(smart_init)
8 w5 e* u2 l% q, q" y B#pragma resource "*.dfm"
* T' D2 ^! b; l/ t: K3 xTForm1 *Form1;
/ D* M8 {6 C, c$ ~ unsigned char *BIN_Byte; X; N8 Z: \% A. ]
long BIN_Byte_Len;- m4 {/ x D, c. A/ O. M
//---------------------------------------------------------------------------
3 x1 I' t' ^ U1 `& N6 t/ D8 Q__fastcall TForm1::TForm1(TComponent* Owner)
5 t; T* z- h4 I& A+ G+ v$ } : TForm(Owner)5 }8 z9 x0 W7 G, i' I7 ?
{! n% Q o( m! d |* B- y1 ^; c
}, A! J% u" u% o% o- X" Z1 ]1 |
//---------------------------------------------------------------------------
4 x" Y) c7 Z1 @; ?, Lvoid __fastcall TForm1::Button1Click(TObject *Sender), E( V; n0 O) j" T; c
{. S8 R) U$ ~2 `3 l% X% T k! B5 T
char sPath[MAXPATH];: V. ?* d0 m% r' C3 ]; L
struct stat statbuf;
2 R% ?# @* _( d2 ]* O# R FILE *stream;</P>+ o- }; W2 k1 `( f% e# {. p6 V
< > getcwd(sPath,sizeof(sPath));
- }: E ?0 s- t3 w+ p5 R8 P OpenDialog1->InitialDir=sPath;. w1 J9 ]& c5 h, g
OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";" T' P2 }' \7 w
OpenDialog1->FileName="fileFrom.BIN";
, w% }0 e3 T6 d! r* a if(OpenDialog1->Execute())* e, S% E8 G6 G! H8 o
{
: j% g; [1 i: i3 @ strcpy(sPath,OpenDialog1->FileName.c_str());
. A& H8 n& ~( `$ A0 C$ c/ B stream=fopen(sPath,"rb");
( o% u2 |' D$ U fstat(fileno(stream),&statbuf);
, a# l- @* y4 \, r% J BIN_Byte_Len=statbuf.st_size;+ E* |$ p6 P7 ~
BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);
) B' u5 E, _% o1 \; b& c for(int i=0;i<BIN_Byte_Len;i++)
' ^2 s: I- ?+ F" m9 i fscanf(stream,"%c",&BIN_Byte); //read file
7 U: m' v" A$ A; s# T9 `2 ~ fclose(stream);7 k3 h, e. o( l8 O) ?3 y
}6 o3 ]/ `; r; x4 I, c
}
$ E) v9 {' S) h: E! d//---------------------------------------------------------------------------6 J; _' d' z- d& d
void __fastcall TForm1::Button2Click(TObject *Sender): K# M+ e" |: J7 W8 N
{+ x V$ {0 V6 l6 g Y
char sPath[MAXPATH];
2 c7 M, \- N, u. _/ X. w. @4 ` FILE *stream;7 D; @- |3 E+ `
getcwd(sPath,sizeof(sPath));//App.Path+ R6 o4 k- E3 h- ?/ w
strcat(sPath,"\\");9 ?3 J& n" i2 V" ?6 e5 l9 `
SaveDialog1->InitialDir = sPath;
2 D( N1 f$ `: i: f SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
' b6 V0 Y, J; y6 V$ m SaveDialog1->FileName="fileTo.BIN";
" H0 j3 M4 E- G) f if(SaveDialog1->Execute())# p) @5 j _7 W2 _/ x3 ]
{) \6 j, ~' p- I
strcpy(sPath , SaveDialog1->FileName.c_str());0 M# [# U; J& v: s
stream=fopen(sPath,"wb");' p& u% B6 M9 y I% U/ C
for(int i=0;i<BIN_Byte_Len;i++): C( `: H3 p0 h7 B, O# X7 d
fprintf(stream,"%c",BIN_Byte); //save file# l' m% a$ x* L8 S9 G
fclose(stream);. c( y% W3 I* u4 H7 i5 ?
}2 `: k$ u5 Z% M# o7 O0 G% l
free(BIN_Byte);5 o2 J' s8 U$ _( h6 S/ `
}8 U4 k3 q z5 z$ |+ A' y
//---------------------------------------------------------------------------
' u9 I& N- M) a! v0 \$ s- O. qvoid __fastcall TForm1::FormDestroy(TObject *Sender)& Z) m v- R- U$ x/ l- }1 I: c2 L
{. @ e$ A. T" [' }* X/ n6 {6 c
if (BIN_Byte!=NULL)
( C% r: i/ ]& O0 N$ w, V free(BIN_Byte);( ~% U1 u# Z- x
}5 ~% ^! h7 U9 H5 E* c
//---------------------------------------------------------------------------$ B7 P/ b0 \9 \; t
</P>
6 J' H4 P, O, h$ G' A< >free(sPath); free(stream);此两句也有问题。</P>3 p5 D& e3 H3 T" ]- d
< >char sPath[MAXPATH];不需要释放;</P>
7 R$ b. q- P; Y& ` S7 h/ D7 |3 D: o< >fclose(stream)已经起到free(stream)的作用</P>
0 B6 ~4 q, X+ H[此贴子已经被作者于2004-6-15 15:16:40编辑过] |
|