Solved

Dynamic Pointers Doubt (I am C++ Programer)

Posted on 2002-04-12
10
179 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 50 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 33

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 117
Connection between libmysql.dll and MySQL Versions 7 99
Delphi XE10 Round Image 2 129
SUM 2 INTEGER ARRAYS INTO 1 10 101
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
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.

831 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