Subject: A *really* off question
To: NetBSD Package <tech-pkg@netbsd.org>
From: John A. Maier <johnam@mail.kemper.org>
List: tech-pkg
Date: 01/05/2000 13:36:44
This has very little  to do with NetBSD (yet) so forgive me, I'm
desparate!  I've just started learning the QT 1.44  and decided a GUI
user-manager would be a good app to write for learning QT.  So far so good! 
But I have a glitch...

What am I doing wrong?  I'm setting up a new class with a structure in it.  I
want to have an array of this new datatype and I'm want to dynamicly add and
delete array members, i.e. via realloc.

This is a sample of what I'm doing (it has nothing to do with what I'm actually
doing).  If you compile:
cc -g -o test test.cc -lc -lstdc++

and run the sample, you will see that the size of rndtest does *not* ever
increase and it cores!

Is sizeof() not able to see the size of a variable that has been resized?

For some reason, I've had no need to realloc anything for a long time and either
I've just forgotten or lost my mind (probably the latter :)

BTW I rewrote this code as a standard C program and it did the same thing but
it never cores (Even when I bump the loop up to 1000! God only knows where that
data is going)... --This is little comfort to me though-- :-(

Thanks for your help in advance, 

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

#include <stdlib.h>
#include <iostream.h>

class testalloc {
public:
	// This is my structure
	typedef struct {
		int rndnum1;
		int rndnum2;
	} STUFF;

	// create a variable of type STUFF
	STUFF rndtest[1];

	// In my constructor I want to add 10 new array members to rndtest.
	testalloc()
	{
		int iCnt; // index to keep track of where we are
		int NewSize; // Use this to for calculation of new size.
		for(iCnt=0; iCnt<10; iCnt++)
		{
			cout << "Old size: " << sizeof(rndtest) << "\n";
			// get the size of entire array and add it to the size
			// of just one element of the array.
 			NewSize = sizeof(rndtest) + sizeof(rndtest[0]); 
			cout << "Resize to: " << NewSize <<  "\n";

			// give rndtest a new size
			cout << "Address: " << realloc(rndtest, NewSize) <<"\n";
 			cout << "New size: " << sizeof(rndtest) << "\n";
			rndtest[iCnt].rndnum1 = rand(); // bogus data
			rndtest[iCnt].rndnum2 = rand(); // bogus data
			cout <<  "Index: " << iCnt << "Rnd1: " << rndtest[iCnt].rndnum1 << ": Rnd2: " << rndtest[iCnt].rndnum2 << "\n\n";
		}
	}
};

main()
{
	testalloc testme;
}
---------------------------------

-- 
John A. Maier
Kemper Military School and College
Director of MIS
Voice: 660-882-5623
Fax: 660-882-3778
Email: johnam@mail.kemper.org
Web: http://www.kemper.org