Solved

Dynamic Pointers Doubt (I am C++ Programer)

Posted on 2002-04-12
10
173 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
Comment Utility
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
Comment Utility
reserved mem 4 dynamic arrays are automaticly freed!
0
 
LVL 7

Accepted Solution

by:
God_Ares earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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
Comment Utility
Look for 'Overview of pointers' in delphi help.
0
 
LVL 7

Expert Comment

by:God_Ares
Comment Utility
sudhakar_koundinya

problem solved?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
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
Comment Utility
k np :) thx 4 pnts
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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