Link to home
Start Free TrialLog in
Avatar of sudhakar_koundinya
sudhakar_koundinya

asked on

Dynamic Pointers Doubt (I am C++ Programer)

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

 
Avatar of God_Ares
God_Ares

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;
reserved mem 4 dynamic arrays are automaticly freed!
ASKER CERTIFIED SOLUTION
Avatar of God_Ares
God_Ares

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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;
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.
Look for 'Overview of pointers' in delphi help.
sudhakar_koundinya

problem solved?
Avatar of sudhakar_koundinya

ASKER

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
k np :) thx 4 pnts