#C/AllocMem()
12 messages in this thread
Hi, I am tring to figure out how to use AllocMem() to hold a 2 dimention
array, which is not initilized until a file is read and copied into it. I
don't seem to be able to do it. Can someone please offer suggestions to how
I can do it. I have to set the stack at 40000 now which I really don't want
to have to do. Thanks… Tom Gray
Tom,
To allocate a two dimensional array, you should actually declare a
pointer to the two dimensional array and AllocMem() the proper amount of
memory. Ex:
UWORD *array[20][40];
array = AllocMem(20 * 40 * sizeof(UWORD), MEMF_PUBLIC);
This method can be easily extended to any number of dimensions.
— Mike Roth / Lattice
Hi Mike,
the only problem I was concerned about using *array[20][40] is that I
have to keep it the same size no matter how big the file is that is being
read into it. I am copying a text file into the array incrementing the 1st
index on receiving a \n. That way I have an easy way to move the text up
and down a line. What I
really wanted to do was allocate memory as the file was being read, thus
only using up memory the size of the file. The Array I have had to declare
has been [300][80] which makes me set the stack at 30000 before starting
the program.I don't want to have to set the stack at all.
Also I was talking to Larry here in CO the other day and he was helping
me with something, I was asking how to make a program resident so I didn't
have to use run when starting it and it would give me back my CLI window.
He made a reference to the Manual , section G 5.6 or something, my manual
stops at 5.4 and then jumps to 6.1(Macro Assembler), I see nothing about
making programs resident. Is there an update to the manual? I received the
5.04 patch but hadn' t heard anything about a manual upgrade. Is there a
way I could get it?
Thanks for you help… Tom Gray
Tom, you could use a *array[20] and then allocate memory for each line as
you read it in.
e.g… while (string = read(file)
{
arraypointer[x] = (char *) AllocMem(strlen(string));
strcpy(arraypointer[x],string);
x++;
} (this is not real code, by the way, but I hope it shows what I
mean)
The good thing about this code is that it allocates less memory in total
than 40 bytes per line. Of course, its limitation is that you cannot then
edit the lines if you need to.
Khalid.
Tom,
To make a program resident, you just need to compile with -b1 (default)
and link with the cres.o startup file instead of the c.o startup. That
will usually do the trick unless you are doing strange things in your
program.
— Mike Roth
P.S.: Call up our customer service department. We have some inserts
that were added to the manual that were done after the original
5.0. You can also have them send you a 5.05 patch disk.
Mike,
First of all, would you believe:
UWORD (*array)[20][40]; /* or whatever */
Then, why would anybody in his right mind want to type all that stuff in
the function call, repeating the stuff in the declaration, and opening
himself to the future maintenance problems, when he could just enter:
array = AllocMem (sizeof (*array), MEMF_PUBLIC);
??? Or doesnt this work with Lattice? It sure works with the OTHER 'C'
compiler. And it's standard code. The code you gave shouldn't even
compile – the declaration was wrong.
BTW – I'd STILL like to know how I can get an upgrade for the Lattice
compiler I bought eons ago that has forty-eleven code generation bugs
without paying what amounts to a full new purchase price.
Jerry.
Well, Lattice DOES have a problem. Anytime you do anything like:
anytype *foo; foo = (anytype *) malloc(sizeof(*foo));
you get the folowing:
Warning 94: uninitialized auto variable "foo"
It's a minor bug (everything compiles just fine), but some folks INSIST
on warning-free code.
The uninitialized warning is coming from the sizeof(*foo). The compiler
apparently doesn't realize that sizeof() doesn't actually use the variable
and so it gives the warning. It seems to be treating sizeof() as if it
were any normal function. I will reproduce the problem and submit a bug
report to get it fixed.
— Mike Roth / Lattice
sizeof() is simply an unary operator, not a function, as I recall… It
just looks like a function, because people tend to put parens around the
thing it operates on. It's like 'return' in that sense. Interesting
warning, nonetheless.
Right. sizeof() isn't a function, so sizeof(*foo) shouldn't generate a
warning. When I first brought this up (6 mos. back or so) I seem to
remember someone from Lattice saying that they had taken a shot at fixing
this but it lead to real unitialized variables slipping through the cracks
on occasion, so it was left as is (sounds like a quick-fix that didn't
take. 😉
In any case, it's a minor irritant.
Jerry,
First, I didn't try to compile the code to test it, although maybe I
should have. I don't even remember the full message so can't respond to
your comments about the code quality. You can upgrade your compiler,
although the cost depends on how old your version is. At worst the upgrade
would be $100 which is definitely cheaper than a new purchase price.
— Mike Roth / Lattice
Mike,
You had:
<whatever-type> *array[n][m];
which is array [m] of array [n] of pointer to <whatever-type>. I know
how easy it is to make this kind of mistake, but …
I bought my compiler early on packaged by Commodore as the Commodore
Lattice compiler. However, within the first month I sent in the bug report
to Lattice (as the supplied documentation told me to) and was informed at
that time that I'd have to pay about $100.00 for an upgrade. I was (and
still am) totally disgusted with Lattice and for that reason will not
upgrade. You could only compile about 25% of the PD software available —
it had so many bugs. I felt that I was screwed by Lattice for a piece of
junk! And for that reason I have always recommended against the product;
if that's how well the company stands behind a product, it won't get any
good marks in my book. Why, I reported about 7 bugs, one of which was that
it wouldn't generate code that would work when indexing arrays of
structures. The other compiler didn't have nearly as many bugs and they
weren't as serious. Now … for the $100.00 that Lattice wanted, I wonder
how many $ is referral sales they lost just from the recommendations I gave
others.
Jerry.