Solved

Pointer typecast

Posted on 2002-03-05
7
492 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON on ISAPI REST Server using Delphi 2 229
TVirtualStringTree  search using TEdit 7 106
how to center only a line in richedit? 4 52
creating threads in delphi 1 82
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

24 Experts available now in Live!

Get 1:1 Help Now