What is the Problem in this code?

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
LVL 14
sudhakar_koundinyaAsked:
Who is Participating?
 
robert_marquardtConnect With a Mentor Commented:
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
 
robert_marquardtCommented:
inc(dest,count+1);
dest^:=chr(0);

Oops, i have to rethink that.
0
 
kretzschmarCommented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
sudhakar_koundinyaAuthor Commented:
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
 
sudhakar_koundinyaAuthor Commented:
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
 
robert_marquardtCommented:
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
 
leusCommented:

Why don't just use Copy()?

0
 
sudhakar_koundinyaAuthor Commented:
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
 
leusCommented:

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
 
sudhakar_koundinyaAuthor Commented:
Thanks Robert.
You thought in right way

Koundinya

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.