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
Solved

Error with pointers, constructor and memory

Posted on 2004-10-16
5
243 Views
Last Modified: 2010-04-05
TCachedData = class(TObject)
  LatticeAddress : ^TLattice;
  MargolusArrayAddressA : ^TMargolusArray;
  MargolusArrayAddressB : ^TMargolusArray;
  fitness : Integer;
  constructor create();
end;

TLattice and TMargolusArray are complex objects.

TCachedDataArray = class(TObject)
  caches : array of TCachedData;
  constructor create(size : Integer);
end;


constructor TCachedData.create();
begin
  self.fitness := 0;
end;

constructor TCachedDataArray.create(size : Integer);
var
  Teller : Integer;
begin
  setlength(caches, size+1);
  for Teller := 0 to size do
  begin
    self.caches[Teller].Create();
  end;
end;

Why does this code give me an error?

LatticeCache : TCachedDataArray;
LatticeCache.create(10);

Delphi gives me an access violation at the 'self.fitness := 0;' in constructor TCachedData.create();
Help much appreciated!
0
Comment
Question by:reynaerde
5 Comments
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 20 total points
ID: 12327292
hint:

constructor TCachedData.create();
begin
  inherited; //add this line
  self.fitness := 0;
end;

meikl ;-)
0
 

Author Comment

by:reynaerde
ID: 12327376
Well, that doesn't seem to change anything.. still get the same error.
Ok, I must have been sleeping: instead of

-self.caches[Teller].Create();

I should have been using:

-self.caches[Teller] := TCachedData.create();

I don't really understand what the difference is between the two, is it possible to have this working using the first way
(i.e. self.caches[Teller].Create();)?
If you can explain it to me I'd be happy to give you the points :)

0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 380 total points
ID: 12327895
The difference is this:

In the first case, you are trying to call an object instance's Create(...) constructor. But.... you don't have an object instance yet. No memory has been set aside yet because the class constructor has not been called, which leads to the access violation.

In the second case, you are calling the class constructor and assigning the created object instance to your object variable.

Just remember that when calling Create(...), you need to call the constructor of the CLASS, and not the OBJECT. And no, it is not possible to make this work by using the first case.

Regards,
Russell


 
0
 

Author Comment

by:reynaerde
ID: 12327910
Thanks :)
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12328018
constructor is a class method you can call it with class name or with a variable of  a class-reference type

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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