- 在线时间
- 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>0 k6 @2 _' h: Z7 f9 ?
< >alloca:</P>% V4 P! j7 O! i8 i0 j/ L
< >Allocates temporary stack space.</P>* O7 T+ f8 Z4 n+ V. `/ L
< >alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>0 O1 c4 X; q2 U2 _4 R
< >malloc:</P>
8 |# e& O. y1 G% i< >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>
& |6 i5 |3 V* U. F1 X! x$ [< >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>
, Z* ?. z L0 P% j& g% Z R, C8 n; }: x2 S5 U9 z$ N8 ~
< >以下是改后的代码</P>' Z9 c1 @2 r, C- p* E' N
< >//---------------------------------------------------------------------------</P>
6 v$ S$ `) |! ], i' M< >#include <vcl.h>7 X9 f+ M$ X3 U6 _; ]
#pragma hdrstop</P>$ p+ B" h7 X. i: b' C/ P
< >#include "Unit1.h"( _/ \) x$ Z+ _% f) c7 j
#include <fstream.h>
0 |$ I8 `- g3 I& o W7 T4 G3 x% C#include <dir.h>* ^1 P) \! p" O; Z- D1 `
#include <sys\stat.h>
1 k2 W# r3 c) X" z6 N: H! \#include <malloc.h>, S* Q s# H! E8 Z5 |8 a' c
//---------------------------------------------------------------------------& x4 s, L! _+ {, _( j8 k
#pragma package(smart_init)
4 `: L! I/ x3 c& m. t6 E#pragma resource "*.dfm"
) D$ s% A9 W5 V. e. jTForm1 *Form1;/ V7 \9 k1 z4 ^: x% u6 J
unsigned char *BIN_Byte;
5 }+ A% ]- M- u- Q: x9 p4 f long BIN_Byte_Len;
0 z, f7 j6 z m+ U//---------------------------------------------------------------------------4 E: C( g3 p$ K4 l0 J4 P6 ~
__fastcall TForm1::TForm1(TComponent* Owner)* M2 V$ d) f1 [' n) f9 g6 s+ K
: TForm(Owner)
) S! L ?5 R; x. U' C2 R{
( S( w, Q# ^- ]5 b}
, Q1 \' r' o3 z7 E//---------------------------------------------------------------------------3 D, J- G8 h: j+ W# S8 r, \$ ]
void __fastcall TForm1::Button1Click(TObject *Sender)
/ z8 r. z! J' T0 L2 B2 ]5 e{& r' [1 k1 N5 s5 {- y' J
char sPath[MAXPATH];# L2 h* @: `+ ?, @# ^$ h1 T9 n
struct stat statbuf;, s. {" `: c8 U; w2 k5 i G
FILE *stream;</P>$ L$ D0 m% i) n6 t
< > getcwd(sPath,sizeof(sPath));
. X1 A0 \3 l' D) S; ?- d8 K5 q0 u OpenDialog1->InitialDir=sPath;
9 h5 S( M( ]" }7 F0 B6 M! z! W OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";. X* Q: n! |9 e3 H; a* H
OpenDialog1->FileName="fileFrom.BIN";! ?' H& ~9 p2 b6 t/ c
if(OpenDialog1->Execute())$ ^3 g% r0 L) u5 c6 k' ?0 ?8 J; b+ R
{
8 W5 s" W W; f4 E strcpy(sPath,OpenDialog1->FileName.c_str());
6 \( }; }1 `0 U; A stream=fopen(sPath,"rb");5 p( w; U' i: H' y9 W8 b9 y* j
fstat(fileno(stream),&statbuf);
/ \+ j) G" N1 {( m. m, R/ I" k BIN_Byte_Len=statbuf.st_size;5 J/ k5 w8 M, p6 u& V
BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);
( t; l( B% X( h) H- R, y% E for(int i=0;i<BIN_Byte_Len;i++)1 ~) y% t4 w& b6 k! b% P
fscanf(stream,"%c",&BIN_Byte); //read file, ~! R3 g+ Z+ ~: Z: _
fclose(stream);
, [' z9 i% R6 C }* |. k B: K% H0 ]7 @
}+ J; U9 q( I7 P% G7 K
//---------------------------------------------------------------------------& t5 U9 A4 _/ p0 D( M2 O
void __fastcall TForm1::Button2Click(TObject *Sender)
5 j1 V1 \) U3 V* O& ]8 {; J{5 q: P q/ b6 l
char sPath[MAXPATH];
& ]: [1 _3 a% X FILE *stream;
1 X* C1 i9 a, A8 o getcwd(sPath,sizeof(sPath));//App.Path
* w+ ~3 k/ O- t; h4 X strcat(sPath,"\\");, p m8 e3 b, f! {# g' m
SaveDialog1->InitialDir = sPath;+ D9 ~5 p. @" E6 o- L. Z5 _
SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";6 u" j$ s; R/ h+ ~* b$ ^5 c
SaveDialog1->FileName="fileTo.BIN";% y$ P3 l D u7 j H5 Q
if(SaveDialog1->Execute())
( P8 A% k$ b3 Q# Q& t {& o! {1 [% ~) d2 D) t( a9 y2 ]
strcpy(sPath , SaveDialog1->FileName.c_str());% e" C* E9 P# Q0 q& H! [( @0 w
stream=fopen(sPath,"wb");: k, i1 h! p" \9 d
for(int i=0;i<BIN_Byte_Len;i++)
3 E" A3 |, E$ H1 R7 M fprintf(stream,"%c",BIN_Byte); //save file
/ ^) q4 r" Y" c1 a7 g9 e fclose(stream);
9 G; j3 m/ ?: A3 s2 e1 M }
7 B* q5 w( I; z free(BIN_Byte);4 x9 Y2 y/ q$ H9 `4 I& T7 S) m
}
8 C2 y, C2 u F4 C2 G' h! Y//---------------------------------------------------------------------------
, N/ t% P0 ^: Gvoid __fastcall TForm1::FormDestroy(TObject *Sender)* s/ r7 }9 R4 T
{) N9 ]1 Y6 V7 t# j8 r
if (BIN_Byte!=NULL)
1 j1 |) @; _$ G6 I4 w6 d+ V5 M6 h4 r free(BIN_Byte);7 H; E' X: |9 h% F [
}
4 h. ?2 E6 G5 g9 U' v. K( V2 R//---------------------------------------------------------------------------
1 {, [, p) o" y</P> q' s c/ {6 W0 w; ]9 A
< >free(sPath); free(stream);此两句也有问题。</P>4 n2 x Z7 U8 W7 b: i5 y
< >char sPath[MAXPATH];不需要释放;</P>9 V- C+ L* L4 C- @. T2 f% N
< >fclose(stream)已经起到free(stream)的作用</P>( k9 |& i/ j& s3 x& [7 ?. s; r$ Z
[此贴子已经被作者于2004-6-15 15:16:40编辑过] |
|