Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pointer typecast

Posted on 2002-03-05
7
Medium Priority
?
500 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
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
Independent Software Vendors: 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 400 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Loops Section Overview
Suggested Courses

926 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