+ 1
Using C, I wanna divide memory space into few segments.
Assume that I have 32KB space, and I wanna use each 8 blocks of 4KB to use two dimention array. How should I use malloc function? ■■■■■■■■ 32kb memory -> ■ is 4kb block in ■, I want to put in 2 dimensional array to put 16 integers
6 ответов
0
Can you do: 32kb is 32000bytes
void* arr_32;//I declare a pointer to Void that Malloc will return.
arr_32=malloc(32000);//return void*;
if you already know the size of the matrix, you can do it without using malloc and it works out even better in some cases.
void* arr[4000];
//int a=sizeof(void*);//We measure the type of data, in this case Void *
//printf("%d",a);
//Output: 8
//a=32000/a;//We divide the type of data by the amount you want.
//printf("%d",a);
//Output: 4000
//void* arr[a];//We declare the matrix.
But out of curiosity: Why do you want to do it?
0
The actual allocation is made by the OS (System Operating). Your application the only thing that can do is make a request to virtual memory and if the OS wants it will transform it into physical memory. Why do you want to request 32kb of memory?
0
Now let your question better. What do you want to do?
int arr[8000];//sizeof(arr);//4?
0
32 byte is one of my project's condition my teammates wants.
can I ask one more?
whole memory is then 32kb and I want to divide this virtual memory to 8 pieces and get each index. is that possible?
0
Martin Taylor I thought that rule only applies to binary.
0
이유준 Thi?:
int a[2];
a[0]=(int)malloc(sizeof(int)*4);//We store the start of the string as an integer value.
((int*)a[0])[0]=1;//In order to use the second arrays we must transform the integer into a pointer.
free((int*)a[0]);//Free allocated memory.
Martin Taylor Does this code look good?