monit-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: alert.c


From: Jan-Henrik Haukeland
Subject: RE: alert.c
Date: Sat, 19 Oct 2002 00:01:36 +0200

> In this snippet of code from alert.c:
> *
> I have a few questions. Why are we using a fixed sized buffer for buf?

Since I'm to blame for the code I can try and answer this. The buffer is not
fixed, I relay on a feature in gcc that allows for dynamically declaring a
buffer size. The size is different for each string to replace. If this feature
didn't exist or another compiler than gcc was used it would probably be better
to malloc the string.

> Wouldn't it be easier and better to malloc it?

The idea (not a big point in this case) is that malloc is more expensive than
automatic variables. Besides it's one less free() to remember.

> Shouldn't we be using strncat?

Since we now the size of the strings we are operating on it's okay.

> Why do we free(*src) and then use it right after?

Because src is the old string and buf the new replaced string, we free(() src
to avoid a memory leak before we set src to point to the new string.

I hope that explained the function?

Jan-Henrik





reply via email to

[Prev in Thread] Current Thread [Next in Thread]