Solved

Dynamic Pointers Doubt (I am C++ Programer)

Posted on 2002-04-12
10
176 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
 
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
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.

 
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

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

17 Experts available now in Live!

Get 1:1 Help Now