🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #49803

Re: Is this correct?

#49803
From: Fergus Henderson
Date: Sun, 12 Dec 1993 08:08
38 lines
1889 bytes
lele@itnsg1.cineca.it (Lele Gaifax) writes:

>struct point { int x; int y; };
>
>/* This macro should return a pointer to an initialized
> * struct point. I use it to generate inline points to be passed
> * as argument. If some space gets alloced, I would like it freed
> * automatically... */
>
>#define POINT(_x,_y) ({struct point _p = {_x,_y}; &_p;})
>
>Ok. point.c defines other functions that accept a pointer to a
>struct point as the first argument. The question is about the macro POINT():
>is it correct to use it like
>	...
>	move_to_point (POINT(45,34));
>	...
>??. Please note that *it does work*, but I guess it is not a correct use
>of this GCC feature.

You are right - it is not correct to use it like that.
It might work most of the time, and yet mysteriously fail
at random intervals.

>I tried to use alloca to setup such a macro:
>
>#define POINT(_x,_y) ({struct point * _pp = alloca(sizeof (struct point)); \
>		       *_pp = (struct point){_x,_y}; \
>		       _pp;})
>
>Alloca gets some memory from the stack, and frees it when the function exits...
>or when the block ends?

When the function exits.
Using alloca() should work just fine.

-- 
Fergus Henderson                     fjh@munta.cs.mu.OZ.AU
Technical Details
Message-ID: <9334619.12910@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!cs.utexas.edu!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
References: <lele.755522550@itnsg1>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,877e47fd4e8cdb51
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1993-12-12 00:13:40 PST
Newsgroups: gnu.gcc.help
Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!cs.utexas.edu!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Subject: Re: Is this correct?
Message-ID: <9334619.12910@mulga.cs.mu.OZ.AU>
Sender: news@cs.mu.OZ.AU
Organization: Computer Science, University of Melbourne, Australia
References: <lele.755522550@itnsg1>
Date: Sun, 12 Dec 1993 08:08:56 GMT
Lines: 38