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

x
?
Solved

Dynamic Pointers Doubt (I am C++ Programer)

Posted on 2002-04-12
10
Medium Priority
?
188 Views
Last Modified: 2010-04-04
Hi,

Basically I am C++ Programmer and learning Delphi for my company's project. The Problem is some thing like this

it is easy to create dynamic aarrays and pointers in c++ or c for me. And i am facing problem in Pascal

for e.g

char* x[10];

for(i=0;i<100;i+=1)
{
  x[i]=(char*) malloc(sizeof(char));
  x[i][0]='a';
}

 

and i am not able to do the same in pascal

first i tried like this

x: array[1..10]of PChar;

for i= 1 to 100 do
begin
getmem(x[i],sizeof(char));
x[i][0]='a';
end;


i don't know whether this idea is right or not, but i am facing problems

and other idea what i used is
type

aa: array[1..10] of char;
var
x: ^aa;

but in this case i don't know how to allocate dynamic memory.

Please help me, by giving good answer.


Thanks in Advance
Koundinya

P.S: Please provide me links where i can learn Pointers concepts in Delphi/Pascal

 
0
Comment
Question by:sudhakar_koundinya
10 Comments
 
LVL 7

Expert Comment

by:God_Ares
ID: 6936531
dynamic array's is quite simple

var
  MyStingArray : array of String; //case IN-sensetive!

begin

  SetLength(MyStingArray,500); //500 strings ready no  getmem required.
  MyStingArray[0] = 'Jack';

end;
0
 
LVL 7

Expert Comment

by:God_Ares
ID: 6936535
reserved mem 4 dynamic arrays are automaticly freed!
0
 
LVL 7

Accepted Solution

by:
God_Ares earned 200 total points
ID: 6936537
btw if you do

var ObjectArray : Array of TanykindofObject;

begin
  Setlength(ObjectArray,1);//space 4 1 object
  ObjectArray[0] := TanykindofObject.create;//if no params then no () .
 

  .. code ..
  ObjectArray[0].free;

  This is still required..
end;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Expert Comment

by:AvonWyss
ID: 6937015
God_ares is right about the usage of dynamic array. As a general rule, in Delphi, you should virtually NEVER need to directly use GetMem, AllocMem, FreeMem, etc. in normal applications. There are strings, dynamic arrays, classes, interfaces, and many more whch are managed by Delphi for you.
0
 
LVL 34

Expert Comment

by:Slick812
ID: 6938237
Pointers, variable as Pointers and Dymanic array setup are different in C and pascal, but this you already know (ha, ha)

char* x[10];

for(i=0;i<100;i+=1)
{
 x[i]=(char*) malloc(sizeof(char));
 x[i][0]="a";
}


x: array[1..10]of PChar; {this is not a dymanic array it ais a static array with 10 members of PChar. Also this is a single array}

for i= 1 to 100 do
begin
getmem(x[i],sizeof(char));
{since this is a static array getmem is not needed}
x[i][0]='a';
{you can't get a second array value from a single (one-dimensional) array}
end;

aa: array[1..10] of char; {another single static array}
var
x: ^aa; {don't know what you are trying to do here}

- - - - - - - - - - - - - - - - - - - - - - - - -

maybe your C code would translate to Pascal like this

X: Array of Array[0..9] of Char;
{X is a dymanic multidimensional array}


for i := 0 to 99 do
begin
 SetLength(X, i+1);
 X[i,0]:='a'; {instead of x[i][0] = "a"}
end;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6939933
Ehm, I'm sorry, I'm not really a C++ expert, but your code looks wrong to me:

char* x[10];

for(i=0;i<100;i+=1)
{
 x[i]=(char*) malloc(sizeof(char));
 x[i][0]='a';
}

You're telling C++, that your array can hold 10 strings. Then you fill in 100 strings? There's something wrong!

I agree with God_ares, Delphi offers dynamic arrays, which are much easier to use than this pchar stuff.

Regards, Madshi.
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6943596
Look for 'Overview of pointers' in delphi help.
0
 
LVL 7

Expert Comment

by:God_Ares
ID: 6957588
sudhakar_koundinya

problem solved?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 6959499
Hi

Sorry for the late response
as i was busy with some other project

Thanks for the good answer


arrays helped me a lot


Sudhakar
0
 
LVL 7

Expert Comment

by:God_Ares
ID: 6959522
k np :) thx 4 pnts
0

Featured Post

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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

876 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