| | |
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.
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.
Thx. I looked it up in a book.
BTW: Instead of using a vector: int a[100] you could use:
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:
It's especially useful when you don't what to use up more memory than required!
BTW: Instead of using a vector: int a[100] you could use:
C Syntax (Toggle Plain Text)
int *a; a=(int*)malloc(100*sizeof(int)); OR 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:
C Syntax (Toggle Plain Text)
int n; //reads the size printf("n="); scanf("%i",&n); int *a=(int*)malloc(n*sizeof(int));
It's especially useful when you don't what to use up more memory than required!
![]() |
Similar Threads
- Dynamic memory allocation with strings (C)
- 2D array memory allocation error (C++)
- memory allocation ptr to array? how? (C)
- Why use Dynamic and Static Memory Allocation... (C)
- memory allocation (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C Forum
- Previous Thread: Using .length() with multidimensional char array ? (Help)
- Next Thread: program help
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue mysql number odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





