Professional Documents
Culture Documents
Vector(){
size = 0;
data = NULL;
owner = 0;
}
//only constructor to allocate space for data
Vector(long int n){
size = n;
data = (double *)malloc(sizeof(double)*n);
owner = 1;
}
malloc(1000*1000)
the function returns a pointer to 106 bytes of memory. The pointer is of type void *.
In the usage
data = (double *)malloc(sizeof(double)*n);
void * is cast to double *. Type casts are used to convert values of one type to another type. For
example, we may say (int)1.4142 to convert the double value 1.4142 to an int (it will be truncated). The cast is
needed here because data is of type double * while malloc() returns void *.
What happens during malloc()? The short answer, which will be elaborated later, is that the function call
first goes to the C library. The C library may in turn call the operating system if it is not able to come up with the
memory by itself. The operating system typically allocates an area in virtual memory to the calling process. No
region in physical memory is set aside. Physical memory is set aside only when the process first tries to access the
memory it has claimed for itself. There is a page fault during first access, and the page fault handler sets aside
physical memory.
Suppose we need an array of Vector objects of length 100 with each vector of size 1000. The definition
https://divakarvi.github.io/bk-spca/spca.html[20-1-2019 23:44:49]