Solved

Pointer typecast

Posted on 2002-03-05
7
491 Views
Last Modified: 2010-04-05
Can anybody tell me why the pointer typecast below is necessary.
AllocMem returns a pointer so the typecast is pointless the way I see it, but if it's omitted it crashes.

type
  TMyRec = record
    ip: string[15];
    port: integer;
  end;
var
  MyAry: array of TMyRec;
begin
  //         ||
  //         ||
  //         \/    
  MyAry := Pointer( AllocMem(SizeOf(TMyRec)*2) );
  MyAry[0].ip := '0.0.0.0';
  MyAry[1].ip := '255.255.255.255';
  showmessage(MyAry[0].ip);
  showmessage(MyAry[1].ip);
end;
0
Comment
Question by:bnz
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
Why don't you use SetLength?
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
Uah! The pointer typecast is not necessary, in fact it's plain and simply *wrong*!!!

You can't assign an allocated memory block to a dynamic array. Instead you HAVE to use SetLength (as Robert already said).

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
The reason is that dynamic arrays are more than just a simply memory buffer. They have a reference count and such stuff. Just like dynamic strings. You wouldn't do the following, either, or would you?

var str : string;
begin
  str := pointer(AllocMem(100));   // crash code
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:bnz
Comment Utility
>You wouldn't do the following, either, or would you?
>
>var str : string;
>begin
> str := pointer(AllocMem(100));   // crash code

Heh, I think I would ;)


isn't a string just a simple nullterminated pointer ?

var
    s: string;
begin
    s := 'delphi';
showmessage(s);
end;

if I open the cpu window and look at what it points to it is delphi#0


If the array is wrong, why does it then works ?
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
Comment Utility
A Delphi dynamic string looks like this:

[length][allocSize][referenceCount][stringData]#0

The string variable in fact points to string data, but nevertheless there are 12 bytes more allocated for each dynamic string, which are stored in front of the string data. It's quite similar with dynamic arrays. So please use SetLength.

>> If the array is wrong, why does it then works ?

Probably it works as long as Delphi doesn't try to do special things with the array like resizing, freeing or changing the reference count. But it's a perfect situation for wild crashes. Please don't do it...   :-)

Regards, Madshi.
0
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
To add to Madshi: A string in Delphi is much more a class in the C++ sense than anything else.
Declaring a string variable implies calling the constructor.
For a Delphi string this is simply assigning nil. nil serves as '' string (empty string).
Any string variable is placed inside a try finally block.
In the finally part the reference counter is decremented and the memory is freed if the counter reaches zero.
0
 

Author Comment

by:bnz
Comment Utility
Thank you both

I have put a follow up question here

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20274342
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now