- 在线时间
- 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>
6 ^ J9 B& c/ ^7 s< >alloca:</P>8 s0 J) A. }( b4 l! ~
< >Allocates temporary stack space.</P>
2 U2 F2 U! K$ Z8 |* ~) E; ]. H- c< >alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>* t# L' O ?- O9 Z* }
< >malloc:</P>
1 v( |( U; ?" i& m/ \/ K/ `/ b2 v< >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 X1 y! @+ }* `! h5 u* o8 J: 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>4 L, c$ D$ \6 G7 U( ~% ]: ?
+ m" h7 A8 a7 n/ A% s9 _ t
< >以下是改后的代码</P>
& d! B' d# z0 G< >//---------------------------------------------------------------------------</P>
! y* z" x; i; W" ~< >#include <vcl.h>3 C" \4 ~. O6 W; A$ L
#pragma hdrstop</P>- l& X5 M, j, ]' p" {
< >#include "Unit1.h"1 ^! L2 r. D @; _
#include <fstream.h>% P/ w' e' F6 x ]% M( o0 J- o- ~
#include <dir.h>
, b9 B4 p. b( G+ _#include <sys\stat.h>
& z" t; j/ ~# r8 P, a. }#include <malloc.h>
1 F, ]+ N( T2 r8 Y//---------------------------------------------------------------------------4 |) l6 m$ T/ g4 p
#pragma package(smart_init)4 `0 ^; Q% f k; T" M( J* r
#pragma resource "*.dfm"
. F, I g: t) U. L$ v, JTForm1 *Form1;. y3 ]4 E C; Z: ?& g
unsigned char *BIN_Byte;
! x- N" a& t0 |/ K, w2 Z long BIN_Byte_Len;
/ }# [( X# z1 s, s# M+ j! m//---------------------------------------------------------------------------% n* y: P; `' X( I
__fastcall TForm1::TForm1(TComponent* Owner)
8 X3 o9 ]' _2 I3 b! K2 w : TForm(Owner)
. n* u2 y( T7 H' h4 N, r6 x% _{9 c: r) B) I" A4 O+ |
}! C' T, b+ p/ X% W
//---------------------------------------------------------------------------5 `; I2 `' Y9 p5 H* ~$ {
void __fastcall TForm1::Button1Click(TObject *Sender)
- Q; }! S4 P& b8 x2 T1 z3 W2 \, z{8 X8 R8 ]4 S9 g, L6 F
char sPath[MAXPATH];2 n, ]/ {6 l( \
struct stat statbuf;! [% n* U4 ?5 J( h# b
FILE *stream;</P>: Q8 }. z6 H* S
< > getcwd(sPath,sizeof(sPath));6 T# \+ {- {# |4 y" m+ i l* o
OpenDialog1->InitialDir=sPath;" \' h$ Q7 x* v
OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
9 r& Q+ B" g" Z5 f OpenDialog1->FileName="fileFrom.BIN";9 l2 c! c7 H! v4 d. V' n
if(OpenDialog1->Execute())
+ f, x( J% U+ k" k% L/ R {
) |6 z9 p# c- K0 T" N* i. h/ L strcpy(sPath,OpenDialog1->FileName.c_str());) | I) l0 Z# Q9 \5 |6 V8 ^
stream=fopen(sPath,"rb");, m3 W6 \( x& S
fstat(fileno(stream),&statbuf);
" b9 N O5 y% S. U& R BIN_Byte_Len=statbuf.st_size;
: t+ F4 }5 P' W1 z8 S$ P6 O- F BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);
5 E' a2 I8 t: q" T2 m; x$ s for(int i=0;i<BIN_Byte_Len;i++)
% ^- o" u2 g; h' t8 [ fscanf(stream,"%c",&BIN_Byte); //read file
. x- K: u; y4 h8 J* y1 D- R# O! ` fclose(stream);
+ \( {% l4 [+ O u( X* | }0 T4 {( Z* T P& C6 k1 n
}
7 @# i2 K; J/ L//---------------------------------------------------------------------------7 i4 y) x8 x* R: M+ K! k
void __fastcall TForm1::Button2Click(TObject *Sender), R1 _+ Y5 v" _8 O! ?& o1 } s
{
( O: w- \5 o; e2 n# Q char sPath[MAXPATH];
4 O* l, B, C+ {1 Y f* ~# p FILE *stream;/ N% O3 w1 A8 q# n" A
getcwd(sPath,sizeof(sPath));//App.Path
7 g6 X: R* m7 `' @; b$ U strcat(sPath,"\\");, E7 U! y S1 i9 c# f
SaveDialog1->InitialDir = sPath;) X7 v: ^' N6 K. K7 H/ c
SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";9 n( g2 J, W8 K" Q: c2 h3 R2 g
SaveDialog1->FileName="fileTo.BIN";& g9 `# H U! C: t3 C
if(SaveDialog1->Execute())- X8 ~/ F8 G' y, R; l* `9 y/ A
{
, n$ f: b/ U" o; r) U* @8 k L5 u strcpy(sPath , SaveDialog1->FileName.c_str());
& |7 m* G' \- \ stream=fopen(sPath,"wb");/ f p1 O) s5 G+ V
for(int i=0;i<BIN_Byte_Len;i++)( ~/ E( r( I3 N
fprintf(stream,"%c",BIN_Byte); //save file
6 k3 B) H' ?( P9 w. Q$ s! A fclose(stream);
* x2 m7 O$ n6 k$ \7 T }) o1 b: @0 d! x" T9 X
free(BIN_Byte);
- B; H u7 U2 i6 W0 q: m}
( h$ `' P3 `# Z5 ?5 D//---------------------------------------------------------------------------
3 c; S' t7 z m: e5 avoid __fastcall TForm1::FormDestroy(TObject *Sender)
# [4 z& p# Z$ [" m{
% c6 f. q' l6 I( t/ R! a0 U if (BIN_Byte!=NULL)
, l2 T1 y+ H2 Q5 ]7 s4 `; Y free(BIN_Byte);- C8 w& U% [5 a' {: t0 A: Z/ E2 D
}6 O, P4 K7 K% _ `
//---------------------------------------------------------------------------) r! K2 g) P, f
</P>
- N: R y6 y+ v6 X! Z< >free(sPath); free(stream);此两句也有问题。</P>
% k# m4 K) A( E# b1 S4 t& I< >char sPath[MAXPATH];不需要释放;</P>: B7 k; Z4 A" e+ @, f) s
< >fclose(stream)已经起到free(stream)的作用</P>, c1 _4 Z% _7 M3 p0 q7 g
[此贴子已经被作者于2004-6-15 15:16:40编辑过] |
|