http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

TheGrumpyProgrammer



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

Re: Dynamic arrays



> It's not the size changing factor of the linked list that is necessarily
> important. I also need a compact data structure. Let me give an example.

I've found (and been told) that linked lists are best suited for data where
it is important to be able to grow and shrink the amount of data,
particularly when you need to take elements out of or insert elements into
the middle of the list. IMHO, I agree with you that this is not what you
want.

> So, does anyone know much about this? I think it involves either using
the
> method used for strings or the new operator.

These are essentially the same thing in most repects. A "string" is really
an array of chars (there isn't really a string type of data in C, and I
don't think that there's one in C++, which I assume you're using).
Actually, one might say that there isn't really such thing as an array; a
string is just a pointer to chars (hence the (char *) type).

> I am sure there is a better way to solve this problem (I in fact know of
<snip>
> Again, this is just an example of the problem to illustrate the need.
Just
> addressing solutions to the redraw problem really don't help me... I'm
> interested in dynamically sized multi-dimensional arrays for a variety of
> problems, and to further my C++ knowledge.

I must agree. :] Any time when you're tempted to solve a problem by
throwing memory at it and you find that you're using that amount of it,
there is almost certainly a much better solution. What you mentioned would
probably crash my little computer, at least. I don't understand exactly
what you're trying to do, but I will tell you how I would do it (and
recommend that you try something else <g>). What I think might get you what
you want is a two-dimensional array that is dynamically allocated (like you
said). One way of doing this is to allocate a one-dimensional array and
calculate the position in the array when referencing values (this has
already been mentioned), something like:

/* where "pixel" is the data type you want to use */
pixel *buffer;
/* where "FooMemoryAllocate" is your favorite allocation function, such as
malloc(), new(), HeapAlloc(), or whatever, used in the proper syntax, with
proper error checking, of course */
buffer = FooMemoryAllocate(sizeof(pixel) * arrayXDim * arrayYDim);
/* now set the pixel at x, y in the buffer to the value of c */
buffer[x + y * arrayYDim] = c;

or you might do the same thing with a real two-dimensional array using a
pointer-to-a-pointer:

/* a pointer to a pointer to pixels */
pixel **buffer;
/* allocate an array of pointers to pixels, with a size of arrayXDim */
buffer = FooMemoryAllocate(sizeof(pixel *) * arrayXDim);
/* for every pointer to pixel that was just allocated, allocate arrayYDim
number of pixels */
for(i = 0; i < arrayXDim; i++) {
	buffer[i] = FooMemoryAllocate(sizeof(pixel) * arrayYDim);
}
/* now this will set the y'th pixel pointed to by the x'th pointer,
basically */
buffer[x][y] = c;

Now you can use a reallocation function to resize these. Make sure that you
free all the memory that is allocated (usually in reverse order of
allocation, with one call to the freeing function for each call to it's
corresponding allocation function). I'd suggest reading up on pointers,
arrays, and dynamic memory allocation. This is a well documented (probably
because it's initially very confusing, but also very important) subject. :]

-[ Neil-Edelman -- dreaded.neil@phreaker.net -- ICQ UIN: 705130
-[ http://neil.freeshell.org/ -- http://moop.bizland.com/
-[ "My air-to-air missile overrides your air traffic control clearance"

=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once

To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html