www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/04/18:54:17

From: sparhawk AT eunet DOT at (Gerhard Gruber)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: how do i define structures ?
Date: Fri, 04 Sep 1998 22:30:57 GMT
Organization: EUnet Austria
Lines: 57
Message-ID: <35fe60e8.49079926@news.Austria.EU.net>
References: <35EFEEF9 DOT F9BF8861 AT gmx DOT net>
NNTP-Posting-Host: e083.dynamic.vienna.at.eu.net
Mime-Version: 1.0
NNTP-Posting-Date: 4 Sep 1998 22:32:46 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Destination: Markus Ewald <Markus_Ewald AT gmx DOT net>
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Fri, 04 Sep 1998 15:45:29 +0200:

>Do I have to use malloc and reserve space for the structure in order to
>use it first ? It worked fine without malloc-ing, but in the worst case, i
>can be happy that it didn't crash because I've overwritten critical memory...

Yes! You have to use malloc(). If you use a local variable then this means
that the space for your structure is reserved on the stack and when the
function returns then this space is freed up. You only have to use malloc if
you are returning the structure. If you pass it to another function then it is
ok but you should still pass the pointer (without the need of malloc'ing)
because of the overhead.

Example:

f1()
{
   mystruct s;

   return(&s);
}


The above is not ok.

f1()
{
   mystruct s;

   f2(&s);

   return(0):
}

f2(mystruct *p)
{
   p->x = 0;
}

this is ok.

--
Bye,
   Gerhard

email: sparhawk AT eunet DOT at
       g DOT gruber AT sis DOT co DOT at

Spelling corrections are appreciated.

- Raw text -


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