[LC++]question about far *t

Erik Mouw J.A.K.Mouw at ITS.TUDelft.NL
Wed Jul 25 07:39:13 UTC 2001


On Tue, Jul 24, 2001 at 12:42:31AM +0430, gosh larous wrote:
> Hi. I have written a function in C under borland C++.  I define a function
> as: double far *ghd(.....) and in the body of the function I returned a
> pointer:
> 
> double  *ghd(....)
> {
> double str[50];
> ........
> ........
> return(str);
> }
> 
> but it returns me only str[50]. I want it to return me all the array
> memebers. from 0 to 50. Can you help me in this regards.

Two things:

- It does return all array members, but you only get a pointer to the
  first one and have to do the indexing yourself.
- This code is unsafe. str is allocated on the stack, which will be
  cleaned up as soon as ghd() returns. ghd() returns a pointer into an
  invalid memory area to its caller. If the caller calls a couple of
  other functions you *will* get corruption of the str area.

Oh, and forget about the "far" stuff, linux uses a flat 32 bit memory
model so it doesn't need "near" or "far" pointers.


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2783635  Fax: +31-15-2781843  Email: J.A.K.Mouw at its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/



More information about the tuxCPProgramming mailing list