How to use alloc's memory allocation functions?

Reply

Join Date: Jun 2004
Posts: 34
Reputation: Fili is an unknown quantity at this point 
Solved Threads: 0
Fili's Avatar
Fili Fili is offline Offline
Light Poster

How to use alloc's memory allocation functions?

 
0
  #1
Jun 18th, 2004
I have a very important question (for me)

How do i use <alloc.h>/<malloc.h>/<stdlib.h> for memory allocation?

i'm talking about: malloc(),calloc(),halloc(),realloc(),alloca(),heapwalk(),free()
etc?
i'm having a lot of trouble with these!

thank you!!
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: How to use alloc's memory allocation functions?

 
0
  #2
Jun 19th, 2004
These are used to allocate memory dynamically.With these you get memory from the OS and point to that memory address with a pointer.

Presonally I use C++ new operator.

Eg.

Some_data_type *object;

object = (Some_data_type*)malloc(sizeof(Some_data_type),1);

that is to assign memory.

Then when you are finished with it:

free(object);

That's the general way to use it.
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 34
Reputation: Fili is an unknown quantity at this point 
Solved Threads: 0
Fili's Avatar
Fili Fili is offline Offline
Light Poster

Re: How to use alloc's memory allocation functions?

 
0
  #3
Jun 23rd, 2004
Thx. I looked it up in a book.

BTW: Instead of using a vector: int a[100] you could use:
  1. int *a;
  2. a=(int*)malloc(100*sizeof(int)); OR
  3. a=(int*)calloc(100,sizeof(int));

It's kind of useful. If you wanna read a vector of an undefined size (n), you have to aproximate and think within which intervals it is. If you use:

  1. int n;
  2. //reads the size
  3. printf("n=");
  4. scanf("%i",&n);
  5. int *a=(int*)malloc(n*sizeof(int));

It's especially useful when you don't what to use up more memory than required!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC