- 在线时间
- 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>, ~# u4 f+ q8 M4 v; ^9 E
< >alloca:</P>7 R! r$ L( {6 y) E; U
< >Allocates temporary stack space.</P>
+ c" T& u1 z" k* }/ z< >alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>4 @2 z" e& V8 M Y5 [: k% K& `0 ^* @
< >malloc:</P>
" `2 X I2 x8 u% t& E! `1 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>" T9 h. J* p2 P6 Z5 ]8 j
< >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>
6 x* O/ ~$ @, h) n! Q# i' S0 o! }; z) y, U3 [- x
< >以下是改后的代码</P>( r, Z* h2 I- p: z' ]; S
< >//---------------------------------------------------------------------------</P>
9 p- J, m3 c& s9 D) ?- `< >#include <vcl.h>
% L# G( |/ {% G6 T) T. o#pragma hdrstop</P>7 Q `5 E8 i8 [8 V1 q$ Y4 W
< >#include "Unit1.h"
6 Q* t+ C+ L, m( T) k; o' z: ]/ d#include <fstream.h>
. |% l- b% t6 ?( B) k# }: `#include <dir.h>, W/ A. ^( N H8 W
#include <sys\stat.h>) |/ W, z- F: R p0 h
#include <malloc.h>* {* ]# d& u% ~: D- N0 C8 Q1 z
//---------------------------------------------------------------------------0 w4 A e& x$ y+ f
#pragma package(smart_init)
6 n/ R# ^. P& H8 v8 M$ _! a9 x8 p#pragma resource "*.dfm"
5 h% u; L$ V4 A7 `1 e3 A' h! aTForm1 *Form1;1 `4 k' j* e- [$ i9 X
unsigned char *BIN_Byte;- V) z- p, t3 v: Y q* S
long BIN_Byte_Len;: s' r; ^& s3 x, D$ q
//---------------------------------------------------------------------------
$ E+ l! q# G5 J- Z9 A( O6 F1 w* ?4 `__fastcall TForm1::TForm1(TComponent* Owner)
5 S w/ R8 o/ B/ f! d& _ : TForm(Owner)
( H4 S4 B, H1 o; o% m% E- O9 C5 X{$ U: s% @. L9 D3 d* k7 \
}
6 N+ I4 t; ]3 @8 e//---------------------------------------------------------------------------7 w" j6 [0 Q$ \2 {$ W, }
void __fastcall TForm1::Button1Click(TObject *Sender)
9 \0 X+ t1 [+ l0 ]3 T{9 P$ S0 N' U7 v
char sPath[MAXPATH];
) y# ~6 h' d& x) k struct stat statbuf;7 v6 l% }# p, R
FILE *stream;</P>, t, |) Z. z) k
< > getcwd(sPath,sizeof(sPath));8 D5 J7 J' m9 J/ u
OpenDialog1->InitialDir=sPath;- C# q. E1 t' w$ \+ k6 x
OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";, I" |# P. l& g F" Y+ C- A0 l
OpenDialog1->FileName="fileFrom.BIN";
1 J2 l; T8 g- @ if(OpenDialog1->Execute())
. @, L, N; M1 O9 U2 c) q {8 |( z5 v# V1 b# H
strcpy(sPath,OpenDialog1->FileName.c_str());! F* f* a0 [9 U6 _3 n
stream=fopen(sPath,"rb");
; x3 _6 m# f+ h4 @/ b. v, E fstat(fileno(stream),&statbuf);
2 x4 _) { Z: q6 c8 t3 o BIN_Byte_Len=statbuf.st_size;! T: C/ G0 ^3 B: Q. ]8 y5 I0 c6 v
BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);- x: A8 F9 i. W# p5 Y3 G1 }, b c
for(int i=0;i<BIN_Byte_Len;i++)8 p V" ^: t$ u3 d5 V- R7 n/ O
fscanf(stream,"%c",&BIN_Byte); //read file
K4 Z- z- _ O. S0 w, n fclose(stream); |( [5 v0 G' _$ q; G
}6 |* @! J4 U6 B1 N. ^! R
}
% T c% c4 R* Q//---------------------------------------------------------------------------
( i0 F/ r2 |. M, Qvoid __fastcall TForm1::Button2Click(TObject *Sender)
& O2 V1 n0 h- A% n1 g2 M C{/ h+ d7 v. x# I& [8 ]( m1 v/ a
char sPath[MAXPATH];
) O3 L/ Q* }. {4 I4 E5 `! A FILE *stream;9 o7 X$ Q$ [+ G6 c$ g$ P' m0 ^
getcwd(sPath,sizeof(sPath));//App.Path
2 C. |, B5 V/ v+ ?) o2 v' ]; ? strcat(sPath,"\\");
% [; ^2 ] F9 U* ?# m/ d SaveDialog1->InitialDir = sPath;6 j& ?" Q8 k, P1 q. V( m# l, s5 k
SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
6 c& P" {! r8 V& J S& I" f: P2 |" G SaveDialog1->FileName="fileTo.BIN";
8 _( a$ b% d2 W" x. \ if(SaveDialog1->Execute())
1 r( C% [ o) b; w* Z7 M {5 w4 C a5 R6 R
strcpy(sPath , SaveDialog1->FileName.c_str());5 s0 M! T$ V- W9 R: D/ O
stream=fopen(sPath,"wb");
, R/ Z' C% A! k2 [! F for(int i=0;i<BIN_Byte_Len;i++)# S% T3 J3 Q1 B
fprintf(stream,"%c",BIN_Byte); //save file2 K N, T: c/ a
fclose(stream);
' {! t+ F: I7 k* X1 f9 _/ T }
$ ^' X* q; j, x }" E7 Z free(BIN_Byte);5 ~4 b+ b# S6 W9 M! d
}3 [6 d+ C4 [" v% _- F5 `+ w. n
//---------------------------------------------------------------------------( v0 m: Q2 l6 ?7 j) }% Z- ]- @% z1 E
void __fastcall TForm1::FormDestroy(TObject *Sender)- o1 P) |0 o, i! X& z
{
4 O6 I5 X$ R7 G( t0 K" A# R if (BIN_Byte!=NULL)% @1 L. Z+ _& H! k3 q0 R7 j% z" J
free(BIN_Byte);
& ?# ^8 B3 z, F. o! ?% g: C# V}
$ ]# ?1 B7 W. ?' I//---------------------------------------------------------------------------
$ E D8 A+ \* n$ r- J7 L</P>$ b+ j) K( v% h( Y
< >free(sPath); free(stream);此两句也有问题。</P>. T1 [+ b& @0 |7 h
< >char sPath[MAXPATH];不需要释放;</P>
1 ^2 q9 t. T7 O5 h( Z< >fclose(stream)已经起到free(stream)的作用</P>$ L L& ~4 _7 p! I) _* |' c2 a" @
[此贴子已经被作者于2004-6-15 15:16:40编辑过] |
|