Solved

What is the Problem in this code?

Posted on 2002-05-28
10
138 Views
Last Modified: 2010-04-04
Hi guys,

I am not understanding where i am doing mistake in my code. I will give code snippet and try to explain what i am doing here. And Please tell me what is the mistake in this code.
 procedure CopyChars(src:PChar;var dest:Pchar;pos:Integer;count:Integer);
procedure Parse( strInputBuffer:PChar; streamLength:LongInt; var strOutputBuffer:PChar; var errorCode:Integer);


procedure CopyChars(src:PChar;var dest:Pchar;pos:Integer;count:Integer);
var
local:PChar;
local1:Pchar;
begin
     try
     local:=src;

     if dest = nil then
     begin
          GetMem(dest,count+1);
     end
     else
     begin
           ReAllocMem(dest,count+1);
     end;

     local1:=dest;
     try
         Move(src[pos],dest^,count);
     except
     on error : exception do
     begin
     showmessage(error.message);
//        showmessage('error raised here');
     end;
     end;
     inc(dest,count+1);
     dest^:=chr(0);

     dest:=local1;
     src:=local;


    except
       on error: exception do showmessage('Error in copy chars : '+error.message);
    end;


end;

in procedue Parse()

while true do
begin
 try
//begin of code
// some code
                                     CopyChars(strInputBuffer,strFilter, index,  counter-index);
                                       strFilterCode := strFilter;
                                       except

                                       end;

// and other code
end;

Case 1
when my parse procedure calls the copychars for the first time, code is working fine. But when it calls for second time. Copychars procedure reallocates the memory for destination pointer and try to copy the data from the source to destination pointer, It is giving Illegal Pointer access exception. I am not understanding, why it is happening like that
I tried to allocate or reallocate the memory external to copychars procedure also. But same exception was raised by the program.


Case 2.

As above is giving problems

i Used GetMem and FreeMem procedures also. When I do so for the first time itself it is giving problem

Case 3
I used SysAllocMem,SysGetMem,SysRealloc,SysFreeMem procedues also. But no use.


all above cases itried with in procedure and external to procedure
by creating variables as class members ./ by creating procedure variables
 
***************************************************************************************************************
I am not understanding  where i am doing problem. Please help me. If u identify the mistake in my code
***************************************************************************************************************
Thanks
Koundinya
0
Comment
Question by:sudhakar_koundinya
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
ID: 7038608
inc(dest,count+1);
dest^:=chr(0);

This is 1 Byte beyond end of buffer.

Is is possible that it crashes because the source string is too short? You are copying count bytes which may be beyond buffer end of source.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7038609
inc(dest,count+1);
dest^:=chr(0);

Oops, i have to rethink that.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7038675
hmmm,

robert i think you're on the right track,
as the pointer is moved by this increment
and is defined as var parameter which affect the var of the caller, so that it could be,
that the next call violates

just a guess

meikl ;-)
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 7038743
Thanks Robert and meikel fro ur quick response




inc(dest,count+1);
dest^:=chr(0); // i want to treat this as end of string


Move(src[pos],dest^,count);

Just tell me in this case it copies count no of bytes or count+1 bytes

if it is count + 1 bytes as Robert expected i may be  going wrong.

Thanks again and waiting for ur quick response

Sudhakar Koundinya
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 7038753
and i want to know which is faster


Move(src[pos],dest^,count);

         or

dest^:src[pos];
inc(dest,count);
dest^:=chr(0)// end of string

If i do like this will it effect the source stream ?


Thanks
Koundinya
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7039511
The information we need are the parameter values of the first and second call.
I suspect that you call it
Pos=5, Count=30
Pos=10, Count=30
That is count does not decrease whereas Pos increases.
If so you get the overflow on the source string when you copy bytes outside the source string.
0
 

Expert Comment

by:leus
ID: 7042830

Why don't just use Copy()?

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 7043755
Hi leus

I can use Copy


But Before using that, i want to know wheteher the copy function treats null (Ascii 0) charachter as termination point of string or not.(It is string handling routine) If it treats like that there is no use for me. Because my program totally depends on binary streams

In that case I have to depend either of following procedures

Move(src[pos],dest^,count);

        or

dest^:src[pos];
inc(dest,count);
dest^:=chr(0)// end of string

But I don't Know Which will work fast

Thanks
Koundinya
0
 

Expert Comment

by:leus
ID: 7044574

I always prefer to use built in functions instead of provide replacements, because I'll never know if I'm doing something wrong with certain things (like different operating systems, etc.) So my bet is for Move() instead of your second solution.

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 7046227
Thanks Robert.
You thought in right way

Koundinya

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question