in Technology by
How is the dynamic memory allocation in 1D array in C?

1 Answer

0 votes
by
In the below example, I am creating a pointer to an integer and assign it heap memory. When memory is successfully assigned to the pointer then we can use this pointer as a 1D array and using the square braces “[]” we can access the pointer as like the statically allocated array. Let us see the below Image for better understanding. Let’s see a program example, #include #include #define FAIL 1 #define TRUE 0 int main(int argc, char *argv[]) { int *piBuffer = NULL; //pointer to integer int nBlock = 0; //Variable store number of block int iLoop = 0; //Variable for looping printf("\nEnter the number of block = "); scanf("%d",&nBlock); //Get input for number of block piBuffer = (int *)malloc(nBlock * sizeof(int)); //Check memory validity if(piBuffer == NULL) { return FAIL; } //copy iLoop to each block of 1D Array for (iLoop =0; iLoop < nBlock; iLoop++) { piBuffer[iLoop] = iLoop; } //Print the copy data for (iLoop =0; iLoop < nBlock; iLoop++) { printf("\npcBuffer[%d] = %d\n", iLoop,piBuffer[iLoop]); } // free allocated memory free(piBuffer); return TRUE; } Output:

Related questions

0 votes
    What is static memory allocation and dynamic memory allocation?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    How to dynamically allocate a 1D and 2D array in c?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    In Visual Studio, we've all had "baadf00d", have seen seen "CC" and "CD" when inspecting ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    What are the steps to use shape for a 1D array, 2D array and 3D/ND array respectively in NumPy?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    Which of the following function stacks 1D arrays as columns into a 2D array? (a) row_stack (b) ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    List the steps to create a 1D array and 2D array in NumPy?...
asked Apr 24, 2021 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
+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
    Distinguish between malloc() & calloc() memory allocation?...
asked Jan 17, 2021 in Technology by JackTerrance
+1 vote
    Distinguish between malloc() & calloc() memory allocation....
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    I am currently trying to create an autocomplete with a source that is stored in a javascript variable but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    Which of these class object can be used to form a dynamic array? (a) ArrayList (b) Map (c) Vector ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these standard collection classes implements a dynamic array? (a) AbstractList (b) LinkedList (c) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of these class object can be used to form a dynamic array? (a) ArrayList (b) Map (c) ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
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
...