Qbasicnews.com

Full Version: I need some help with using memory! (C)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I get how to make free memory it´s just using calloc and then telling it what kind of veribel you whant to make memory for but the thig I sont get is how I give a value of a verbiel to this memory place and how to get it from there as it is now I only know how to take the free monery not how to using it Sad (and man that is one looooong sentens)
¡I HAVEN'T UNDERSTOOD A SINGLE WORD!! could you type slower? What is veribel??

Let me guess...

To reserve some memory you must tell malloc how much bytes you want to reserve, and then make a cast to assing a type. For example, for an array of integers:

Code:
int *arrayofintegers;

arrayofintegers = (int *) malloc (10 * sizeof(int));

The above code reserves memory to store 10 integer values. That's the 10*sizeof(int), that is, the number of bytes which will be reserved. Once the memory is reserved, arrayofintegers becomes a pointer to that zone of memory, otherwise it equals NULL (for example if there is no memory left to allocate). That's why we should check that out:

Code:
if (arrayofintegers == NULL)
{
   puts("ERROR! Out of memory :( ");
   exit(-1);
}

Just to be safe.

Then we can use the memory, for example to store stuff. We can use it as an array, that's one of the things that makes "C" rock:

Code:
arrayofintegers[0] = 6;
arrayofintegers[8] = 72;

Once we are done with our dynamic array, we should free the memory:

Code:
free(arrayofintegers);

Smile
I know how to make free memory but I dont know how to use it. If you did explain this in your post I didn´t get it ( and in that case I´m dumb Wink )
Quote:Then we can use the memory, for example to store stuff. We can use it as an array, that's one of the things that makes "C" rock:

Code:
arrayofintegers[0] = 6;
arrayofintegers[8] = 72;

Not too hard: Just like an array... I think you know how arrays are used...

Back later, and for example:

This is the "static" way of doing things: 10 indexes integer array:

Code:
int main(void)
{
   int array[10];
   int i;

   for(i=0;i<10;i++)
      array[i] = i;

   printf("Array value at position #5 is %d", array[5]");
}

This is the "dynamic" way of doing things: 10 indexes integer dynamic memory array:

Code:
int main(void)
{
   int *array;
   int i;

   // Make array:
   array = (int *) malloc (10 * sizeof(int));

   // Check if ev'rything goes ok:
   if (array == NULL)
   {
      puts("Error! Not enough memory - buy more DIMMs :(");
      exit(-1);
   }

   // Ok, now we can use it:

   for(i=0;i<10;i++)
      array[i] = i;

   printf("Array value at position #5 is %d", array[5]");
}

Note that once the array is created (memory allocated), you can use it as a normal static array. Hope this makes it clear now.
yeah thanks a ton and I just do the same thing with calloc?
hmm I see now that you use it just like a varibel or array with out memory so what is it good for? I mean cantI just make an array out of it right away?
Well, it *is* good when you're into complex data types such as lists, queues, trees and such. Also, they are really util for dynamic arrays which size varies along the program. Imagine that you are coding some kind of database. If you make your program to have 10,000 entries from the beginning even though they are blank, you'll kill windows 'cause it will be out of memory really soon, so it is better to start with an array with 0 entries and reallocate a bit of memory everytime the user enters a new entry. That way you are only taking the memory you need.
ah okey cool Smile
btw I got a question if I wanna creat places in my array when I need them can I do it this way?

Code:
int array;
int i;
for(i=0;i<300;i++)
{*the code does somthing*
array = (int *) malloc (i * sizeof(int));
*the code does somthing else*}
To add space or shrink an already allocated space you should use realloc. Malloc is for the first time you use it, realloc is for the subsequent times. Basicly, realloc tries to append more space at the end of the currently allocated space, but if it doesn't fit it manages to find a free memory area big enough, then moves the data there. Check your help file for syntax, it is self explanatory (at least in borland, microsoft and DJGPP helps)
Pages: 1 2 3