943,503 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 18312
  • C RSS
Jun 18th, 2004
0

How to use alloc's memory allocation functions?

Expand Post »
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!!
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jun 19th, 2004
0

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

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.
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 23rd, 2004
0

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

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!
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: New Type
Next Thread in C Forum Timeline: program help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC