adric22 wrote:
>> char *t1;
>> char *at1;
>> t1=malloc(number_of_bytes);
>> /* use t1 */
>> *t1='a';
>> for ( i=0; i < number_of_bytes; i++ )
>> t1[i]=0;
>> at1=t1;
>> for ( i=0; i < number_of_bytes; i++ )
>> *at1++=0;
>> free(t1);
>> t1=NULL;
> Thanks for the help... but I'm not getting how to use this. Where do
> I specify these large, medium, compact, etc?
> Also, I tried using the malloc() but I'm not sure where to put that.
> Does it go in the global variable space, or in an actual procedure as
> part of the code? I tried it both ways and got an error. I tried
> this:
> unsigned char *variablespace;
> variablespace=malloc(65535);
> but it complained and gave two errors:
> 1) declaration needs type or storage class.
> 2) type mismatch in redeclaration of 'viarablespace'
> Essentially I'm trying to create a large array called variablespace
> that is at least 64K in size. I looked at that code snippet there,
> but I am not entirely sure what you are showing me.
If you are using the IDE the "memory model" will be one of the global
compilation options. The memory model controls what kind of default
pointer (far or near) is used for code and data.
large is often a good choice is there is a lot of code and data.
malloc is used right in the c code, often right in the function where
the memory is used. malloc returns a chunk of memory that can be used by
your program. malloc returns a POINTER to the memory.
here is an example of using an array, statically declared and
dynamically declared.
#define STR_LEN 127
#define NUM_FOOS 100
typedef struct
{
int x,y,z;
char str[STR_LEN+1];
}
foo_t;
/* static */
foo_t lots_of_foo[NUM_FOOS];
int i;
for ( i=0; i < NUM_FOOS; i++ )
{
lots_of_foo[i].x=100;
lots_of_foo[i].y=1;
lots_of_foo[i].z=22;
strcpy(lots_of_foo[i].str, "a string");
}
/* dynamic */
foo_t foo_pointer, *ft;
int i;
foo_pointer=malloc(sizeof(foo_t * NUM_FOOS);
/* malloc allocates bytes */
/* foo_pointer points toa region of memory big enough to hold 100 foo_
structures */
/* access foo_pointer using pointer syntax */
for ( ft=foo_pointer, i=0; i < NUM_FOOS; i++, ft++ )
{
ft->x=123;
ft->y=22;
ft->z=77;
strcpy(ft->str, "a string");
}
/* and showing how pointers can be treated like arrays */
for ( i=0; i < NUM_FOOS; i++ )
{
foo_pointer[i].x=45;
foo_pointer[i].y=22;
strcpy(foo_pointer[i], "a string");
}
free(foo_pointer); /* once all done with the memory, free it so it can
be used again for something else */
ft++
c knows ft points to a foo_t so the ++ operator adds the siz eof foo_t
to the pointer, and it now to the next structure.
Alex