delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/08/11/09:15:25

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: ...but
Date: 11 Aug 2000 13:07:36 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 51
Message-ID: <8n0tqo$ds7$1@nets3.rz.RWTH-Aachen.DE>
References: <SEik5.4799$aK5 DOT 80126 AT news1 DOT online DOT no> <8mueij$hd7$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE> <VhEk5.5698$aK5 DOT 95670 AT news1 DOT online DOT no>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 965999256 14215 137.226.32.75 (11 Aug 2000 13:07:36 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 11 Aug 2000 13:07:36 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Vermin <ratspl AT hotmail DOT com> wrote:
> but is it possible to have an array (or a pointer to an array) which doesn't
> have a a given size (or doesn't really exist) until you define it by making
> space for it in the mem...

That's exactly what pointers and 'new' (or malloc(), if you're writing
C instead of C++) are for.

If you want to do that in a subroutine, it needs that pointer passed
in by reference, so the pointer itself can be modified. I'm not
footsure with the C++ 'reference argument' thingy, so I'll explain how
it'd be done in C. There, pass-by-reference is implemented via another
level of pointer:

> some pseudo code:

> procedure proc1(pointer pp){
>   pp (points to) new array[arraySize];
> }

This would be

   void proc1(type **pp) {
	size_t arraySize;

	/* calculate arraySize, somehow */
	*pp = malloc(arraySize * sizeof(*pp))

	// C++-style would look like this:
	// *pp = new type[arraySize];
   }

> main(){
>   pointer p1;
>   call proc1(p1);
> }

and this:

    int main(void) {
	type *p1 = NULL;

	proc1(&p1);
    }

An alternative approach would be to have proc1() return a 'type *' as
its result. But that only works for *one* result per function, as
usual.
-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019