Solved

Pointer typecast

Posted on 2002-03-05
7
496 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6842104
Why don't you use SetLength?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6846759
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
ID: 6846761
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bnz
ID: 6846794
>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
ID: 6846815
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
ID: 6846906
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
ID: 6846934
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

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.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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