[ Usenet FAQs | Web FAQs | Documents | RFC Index ]
    Search the FAQ Archives

Single Page

Top Document: comp.windows.x.intrinsics Frequently Asked Questions (FAQ)
Previous Document: 23. How do I reparent a widget in Xt, i.e. XtReparentWidget()?
Next Document: 25. How to debug an Xt application?


24. Why use XtMalloc, XtFree, etc?


----------------------------------------------------------------------

Unfortunately, most code that calls malloc(), realloc() or calloc()
tends to ignore the possibility of returning NULL.  At best it is
handled something like:

	ptr = (type *) malloc (sizeof (type))
	if (!ptr)
	{
		perror ("malloc in xyzzy()");
		exit (1)
	}
To handle this common case the Intrinsics define the functions
XtMalloc(), XtCalloc(), XtNew(), XtNewString() and XtRealloc() which
all use the standard C language functions malloc(), calloc() and
realloc() but execute XtErrorMsg() if a NULL value is returned.  Xt
error handlers are not supposed to return so this effectively exits.

In addition, if XtRealloc() is called with a NULL pointer, it uses
XtMalloc() to get the initial space.  This allows code like:

	if (!ptr)
		ptr = (type *) malloc (sizeof (type));
	else
		ptr = (type *) realloc (ptr, sizeof (type) * (count + 1));
	++count;

to be written as:

	ptr = XtRealloc (ptr, sizeof (ptr) * ++count);

Also, XtFree() accepts a NULL pointer as an argument.  Generally, I've
found the Xt functions conveniant to use.  However, anytime I'm
allocating anything potentially large I use the standard functions so
I can fully recover from not enough memory errors.

XtNew() and XtNewString() are conveniant macros for allocating a
structure or copying a string:

	  struct abc *xyzzy;
	  char	     *ptr;
	  char	     *str = "abcdef";

	  xyzzy = XtNew (struct abc);	/* takes care of type casting */
	  ptr = XtNewString (str);

A strict interpretation of the Intrinsics reference manual allow an
implementation to provide functions that are not exchangable with
malloc() and free().  I.e. code such as:

	 char	      *ptr;

	 ptr = XtMalloc (100);
	 /* ... */
	 free (ptr);

may not work.  Personally, I'd call any implementation that did this
broken and complain to the vendor.

A common error for Motif programmers is to use XtFree() on a string
when they should really be using XmStringFree().



Top Document: comp.windows.x.intrinsics Frequently Asked Questions (FAQ)
Previous Document: 23. How do I reparent a widget in Xt, i.e. XtReparentWidget()?
Next Document: 25. How to debug an Xt application?

Single Page


[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
ware@cis.ohio-state.edu

Last Update October 23 2009 @ 09:20 AM

Some parts © 2009 Advameg, Inc.