Pointers HELP PLEASE! 
Author Message
 Pointers HELP PLEASE!

My name is Luke Shingles
I am 11 years old and programming in TP 3.0
I am getting TP 7.0 soon.

Here is my question:
When I run this program it says "four"
how do you access the "one","two" and "three"?

      Here is my source code:

type
   TPointer = string[20];

var
txt:       ^TPointer;
heaptop:   ^integer;

begin
mark(heaptop);
txt := nil;
txt^ := 'one';
txt^ := 'two';
txt^ := 'three';
txt^ := 'four';
writeln(txt^);
release(heaptop);
end.

Luke Shingles



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers HELP PLEASE!

Hi, Donald.

The problem is that each execution of "txt^ :=  ???" overwrites the
previous value of txt^.  If you want to see each of the four values, try
the following.

program Sample.

type
   TPointer = string[20];

var
  txt:       ^TPointer;
  heaptop:   ^integer;

begin
  mark(heaptop);
  txt := nil;
  txt^ := 'one';
  writeln(txt^);
  txt^ := 'two';
  writeln(txt^);
  txt^ := 'three';
  writeln(txt^);
  txt^ := 'four';
  writeln(txt^);
  release(heaptop);
end.



Quote:
> My name is Luke Shingles
> I am 11 years old and programming in TP 3.0
> I am getting TP 7.0 soon.

> Here is my question:
> When I run this program it says "four"
> how do you access the "one","two" and "three"?

>       Here is my source code:

> type
>    TPointer = string[20];

> var
> txt:       ^TPointer;
> heaptop:   ^integer;

> begin
> mark(heaptop);
> txt := nil;
> txt^ := 'one';
> txt^ := 'two';
> txt^ := 'three';
> txt^ := 'four';
> writeln(txt^);
> release(heaptop);
> end.

> Luke Shingles




Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers HELP PLEASE!

Quote:

> Hi, Donald.

> The problem is that each execution of "txt^ :=  ???" overwrites the
> previous value of txt^.  If you want to see each of the four values, try
> the following.

> program Sample.

> type
>    TPointer = string[20];

> var
>   txt:       ^TPointer;
>   heaptop:   ^integer;

> begin
>   mark(heaptop);
>   txt := nil;
>   txt^ := 'one';
>   writeln(txt^);
>   txt^ := 'two';
>   writeln(txt^);
>   txt^ := 'three';
>   writeln(txt^);
>   txt^ := 'four';
>   writeln(txt^);
>   release(heaptop);
> end.



> > My name is Luke Shingles
> > I am 11 years old and programming in TP 3.0
> > I am getting TP 7.0 soon.

> > Here is my question:
> > When I run this program it says "four"
> > how do you access the "one","two" and "three"?

> >       Here is my source code:

> > type
> >    TPointer = string[20];

> > var
> > txt:       ^TPointer;
> > heaptop:   ^integer;

> > begin
> > mark(heaptop);
> > txt := nil;
> > txt^ := 'one';
> > txt^ := 'two';
> > txt^ := 'three';
> > txt^ := 'four';
> > writeln(txt^);
> > release(heaptop);
> > end.

> > Luke Shingles


Wow...

Kurt..

It's obvious from your post that you understand the general concept of
pointers.

But didn't the txt := nil kind of bother you?

txt^ points to 0000:0000.

In real mode, assigning a value to txt^ is probably going affect system
stability.

In Protected mode, you will get a GPF. 0000 isn't a good selector to
read/write from.

Place a getmem(txt,sizeof(TPointer)); after or instead of txt := nil;
and the second version should be fine.

Note: programming style.

Quote:
> type
>    TPointer = string[20];

> var
>   txt:       ^TPointer;
>   heaptop:   ^integer;

TPointer is a string, not a pointer.  Why call it a pointer?  It just
adds to the confusion.

txt is a pointer that points to a type TPointer.

In fact txt is a pointer that points to a string[20].

This is just programming style but it clarifies things a little.

type
  str20 = string[20];

  pstr20 = ^str20;

var
  txt : pstr20;

This allows you to pass txt as a parameter properly (i.e. type checking)
to a function/procedure

you can't do this.

procedure Dosomething(temptxt : ^TPointer);

But you can do this.

procedure Dosomthing(temptxt : pstr20);

This doesn't gaurantee that temptxt will have a valid value, but atleast
without typecasting you know that temptxt is pointing to a TPointer type
variable.

If someone is abusing typecasting, then it's up to them to make sure
things are proper.

In my opinion type checking is a strength of Pascal and a weakness of C.

In Pascal there is a way to effective turn off type checking, I don't
think there is a way to effectively turn on type checking in C.

mykey



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers HELP PLEASE!


Quote:

>Hi, Donald.

>The problem is that each execution of "txt^ :=  ???" overwrites the
>previous value of txt^.  If you want to see each of the four values, try
>the following.

>program Sample.

>type
>   TPointer = string[20];

>var
>  txt:       ^TPointer;
>  heaptop:   ^integer;

>begin
>  mark(heaptop);
>  txt := nil;
>  txt^ := 'one';

Come on, you are assigning to the target of a nil pointer and thus
overwriting the interrupt table. One has to allocate the target variable
with

new(txt);

In general pointers always have to be allocated or assigned from a
previously allocated pointer before the target variable can be used.

Quote:
>  writeln(txt^);
>  txt^ := 'two';
>  writeln(txt^);
>  txt^ := 'three';
>  writeln(txt^);
>  txt^ := 'four';
>  writeln(txt^);
>  release(heaptop);

Also the mark/release sequence is unnecessary. When the program exits
all memory allocations are released. Mark/release is very seldom needed.
When one needs to free memory it is better to use dispose. On version
3.0 one should never mix the two methods of releasing memory. Even on
latter ones mixing them can cause problems. So forget mark/release.

Quote:
>end.

Osmo


Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers HELP PLEASE!

Il giorno Sun, 21 Sep 1997 20:52:49 +1000,

sub-cosciente ha scritto :

Quote:
>My name is Luke Shingles
>I am 11 years old and programming in TP 3.0
>I am getting TP 7.0 soon.

Hello Luke !!!!!!!!!!!!

Quote:
>Here is my question:
>When I run this program it says "four"
>how do you access the "one","two" and "three"?

>type
>   TPointer = string[20];

>var
>txt:       ^TPointer;
>heaptop:   ^integer;

>begin
>mark(heaptop);
>txt := nil;
>txt^ := 'one';
>txt^ := 'two';
>txt^ := 'three';
>txt^ := 'four';
>writeln(txt^);
>release(heaptop);
>end.

I think that, unless incrementing the pointer, the txt is rewritten
every time so older informations are lost.

But why do you use Mark and Realease ?

Andrea Laforgia
--------------------------------------------------------
Un di' qualcuno disse : "Chi beve birra campa cent'anni"
E Matusalemme rispose : "E che e' veleno ?"
--------------------------------------------------------
Tutto cio' che ho scritto sul rigo seguente e' falso
Tutto cio' che ho scritto sul rigo precedente e' falso
--------------------------------------------------------
Ricordate sempre :
"Grattatio pallarum omnia mala fugit" (Andrea Laforgia)
--------------------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers HELP PLEASE!



Quote:

>Il giorno Sun, 21 Sep 1997 20:52:49 +1000,

profondo del suo
>sub-cosciente ha scritto :

>>My name is Luke Shingles
>>I am 11 years old and programming in TP 3.0
>>I am getting TP 7.0 soon.

>>begin
>>mark(heaptop);
>>txt := nil;
>>txt^ := 'one';
>>txt^ := 'two';
>>txt^ := 'three';
>>txt^ := 'four';
>>writeln(txt^);
>>release(heaptop);
>>end.

>I think that, unless incrementing the pointer, the txt
is rewritten
>every time so older informations are lost.

>But why do you use Mark and Realease ?

Well, because a C programmer has told him, I guess. In
fact, this is a bad example for using pointers in
Pascal.

Martin

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

(Replace underscores by dots!)
------------------------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers HELP PLEASE!


Ronkanen) dal profondo del suo sub-cosciente ha scritto :

Quote:
>Come on, you are assigning to the target of a nil pointer and thus

Yes it's true ! not noticed !

Andrea Laforgia
--------------------------------------------------------
Tutto cio' che ho scritto sul rigo seguente e' falso
Tutto cio' che ho scritto sul rigo precedente e' falso
--------------------------------------------------------
"Grattatio pallarum omnia mala fugit" (Andrea Laforgia)
--------------------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers HELP PLEASE!

Il giorno Wed, 24 Sep 97 11:14:32 GMT,

del suo sub-cosciente ha scritto :

Quote:
>>But why do you use Mark and Realease ?
> this is a bad example for using pointers in
>Pascal.

Yes i'm agree.
In my opinion working with Mark and Release will never teach you
anyhing useful about pointers.
What you should use is New, GetMem and Dispose.


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. FP106 pointer help please

2. pointers, pointers, pointers...

3. Pointers Pointers Pointers

4. need pointer to information please

5. Pointers to pointers

6. Beginner needs some pointers with pointers!

7. POINTERS POINTERS...

8. Please Please, help me with memo-fields, real problem

9. Please, please help me with this code.

10. please, I beg of you help me please.

11. PLEASE PLEASE HELP ME

12. Please Please help me

 

 
Powered by phpBB® Forum Software