[lug] C questions.

George Sexton gsexton at mhsoftware.com
Thu Nov 18 10:01:37 MST 1999

>> 1) I once found, but now lost, a web page containing the documentation
>> for all the gcc/egcs functions.  Anybody have a good reference site or
>> book they love?  Already have K&R, was wanting something a bit more
>> modern, advanced, and comprehensive.  I'd absolutely love a good "tips

P.J. Plauger's Book the Standard C Library is a good reference to have. If
you are interested in portable code, it is indispensable.

>> 2) I need to try to do some dynamic memory allocation.  I'd like to find
>> a brief explanation of how it should be done, or better yet some example
>> code somewhere?  Basically, I need to find the nonzero indeces of a
>> _HUGE_ (like more than millions of entries) sparse matrix.  All I'm
>> doing is growing a two dimensional array which will have the row and
>> column indices of said entries, but I have no idea how many I have until
>> I find them!  It seems inefficient to call realloc every time I find a

Well, a linked list would be one approach but it would be pretty wasteful in
terms of memory consumption. Perhaps something like:

#define 	MAX_NODECOUNT	1024
typedef struct MyStruct {
	int index[MAX_NODECOUNT];
	int useCount;
	struct MyStruct *pNext;

the basic idea would be to allocate a linked list and each node contains
1024 indices. Pseudo code would look something like

MYSTRUCT *pCurrent, *pHead;
pCurrent=pHead=calloc(sizeof struc MyStruct,1);
if (this is one we care about)
	if (pCurrent->useCount==MAX_NODECOUNT) {
		pCurrent->pNext=calloc(sizeof struct MyStruct,1);
		memset(pNext,0x00,sizeof struct MyStruct);
	pCurrent->index[pCurrent->useCount++]=Index #;

Finding the items now becomes:

while (pCurrent) {
	for (i=0; i < pCurrent->useCount; i++) {
		perform processing....

Deallocating memory, etc is left as an exercise for the reader...

George Sexton
MH Software, Inc.
Voice: 303 438 9585
Fax: 303 469 9679
URL: http://www.mhsoftware.com

More information about the LUG mailing list