[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Need Orca/C help...
Joseph T. Lee (jlee@Bonnie.ICS.UCI.EDU) wrote:
: The simplest way is to just do this:
: int array[100][100] = {0};
On the IIgs the array is initialized to zero initially anyways if it is
an external array. I would not ever suggest you put an array of this size
in the routine itself. This would require a stack size of 10000*sizeof(int).
(It looks like it is external anyways.)
:This initializes the array position (0,0) to zero and in the docs, it also says
:that all array positions not intialized along with the declaration initialization
:also gets set to zero, so this is the simplest way.
(you may want to set your text length size to less then you are using now.)
I will not restate what other people have already said. Personally I
like tm@netcom.com's("Toshiyasu Morita") post the best. See below for
the reasons.
: BTW, you can do this with any pointer, it's just not efficient. That's where
: pointer math comes in, but (hint, hint :) it's something you should look at.
Huh?!? What do you mean by not efficient?!? It is very efficient.
Especially on 68000 architecture. There is only one more dereference needed
on the IIgs.
: I don't suggest using calloc nor malloc calls unless you know what you're
: doing and you're writing really weird code. The simple initialization during
: the declaration of the array should be just fine.
I will agree about the "know what you are doing" but the "simple
initialization" may seem good on some machines but others it will make
other parts of the program slower.(for instance loading or in the case of
stack allocation accessing a single element in the array. I probably
wouldn't put 68000 or RISC's in that catagory due to the stack simulation
using address registers.)
I also do not agree with the "really weird code" part either. Once you
learn how to use pointers and malloc and calloc the allocation of memory
becomes easier anyways. Programming gets eisier as well as data structures
are easier as well. ect....
In article <> tsang@cs.washington.edu ("Donald Tsang") writes:
Again some good pointeres but...
!No, the compiler usually converts freely between p[i] and *(p+i)...
!shouldn't make a difference.
Be careful of this. The *(p+1) will work in just about any place but
the p[i] will produce wierd results if i is equal to a negative number.
(Yes I know that *(p-4) can still produce wierd results but they are still
not as bad as bank wrapping with p[-4]. And if p = originalp + 5 then
*(p-4) is the same as originalp+1 while p[-4] could be anywhere.:-) )
Now for some of the reasons for the (malloc memset)/calloc approach.
1) Even though the IIgs produces sparce files other machines do not. Your
program will have a file length of 10000*sizeof(int). On the IIgs that
is a size of ~39 blocks. Of course, again because of the sparce file
feature in ProDOS (and also the loader space initialization) it will
only show up when asking for the file length but the loader will now
have to initialize the array for you.
2) Using malloc and calloc will cut down on the precious size of your data
bank.
3) In the case of stack allocation (If you don't understand what I mean
then:
main(){
int myarray[100][100];
...
} )
I would never suggest it.
4) If you want to have your program restartable, you should use malloc and
calloc. The single initialization only happens once on restartable
programs.(Try it and see.)
5) I am sure there are more.
I would suggest you use tm@netcom.com("Toshiyasu Morita") sugests.
Gary
--
Gary F. Desrochers
garyd@windipc.nrel.gov
I be gone in 1.5 months. Anyone know a good internet site in New Hampshire.