2 May 2007 09:52
Re: [Monetdb-pf-checkins] pathfinder/compiler/mil milprint_summer.c, 1.367, 1.368
I have great compilation troubles with the last lesson from the master(Continue reading)The compiler doesn not know the realloc() and the malloc() function() in the current setting. When I change them into PFrealloc() and PFmalloc() it compiles. But then I still have problems with the "free(buf);" line. There is no PFfree(); and I think GDKfree() will not always work, JanF. On Tuesday 01 May 2007 17:58, Sjoerd Mullender wrote: > Update of /cvsroot/monetdb/pathfinder/compiler/mil > In directory sc8-pr-cvs16:/tmp/cvs-serv17830 > > Modified Files: > milprint_summer.c > Log Message: > When you want to use a library function, make sure the appropriate > include file is included. For alloca there is an extra twist: for the > way to include alloca.h see e.g. monet_utils.mx. Do not hide the lack > of a declaration with a cast. The compiler then thinks the function > returns an int which may well be smaller in space than the actual > value, and hence some bytes of the value may get lost. > > Having said this, alloca should *not* be used in a loop. It allocates > memory in each iteration which is only freed at the end of the > function. If the loop loops many times, that can be a lot of memory > which is allocated on the stack (which is a very finite resource!). > > Here ends today's lesson. > >
The
compiler doesn not know the realloc() and the malloc() function() in the
current setting. When I change them into PFrealloc() and PFmalloc() it
compiles. But then I still have problems with the "free(buf);" line. There is
no PFfree(); and I think GDKfree() will not always work,
JanF.
On Tuesday 01 May 2007 17:58, Sjoerd Mullender wrote:
> Update of /cvsroot/monetdb/pathfinder/compiler/mil
> In directory sc8-pr-cvs16:/tmp/cvs-serv17830
>
> Modified Files:
> milprint_summer.c
> Log Message:
> When you want to use a library function, make sure the appropriate
> include file is included. For alloca there is an extra twist: for the
> way to include alloca.h see e.g. monet_utils.mx. Do not hide the lack
> of a declaration with a cast. The compiler then thinks the function
> returns an int which may well be smaller in space than the actual
> value, and hence some bytes of the value may get lost.
>
> Having said this, alloca should *not* be used in a loop. It allocates
> memory in each iteration which is only freed at the end of the
> function. If the loop loops many times, that can be a lot of memory
> which is allocated on the stack (which is a very finite resource!).
>
> Here ends today's lesson.
>
>
Well, actually, I'm afraid you just re-introduced the problem:
in short
========
size_t maxbufsize = 0;
char *nme = NULL;
while (...)
{
...
if (maxbufsize < strlen(tpe) + 4) {
maxbufsize = strlen(tpe) + 4;
nme = nme ? realloc(nme, maxbufsize) : malloc(maxbufsize);
}
...
if (...) {
*nme++ = 'x';
...
}
}
if (nme)
free(nme);
...
========
IMHO does not work, since the nme++ changes nme, causing later realloc(nme,
maxbufsize) and/or free(nme) to "behave strangly" --- or simply segfault ...
RSS Feed