Using C, I wanna divide memory space into few segments. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

28th May 2021, 1:39 AM
이유준
이유준 - avatar
6 Answers
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?
28th May 2021, 3:44 AM
Daniel Briceño
Daniel Briceño - avatar
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?
28th May 2021, 3:40 AM
Daniel Briceño
Daniel Briceño - avatar
0
Now let your question better. What do you want to do? int arr[8000];//sizeof(arr);//4?
28th May 2021, 4:11 AM
Daniel Briceño
Daniel Briceño - avatar
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?
28th May 2021, 4:14 AM
이유준
이유준 - avatar
0
Martin Taylor I thought that rule only applies to binary.
31st May 2021, 8:16 PM
Daniel Briceño
Daniel Briceño - avatar
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?
31st May 2021, 8:18 PM
Daniel Briceño
Daniel Briceño - avatar