- 在线时间
- 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>! q0 A6 h4 o6 f1 \' M C8 V
< >alloca:</P>
9 A2 ~4 g5 {: ?# y$ M) e& I4 g< >Allocates temporary stack space.</P>
/ b, t8 w- r2 ?) U" t1 @2 o< >alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>8 m$ V8 l, k1 ?3 O# d1 t
< >malloc:</P>
4 C8 H- S* Y: J; Z# u: ]< >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>4 ~" Y. X! l8 N2 H# K+ I* r, o
< >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>
/ D2 Y: n* h% j, P; q, X3 D! t; Q- ~9 z \! o
< >以下是改后的代码</P>9 S" V7 k7 e, ^2 `/ |
< >//---------------------------------------------------------------------------</P>
0 {- M% |+ \) h1 {# S< >#include <vcl.h>
; C+ v4 P* `9 n2 T0 e5 K#pragma hdrstop</P>! ?2 o' n% v$ m
< >#include "Unit1.h"
0 l- ]+ {2 `% v" P+ Z#include <fstream.h>
9 n& G. {. g0 i- |8 b#include <dir.h>
$ N+ B: B' T+ n#include <sys\stat.h>
9 H4 v# c: M, C; {2 \, W+ y3 O#include <malloc.h>& B( Z! v% Z/ h& P/ g! I0 D
//---------------------------------------------------------------------------
+ y0 N5 I2 m( t) U! P#pragma package(smart_init)
9 y5 i. v* a1 w7 W* c2 b6 F#pragma resource "*.dfm"
: n& q# B, h5 k- N& W9 m2 Z" lTForm1 *Form1;9 _( i$ s! O b+ u# C5 ^
unsigned char *BIN_Byte;
0 b0 v |5 a) z long BIN_Byte_Len;0 w9 L) y1 g7 G, S# u
//---------------------------------------------------------------------------+ F0 p9 P. g1 b1 u# x: k
__fastcall TForm1::TForm1(TComponent* Owner)
) P1 `) B. Z0 p& j0 \ : TForm(Owner)8 y2 C4 Z) H. g7 O4 ~
{
& ]) P; X5 E3 b}- `( M. c+ b, Z# R$ S- R6 x- d% I
//---------------------------------------------------------------------------
8 N( G$ k; ?; O% {4 g# \3 Dvoid __fastcall TForm1::Button1Click(TObject *Sender) A5 f% e! g7 i5 j! S
{% `4 b) L0 s8 u: Z
char sPath[MAXPATH];6 v0 g' P8 y* W1 D* E7 y
struct stat statbuf;
; e ?& S6 h3 {$ B& ~1 h" r FILE *stream;</P>2 b2 [3 _/ R# c9 L! \
< > getcwd(sPath,sizeof(sPath));
+ x& m* n4 }% \3 g- P& y OpenDialog1->InitialDir=sPath;
( f8 d; \, ^+ W6 k# l OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";) B4 [- [! A( v6 e4 F- R, ^
OpenDialog1->FileName="fileFrom.BIN";! P; p# m8 @ {) n8 ~. D% r
if(OpenDialog1->Execute())( ]" e: i( s* d
{( V7 \2 |) @+ X1 |$ n
strcpy(sPath,OpenDialog1->FileName.c_str());
$ G) r; r4 N2 h* B# o/ L) e stream=fopen(sPath,"rb");
$ Z# o7 ~* f. P- B9 w6 C fstat(fileno(stream),&statbuf);
" {0 J: ?& G0 X& `8 m' V BIN_Byte_Len=statbuf.st_size;2 L1 o3 |& h5 t: `0 t" I8 B% A
BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);
l$ H# K& e3 T for(int i=0;i<BIN_Byte_Len;i++)
5 g$ _9 L- \6 F( P8 S6 [8 r fscanf(stream,"%c",&BIN_Byte); //read file- r. M9 m! w5 B9 } B+ Y3 l
fclose(stream);( h$ h0 i' z5 ~* R r ?4 Q
}; w# z7 o8 _% c3 r
}' O6 @6 s$ Z$ O, I \
//---------------------------------------------------------------------------: ^8 \3 a( {3 H; T; e; q
void __fastcall TForm1::Button2Click(TObject *Sender)
4 V7 |. ?6 p: U' d! Z4 I1 u{
R- L8 q6 i7 a char sPath[MAXPATH];* l- R( ?: k$ _9 w, {
FILE *stream;
3 y+ P' \ X, u2 Z" p getcwd(sPath,sizeof(sPath));//App.Path
1 g/ H l+ d# r strcat(sPath,"\\");
0 C( O" J1 J- W! i SaveDialog1->InitialDir = sPath;
, }. f$ `8 m# B SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";& C6 @! {; c; Z7 O# p$ ~
SaveDialog1->FileName="fileTo.BIN";
+ `7 E( s: S! f* ?( o& S. g6 J if(SaveDialog1->Execute())
6 h, d- O- X. \( F {! R8 r( E5 S" d4 P& k: I- p, Z
strcpy(sPath , SaveDialog1->FileName.c_str());5 X+ I6 z9 s6 a3 C4 D- }$ N8 P
stream=fopen(sPath,"wb");
* ]* O/ ^ u) K" [ c- r for(int i=0;i<BIN_Byte_Len;i++)2 J) x8 ?7 z6 c; x8 k( M t
fprintf(stream,"%c",BIN_Byte); //save file
5 m9 {0 c4 D/ q& e! b4 P fclose(stream);
) H' X7 p0 {0 T- q }
5 T9 c" ~' i! P+ S. n free(BIN_Byte);# P6 s! X+ h. Z
}
) I1 f+ F! u! h9 u8 W' D; G, O; P8 z//---------------------------------------------------------------------------
: J8 K: u2 A9 v. A9 q8 r tvoid __fastcall TForm1::FormDestroy(TObject *Sender)$ S. A, m/ C# I0 E0 K
{
; w6 `, f D; t" a5 X" F2 T9 \ if (BIN_Byte!=NULL): A, n5 J2 c0 w1 o+ }% ]
free(BIN_Byte);
. @* U9 m; S \0 D5 i2 c4 \! r}: w/ g( F' P7 \2 A b% {0 Q- _
//---------------------------------------------------------------------------
! x" A+ i6 g4 Z# v1 a. R# n7 L6 J! B7 u</P>3 M* i& B( }; u! x8 f8 ?1 U
< >free(sPath); free(stream);此两句也有问题。</P>
8 e& u0 M$ s6 i2 ^0 T< >char sPath[MAXPATH];不需要释放;</P>
* V w- _5 [) ~% y& q/ w< >fclose(stream)已经起到free(stream)的作用</P>/ i2 H* S) K; X1 K) M
[此贴子已经被作者于2004-6-15 15:16:40编辑过] |
|