- 在线时间
- 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>
9 z$ S3 D1 B4 `- r7 X% X& |< >alloca:</P>, Q# Z0 d. s' ^7 s
< >Allocates temporary stack space.</P>( x$ Z" ^& T; R3 b: C& M" A; E
< >alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>
/ U% X1 H. a+ [< >malloc:</P>
/ I% J: O6 v* ]8 @7 ]0 y0 P< >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>. k) P d1 I# c% \$ w- d! 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>" X9 g7 C% L, z/ X4 V e
) n* n3 @/ J4 {( L: r p# x
< >以下是改后的代码</P>
( H- k; t9 O$ V O9 A( p8 y% A< >//---------------------------------------------------------------------------</P> W/ _/ H6 ]1 [3 g( M" G% Z7 w: H
< >#include <vcl.h>
: t, l# P: o( r1 S; Q+ g, M4 M3 m n+ j$ A#pragma hdrstop</P>" I% S0 S" E8 _% ^& m' S/ n
< >#include "Unit1.h"7 s7 ?! O& x3 O, G+ p
#include <fstream.h>) ~2 m/ U! u) Q6 v V$ ^
#include <dir.h>
+ w/ \! ~0 |# g9 |8 h- t#include <sys\stat.h>( u) Z0 Z* g- k6 k+ S+ h+ R( O
#include <malloc.h>
4 B( k l% Q9 \7 I//---------------------------------------------------------------------------! W5 h5 @/ u& }* I3 }8 w7 J0 v
#pragma package(smart_init)
1 c) q6 z4 X% O; i/ l, i4 c7 a#pragma resource "*.dfm"& k, }! M8 q+ V& I3 j( q/ S$ K) Y
TForm1 *Form1;
* h( v% R1 O9 e3 g' u unsigned char *BIN_Byte;, w" ], s" s( h
long BIN_Byte_Len;/ D6 J% ?% d5 L% m, v7 V& ~3 }: Y& V
//---------------------------------------------------------------------------
% A/ N" J+ X3 {2 F__fastcall TForm1::TForm1(TComponent* Owner)
( `" U, c- G+ c" t) v1 r : TForm(Owner)
. _$ k( u" Y0 ^{
9 \/ r3 @9 `* \/ D6 K}# E9 I& d/ r* K
//---------------------------------------------------------------------------: \0 @9 T5 M2 }! B2 }- n
void __fastcall TForm1::Button1Click(TObject *Sender)- t& X, K7 v9 t) h
{/ W7 p8 K0 D* S8 D8 m+ {
char sPath[MAXPATH];3 M* V, v, c! h" o
struct stat statbuf;
. O: f# W% h4 A* v& d) U! v FILE *stream;</P>
3 I3 J8 U; i; q3 I, l< > getcwd(sPath,sizeof(sPath));
+ @1 _- T" X6 s) \8 X& ^* c OpenDialog1->InitialDir=sPath;: z+ v+ U: N0 ~; e1 S, Y$ A! z) c
OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";. @6 C+ `1 o; ~$ H) Q
OpenDialog1->FileName="fileFrom.BIN";8 s/ e; a( r* P% o. L
if(OpenDialog1->Execute())
6 R, u! I" d9 K( g { q8 {# z: e& V3 F- e2 p
strcpy(sPath,OpenDialog1->FileName.c_str());
% x9 w/ | G0 D3 ^ stream=fopen(sPath,"rb");
! B+ y$ G" }3 `. |) n( I fstat(fileno(stream),&statbuf);9 |8 N( Z4 A# {3 w2 a( e& ]( ^
BIN_Byte_Len=statbuf.st_size;& j9 }, R9 \/ \3 t/ p
BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);$ w: P, o5 h& p4 l
for(int i=0;i<BIN_Byte_Len;i++)8 |6 ]3 G6 R1 ? t2 Q, R
fscanf(stream,"%c",&BIN_Byte); //read file
5 H) B$ O# m" a/ T3 F" c/ ? fclose(stream);! m* _) @- G. i7 P M
}
2 K$ ]$ g- V4 q6 X! t5 ?6 ~}( Y; R U. i1 x: r. Q
//---------------------------------------------------------------------------3 m1 x+ v% W4 m' C8 v
void __fastcall TForm1::Button2Click(TObject *Sender)
& T. A$ c+ Y3 u% c! B{5 s+ v) l/ ^1 s; ~& _2 N
char sPath[MAXPATH];
" q7 f7 ~: D' ` FILE *stream;; N. u' v+ h0 Y3 P1 G o1 z
getcwd(sPath,sizeof(sPath));//App.Path3 ^7 l+ ~% L5 |& q8 v1 L0 F
strcat(sPath,"\\");$ b2 ]* H) i& L3 A7 g n
SaveDialog1->InitialDir = sPath;6 x6 G0 }! e% ?3 C1 P) T
SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";$ B+ B* F6 O. P& E2 Y0 T/ {
SaveDialog1->FileName="fileTo.BIN";
% c! \, H O# q: I; ]1 l/ Z if(SaveDialog1->Execute())
. ^! c6 ]! E C; h, o1 M {
# v; a& _- C. f1 F strcpy(sPath , SaveDialog1->FileName.c_str());8 Q, R8 y% ~# t- Q. m
stream=fopen(sPath,"wb");
T8 c; O+ v# h* t) j, z( z. n for(int i=0;i<BIN_Byte_Len;i++)
2 u1 H0 m9 A: `2 D9 V fprintf(stream,"%c",BIN_Byte); //save file- n7 ~2 y% R$ a ~9 y8 T2 ?
fclose(stream);/ \. J- p+ r& h5 b8 _0 S3 U2 [
}
7 r; h% Q3 P; t9 o9 y: j/ |6 H4 Z free(BIN_Byte);
0 k ^/ y/ h; m9 {+ `! n8 J}
& k1 U& b: ?: a/ v- \8 ]$ f9 E//---------------------------------------------------------------------------* O1 z E2 m) o3 w7 P `$ D8 t3 U
void __fastcall TForm1::FormDestroy(TObject *Sender)
; v6 f+ S' k! _$ r3 [, V{9 x8 h" X: }: b) B9 y) _! P7 z. Z
if (BIN_Byte!=NULL)3 A0 _; t, u0 x5 L% h
free(BIN_Byte);
7 s8 } b- w3 e5 E8 M}
# z* c, H' L8 J' v//---------------------------------------------------------------------------3 x* M( e9 H2 ~
</P>1 e2 M/ Q, a, `' H, L
< >free(sPath); free(stream);此两句也有问题。</P>
: E* }- t3 b+ W/ v< >char sPath[MAXPATH];不需要释放;</P>
8 @5 n5 g# @ y5 h, p! [& [; U< >fclose(stream)已经起到free(stream)的作用</P>7 e) V, o9 P5 X" e/ u$ \; w
[此贴子已经被作者于2004-6-15 15:16:40编辑过] |
|