445,928 Members | 1,810 Online
Need help? Post your question and get tips & solutions from a community of 445,928 IT Pros & Developers. It's quick & easy.

# Dynamically growing an array (pointer question)

 P: n/a Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: /* Allocate 1st dimension */ if((Screen = (Atom ***) malloc(sizeof(Atom **) * Width)) == NULL) perrexit("malloc"); /* Allocate 2nd dimension */ for(i = 0; i < Width; i++) if((Screen[i] = (Atom **) malloc(sizeof(Atom *) * Height)) == NULL) perrexit("malloc"); /* Set Screen all NULL initially */ for(i = 0; i < Width; i++) for(j = 0; j < Height; j++) Screen[i][j] = NULL; /* Can I do this? */ Can I then access Screen using normal Array subscript notation? I keep getting strange behaviour in another part of the program, and I'm not sure if this is the problem. I hope this wasn't to verbose. Thanks for any pointers. Fabian Nov 14 '05 #1
7 Replies

 P: n/a Fabian Wauthier wrote: I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: /* Allocate 1st dimension */ if((Screen = (Atom ***) malloc(sizeof(Atom **) * Width)) == NULL) perrexit("malloc"); /* Allocate 2nd dimension */ for(i = 0; i < Width; i++) if((Screen[i] = (Atom **) malloc(sizeof(Atom *) * Height)) == NULL) perrexit("malloc"); /* Set Screen all NULL initially */ for(i = 0; i < Width; i++) for(j = 0; j < Height; j++) Screen[i][j] = NULL; /* Can I do this? */ Can I then access Screen using normal Array subscript notation? I keep getting strange behaviour in another part of the program, and I'm not sure if this is the problem. I guess you know that you don't have a real 2-dimensional array here, you're "faking" one by having an array of pointers, each pointing to an array of Atom structure pointers and use that with the declaration SOME_TPYE **a; the expression "a[i][j]" is evaluated as "*(*(a+i)+j)" to be able to empoy the normal array subscript notation for something that isn't a 2-dimensional C array. So you won't, for example, be able to use the sizeof operator on that "array" and get the same results as with a real array. And you won't be able to use functions written for use with a real 2-dimensional C array of Atom pointers (they'd expect an Atom** and not an Atom***). But as long as you keep that in mind your use of 'Screen' with the notation for 2-dimensional arrays looks ok. BTW, you can save a lot of calls of malloc() (and later of free()) if you allocate the whole set of Atom pointers a once like this: if ( ( *Screen = malloc( Width * Height * sizeof **Screen ) ) == NULL ) perrexit( "malloc" ); for ( i = 1; i < Width; i++ ) Screen[ i ] = Screen [ i - 1 ] + Height; Regards, Jens -- \ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de \__________________________ http://www.toerring.de Nov 14 '05 #2

 P: n/a On 12 Apr 2004 11:08:33 GMT, Je***********@physik.fu-berlin.de wrote: BTW, you can save a lot of calls of malloc() (and later of free()) ifyou allocate the whole set of Atom pointers a once like this:if ( ( *Screen = malloc( Width * Height * sizeof **Screen ) ) == NULL ) perrexit( "malloc" ); If the OP has defined Screen as he indicated, via: Atom ***Screen; then, at the very least, I do not think you want that asterisk in front of 'Screen' up there. After your single malloc, you've got a pointer (Screen) to a 2D matrix of atom pointers; you no longer have the situation where Screen points to the first element of an "array of arrays of atom pointers" as in the OP's scenario, so whatever you're doing down below, I don't see how it can be healthy:for ( i = 1; i < Width; i++ ) Screen[ i ] = Screen [ i - 1 ] + Height; Regards, Jens Since the OP mentioned he wants to "dynamically grow" the array, rather than just dynamically /allocating/ it, he'd need the array of pointer to arrays in order to, say, grow the Height but not the Width. I have no idea if that's what he really wants to do or not, but it's another thing that would be difficult with the "one malloc fits all" approach rather than allocating the columns piecemeal as he did. -leor -- Leor Zolman --- BD Software --- www.bdsoft.com On-Site Training in C/C++, Java, Perl and Unix C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html Nov 14 '05 #3

 P: n/a Leor Zolman wrote: On 12 Apr 2004 11:08:33 GMT, Je***********@physik.fu-berlin.de wrote:BTW, you can save a lot of calls of malloc() (and later of free()) ifyou allocate the whole set of Atom pointers a once like this:if ( ( *Screen = malloc( Width * Height * sizeof **Screen ) ) == NULL ) perrexit( "malloc" ); If the OP has defined Screen as he indicated, via: Atom ***Screen; then, at the very least, I do not think you want that asterisk in front of 'Screen' up there. After your single malloc, you've got a pointer (Screen) to a 2D matrix of atom pointers; you no longer have the situation where Screen points to the first element of an "array of arrays of atom pointers" as in the OP's scenario, so whatever you're doing down below, I don't see how it can be healthy:for ( i = 1; i < Width; i++ ) Screen[ i ] = Screen [ i - 1 ] + Height; Sorry, I meant the OP to keep the first allocation of the array of pointers to Atom pointers: if ( ( Screen = malloc( Width * sizeof *Screen ) ) == NULL ) perrexit( "malloc" ); and only then to allocate memory for the arrays of Atom pointers all at once, initializing Screen[0] (aka *Screen) with that pointer and finally setting up the rest of the pointers, making them point into that memory region. Since the OP mentioned he wants to "dynamically grow" the array, rather than just dynamically /allocating/ it, he'd need the array of pointer to arrays in order to, say, grow the Height but not the Width. I have no idea if that's what he really wants to do or not, but it's another thing that would be difficult with the "one malloc fits all" approach rather than allocating the columns piecemeal as he did. That's correct, of course. I made the mistake of interpreting "dynami- cally grow" to mean allocate dynamically (as opposed to creating a true 2-dimensional array with fixed sizes). If the OP really wants to be able to change the sizes dynamically afterwards there's obviously no way around allocating each array of pointers to Atom pointers of size 'Height' individually. Sorry for the confusion and thanks for pointing that out. Regards, Jens -- \ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de \__________________________ http://www.toerring.de Nov 14 '05 #4