in Technology by
How can you determine the size of an allocated portion of memory?

1 Answer

0 votes
by

In C language, we can calculate the size of the static array using the sizeof operator but there is no operator to calculate the size of the dynamically allocated memory. So using some trick we can get the size of the allocated array. Mainly there are two ways to get the size of allocated memory in every section of the code.

  • Create a global variable to store the size of the allocated memory.
  • Carry the length of allocated memory.

Let see an example code, where I am explaining how you can carry the length of the array. So suppose you need to create an integer array whose size is n. So to carry the array length of the array, you need to allocate the memory for n+1.

int *piArray = malloc ( sizeof(int) * (n+1) );

 

If memory is allocated successfully, assign n (size of the array) its 0 places.

piArray[0] = n;
or
* piArray = n;

 

Now it’s time to create a copy of the original pointer but to left one location from the beginning.

//Left 0th location
int * pTmpArray = piArray +1;

 

Now, whenever in a program you require the size of the allocated dynamic, you get from the temporary pointer(copy pointer).

Related questions

0 votes
    Which built-in library function can be used to re-size the allocated dynamic memory?...
asked Jan 18, 2021 in Technology by JackTerrance
+1 vote
    Which built-in library function can be used to re-size the allocated dynamic memory in C-Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    Which function can be used to release the dynamic allocated memory in C?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    In an application that uses heap, the memory for data is allocated ____________ (a) logical (b) dynamically (c) ... Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    In an application that uses stack, the memory for data is allocated ____________ (a) logical (b) dynamically (c) ... Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    Where is a new object allocated memory? (a) Young space (b) Old space (c) Young or Old space ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    The _____tools help you to select a portion of an image that is to be edited. Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    It seems to be often said that Task Manager does not provide an accurate indication of how much memory is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    It seems to be often said that Task Manager does not provide an accurate indication of how much memory is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    Which operator can be used to determine the size of a data type or variable?...
asked Jan 18, 2021 in Technology by JackTerrance
+1 vote
    Which operator can be used to determine the size of a data type or variable in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    How to determine the size of elastomeric bearings?...
asked Jan 1, 2021 in Technology by JackTerrance
0 votes
    I'm using the cacheCounter in CakePHP, which increments a counter for related fields. Example, I have a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm using the cacheCounter in CakePHP, which increments a counter for related fields. Example, I have a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...