Memory management is one of the most fundamental and important aspect for any computer programming language. In the dynamic memory allocation, the memory is allocated to a variable or program at the run time.
The document discusses dynamic memory allocation in C. It explains that dynamic allocation allows programs to obtain more memory while running or release unused memory. The main dynamic memory functions in C are malloc(), calloc(), free(), and realloc(). Malloc allocates memory, calloc allocates and initializes to zero, free deallocates memory, and realloc changes the size of allocated memory. Examples of using each function are provided.
The document discusses dynamic memory allocation in C programming. It provides details on four main library functions - malloc(), calloc(), free(), and realloc() - that allow dynamic memory allocation. malloc() allocates memory without initialization, calloc() allocates and initializes memory to zero, free() de-allocates previously allocated memory, and realloc() changes the size of previously allocated memory. Examples are given to demonstrate the usage of each function. The document also discusses self-referential structures, bit fields, typedef, scope of variables, and storage classes in C.
This document discusses dynamic memory allocation in C using four library functions: malloc(), calloc(), realloc(), and free(). It explains that malloc() allocates memory and returns a pointer, calloc() allocates memory and initializes it to zero, realloc() changes the size of previously allocated memory, and free() frees memory allocated by the other functions. Code examples are provided to illustrate usage of each function.
Dynamic memory Allocation in c languagekiran Patel
Computer memory can store information temporarily or permanently. There are two types of memory allocation: static allocation at compile time and dynamic allocation at runtime. Dynamic allocation uses functions like malloc(), calloc(), realloc(), and free() to allocate and free memory as needed during program execution. Malloc allocates a single block, calloc allocates multiple blocks and initializes to zero, realloc changes the size of an existing block, and free releases a block back to the system.
Dynamic memory allocation functions like malloc(), calloc(), and realloc() allow programs to dynamically allocate memory at runtime rather than using fixed-size arrays. Malloc allocates a block of contiguous memory and returns a pointer to it. Calloc also initializes the allocated memory to zero. Realloc changes the size of previously allocated memory. These functions help programs efficiently allocate only as much memory as needed rather than reserving a fixed maximum amount.
Data Structure - Dynamic Memory Allocationbabuk110
The document discusses dynamic memory allocation in C. It explains that the amount of data needed is not always known beforehand, so static allocation of memory is inefficient. Dynamic allocation functions like malloc(), calloc(), free() and realloc() allow allocating memory at runtime based on the program's needs. Malloc allocates a block of memory and returns a pointer, while calloc initializes the memory to zero. Free releases previously allocated memory and realloc changes the size of allocated memory.
The document discusses the stack and heap memory in C programming. The stack is a region of memory that stores local variables and function calls. It uses a last-in, first-out approach and frees memory automatically. The heap is a dynamic memory region not tightly managed by the CPU. Functions like malloc(), calloc(), and free() must be used to allocate and free memory in the heap to avoid leaks. The heap does not free memory automatically.
A linked list is a linear data structure consisting of nodes that are connected to each other. Each node contains data and a pointer to the next node. The first node is referenced by the head pointer and the last node points to NULL. Linked lists can be singly, doubly, or circularly linked. A singly linked list is represented by a struct containing an integer data field and a pointer to the next node. Memory for nodes is dynamically allocated using functions like malloc() and freed using free().
The document discusses dynamic memory allocation in C using functions like malloc(), calloc(), realloc() and free(). It explains that malloc() allocates memory without initializing it, calloc() allocates memory and initializes it to zero, and realloc() resizes previously allocated memory. Examples are provided to demonstrate allocating memory for arrays, 2D arrays, structures and resizing memory. It emphasizes the importance of freeing unneeded memory to avoid memory leaks.
Dynamic memory allocation in C allows the size of data structures to be changed during program execution. The malloc() function allocates memory on the heap and returns a void pointer. Additional functions like calloc(), free(), and realloc() allow allocating and freeing contiguous blocks of memory and resizing allocated blocks. System calls like brk() and sbrk() are used to change the program break which marks the end of the heap.
This document discusses dynamic memory allocation in C. It explains that dynamic allocation allows memory to be allocated at runtime, unlike static allocation which requires defining memory sizes at compile time. The key functions covered are malloc() for allocating memory blocks, calloc() for arrays and structures, realloc() for resizing allocated blocks, and free() for releasing used memory to avoid memory leaks. Examples are provided to demonstrate how each function is used.
Dynamic memory allocation allows programs to dynamically allocate and free memory at runtime rather than having fixed-size arrays. Malloc allocates memory and leaves it uninitialized while calloc allocates and initializes memory to zero. Realloc can change the size of previously allocated memory. Free must be used to release dynamically allocated memory to avoid memory leaks. In C++, new allocates memory and returns a pointer while delete frees memory allocated by new.
Dynamic memory allocation in C allows memory to be allocated and freed at runtime. There are two types: static allocation assigns memory at compile time, while dynamic allocation uses functions like malloc(), calloc(), and realloc() to assign memory as needed at runtime. realloc() changes the size of an existing dynamically allocated block, and free() releases memory to be used again. Examples demonstrate allocating and accessing memory for arrays, structures and integers using these functions.
The document discusses dynamic memory allocation in C using functions like malloc(), calloc(), free(), and realloc(). It provides:
1) malloc() allocates memory at runtime and returns a pointer to the allocated block. It does not initialize the memory.
2) calloc() also allocates memory at runtime but initializes all bits to zero.
3) free() frees up previously allocated memory so it can be used again.
4) realloc() resizes previously allocated memory blocks, allowing memory usage to grow or shrink as needed.
The document provides an introduction to memory management in C. It discusses that memory can be allocated in C either by declaring variables, which allocates space on the stack, or by explicitly requesting space from C using functions like malloc(), which allocates space on the heap. The stack follows LIFO order and releases space automatically when the program finishes, while the heap requires manual memory management using free(). Functions like malloc() allocate raw memory on the heap and return a pointer to it, while calloc() allocates initialized memory in blocks. The document also discusses stack overflow and the different memory sections in RAM like text, data, BSS, and heap.
This document discusses dynamic memory allocation in C. It explains that dynamic allocation allows memory to be allocated at runtime, unlike static allocation which requires defining memory sizes at compile time. The key functions covered are malloc() for allocating memory blocks, calloc() for arrays and structures, realloc() for resizing allocated blocks, and free() for releasing used memory to avoid memory leaks. Examples are provided to demonstrate how each function is used.
1. Dynamic memory allocation allows programs to allocate memory as needed during runtime rather than having fixed memory allocations. This is done using dynamic data structures and memory management techniques.
2. Key memory management functions like malloc(), calloc(), free(), and realloc() allow programs to allocate, initialize, free, and modify the size of memory blocks dynamically.
3. Memory is allocated from the heap, located between the program instructions/global variables in permanent storage and local variables on the stack. The size of the heap changes as programs execute due to dynamic memory allocation.
1. Arrays declared with a fixed size limit the program size, while dynamically allocated arrays using heap memory allow the size to be determined at runtime.
2. The heap segment is used for dynamic memory allocation using functions like malloc() and new to request memory from the operating system as needed.
3. Deallocation of dynamically allocated memory is required using free() and delete to avoid memory leaks and ensure memory is returned to the operating system.
Dynamic memory allocation allows programs to request memory from the operating system at runtime. This memory is allocated on the heap. Functions like malloc(), calloc(), and realloc() are used to allocate and reallocate dynamic memory, while free() releases it. Malloc allocates a single block of uninitialized memory. Calloc allocates multiple blocks of initialized (zeroed) memory. Realloc changes the size of previously allocated memory. Proper use of these functions avoids memory leaks.
Dynamic memory allocation in C allows programs to manually allocate and free memory as needed using pointers. The malloc() function allocates a block of memory of a specified size and returns a pointer to it. Calloc() allocates multiple blocks of memory and initializes them to zero. Realloc() changes the size of an existing allocated block. Free() deallocates a block of memory to avoid memory leaks.
Dynamic memory allocation in C allows programs to manually allocate and free memory as needed using pointers. The malloc() function allocates a block of memory of a specified size and returns a pointer to it. Calloc() allocates multiple blocks of memory and initializes them to zero. Realloc() changes the size of an existing allocated block. Free() deallocates a block of memory to avoid memory leaks.
The document discusses dynamic memory allocation in C. Dynamic memory allocation allows memory to be allocated at runtime rather than compile-time. It uses functions like malloc(), calloc(), and free() to allocate and free memory blocks from the heap as needed during program execution. This provides flexibility over static allocation by allowing memory blocks to be variable in size and reallocated as needed.
Data Structure - Dynamic Memory Allocationbabuk110
The document discusses dynamic memory allocation in C. It explains that the amount of data needed is not always known beforehand, so static allocation of memory is inefficient. Dynamic allocation functions like malloc(), calloc(), free() and realloc() allow allocating memory at runtime based on the program's needs. Malloc allocates a block of memory and returns a pointer, while calloc initializes the memory to zero. Free releases previously allocated memory and realloc changes the size of allocated memory.
The document discusses the stack and heap memory in C programming. The stack is a region of memory that stores local variables and function calls. It uses a last-in, first-out approach and frees memory automatically. The heap is a dynamic memory region not tightly managed by the CPU. Functions like malloc(), calloc(), and free() must be used to allocate and free memory in the heap to avoid leaks. The heap does not free memory automatically.
A linked list is a linear data structure consisting of nodes that are connected to each other. Each node contains data and a pointer to the next node. The first node is referenced by the head pointer and the last node points to NULL. Linked lists can be singly, doubly, or circularly linked. A singly linked list is represented by a struct containing an integer data field and a pointer to the next node. Memory for nodes is dynamically allocated using functions like malloc() and freed using free().
The document discusses dynamic memory allocation in C using functions like malloc(), calloc(), realloc() and free(). It explains that malloc() allocates memory without initializing it, calloc() allocates memory and initializes it to zero, and realloc() resizes previously allocated memory. Examples are provided to demonstrate allocating memory for arrays, 2D arrays, structures and resizing memory. It emphasizes the importance of freeing unneeded memory to avoid memory leaks.
Dynamic memory allocation in C allows the size of data structures to be changed during program execution. The malloc() function allocates memory on the heap and returns a void pointer. Additional functions like calloc(), free(), and realloc() allow allocating and freeing contiguous blocks of memory and resizing allocated blocks. System calls like brk() and sbrk() are used to change the program break which marks the end of the heap.
This document discusses dynamic memory allocation in C. It explains that dynamic allocation allows memory to be allocated at runtime, unlike static allocation which requires defining memory sizes at compile time. The key functions covered are malloc() for allocating memory blocks, calloc() for arrays and structures, realloc() for resizing allocated blocks, and free() for releasing used memory to avoid memory leaks. Examples are provided to demonstrate how each function is used.
Dynamic memory allocation allows programs to dynamically allocate and free memory at runtime rather than having fixed-size arrays. Malloc allocates memory and leaves it uninitialized while calloc allocates and initializes memory to zero. Realloc can change the size of previously allocated memory. Free must be used to release dynamically allocated memory to avoid memory leaks. In C++, new allocates memory and returns a pointer while delete frees memory allocated by new.
Dynamic memory allocation in C allows memory to be allocated and freed at runtime. There are two types: static allocation assigns memory at compile time, while dynamic allocation uses functions like malloc(), calloc(), and realloc() to assign memory as needed at runtime. realloc() changes the size of an existing dynamically allocated block, and free() releases memory to be used again. Examples demonstrate allocating and accessing memory for arrays, structures and integers using these functions.
The document discusses dynamic memory allocation in C using functions like malloc(), calloc(), free(), and realloc(). It provides:
1) malloc() allocates memory at runtime and returns a pointer to the allocated block. It does not initialize the memory.
2) calloc() also allocates memory at runtime but initializes all bits to zero.
3) free() frees up previously allocated memory so it can be used again.
4) realloc() resizes previously allocated memory blocks, allowing memory usage to grow or shrink as needed.
The document provides an introduction to memory management in C. It discusses that memory can be allocated in C either by declaring variables, which allocates space on the stack, or by explicitly requesting space from C using functions like malloc(), which allocates space on the heap. The stack follows LIFO order and releases space automatically when the program finishes, while the heap requires manual memory management using free(). Functions like malloc() allocate raw memory on the heap and return a pointer to it, while calloc() allocates initialized memory in blocks. The document also discusses stack overflow and the different memory sections in RAM like text, data, BSS, and heap.
This document discusses dynamic memory allocation in C. It explains that dynamic allocation allows memory to be allocated at runtime, unlike static allocation which requires defining memory sizes at compile time. The key functions covered are malloc() for allocating memory blocks, calloc() for arrays and structures, realloc() for resizing allocated blocks, and free() for releasing used memory to avoid memory leaks. Examples are provided to demonstrate how each function is used.
1. Dynamic memory allocation allows programs to allocate memory as needed during runtime rather than having fixed memory allocations. This is done using dynamic data structures and memory management techniques.
2. Key memory management functions like malloc(), calloc(), free(), and realloc() allow programs to allocate, initialize, free, and modify the size of memory blocks dynamically.
3. Memory is allocated from the heap, located between the program instructions/global variables in permanent storage and local variables on the stack. The size of the heap changes as programs execute due to dynamic memory allocation.
1. Arrays declared with a fixed size limit the program size, while dynamically allocated arrays using heap memory allow the size to be determined at runtime.
2. The heap segment is used for dynamic memory allocation using functions like malloc() and new to request memory from the operating system as needed.
3. Deallocation of dynamically allocated memory is required using free() and delete to avoid memory leaks and ensure memory is returned to the operating system.
Dynamic memory allocation allows programs to request memory from the operating system at runtime. This memory is allocated on the heap. Functions like malloc(), calloc(), and realloc() are used to allocate and reallocate dynamic memory, while free() releases it. Malloc allocates a single block of uninitialized memory. Calloc allocates multiple blocks of initialized (zeroed) memory. Realloc changes the size of previously allocated memory. Proper use of these functions avoids memory leaks.
Dynamic memory allocation in C allows programs to manually allocate and free memory as needed using pointers. The malloc() function allocates a block of memory of a specified size and returns a pointer to it. Calloc() allocates multiple blocks of memory and initializes them to zero. Realloc() changes the size of an existing allocated block. Free() deallocates a block of memory to avoid memory leaks.
Dynamic memory allocation in C allows programs to manually allocate and free memory as needed using pointers. The malloc() function allocates a block of memory of a specified size and returns a pointer to it. Calloc() allocates multiple blocks of memory and initializes them to zero. Realloc() changes the size of an existing allocated block. Free() deallocates a block of memory to avoid memory leaks.
The document discusses dynamic memory allocation in C. Dynamic memory allocation allows memory to be allocated at runtime rather than compile-time. It uses functions like malloc(), calloc(), and free() to allocate and free memory blocks from the heap as needed during program execution. This provides flexibility over static allocation by allowing memory blocks to be variable in size and reallocated as needed.
The main purpose of the current study was to formulate an empirical expression for predicting the axial compression capacity and axial strain of concrete-filled plastic tubular specimens (CFPT) using the artificial neural network (ANN). A total of seventy-two experimental test data of CFPT and unconfined concrete were used for training, testing, and validating the ANN models. The ANN axial strength and strain predictions were compared with the experimental data and predictions from several existing strength models for fiber-reinforced polymer (FRP)-confined concrete. Five statistical indices were used to determine the performance of all models considered in the present study. The statistical evaluation showed that the ANN model was more effective and precise than the other models in predicting the compressive strength, with 2.8% AA error, and strain at peak stress, with 6.58% AA error, of concrete-filled plastic tube tested under axial compression load. Similar lower values were obtained for the NRMSE index.
AI-Powered Data Management and Governance in RetailIJDKP
Artificial intelligence (AI) is transforming the retail industry’s approach to data management and decisionmaking. This journal explores how AI-powered techniques enhance data governance in retail, ensuring data quality, security, and compliance in an era of big data and real-time analytics. We review the current landscape of AI adoption in retail, underscoring the need for robust data governance frameworks to handle the influx of data and support AI initiatives. Drawing on literature and industry examples, we examine established data governance frameworks and how AI technologies (such as machine learning and automation) are augmenting traditional data management practices. Key applications are identified, including AI-driven data quality improvement, automated metadata management, and intelligent data lineage tracking, illustrating how these innovations streamline operations and maintain data integrity. Ethical considerations including customer privacy, bias mitigation, transparency, and regulatory compliance are discussed to address the challenges of deploying AI in data governance responsibly.
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia
In the world of technology, Jacob Murphy Australia stands out as a Junior Software Engineer with a passion for innovation. Holding a Bachelor of Science in Computer Science from Columbia University, Jacob's forte lies in software engineering and object-oriented programming. As a Freelance Software Engineer, he excels in optimizing software applications to deliver exceptional user experiences and operational efficiency. Jacob thrives in collaborative environments, actively engaging in design and code reviews to ensure top-notch solutions. With a diverse skill set encompassing Java, C++, Python, and Agile methodologies, Jacob is poised to be a valuable asset to any software development team.
Design of Variable Depth Single-Span Post.pdfKamel Farid
Hunched Single Span Bridge: -
(HSSBs) have maximum depth at ends and minimum depth at midspan.
Used for long-span river crossings or highway overpasses when:
Aesthetically pleasing shape is required or
Vertical clearance needs to be maximized
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...ijdmsjournal
Agile methodologies have transformed organizational management by prioritizing team autonomy and
iterative learning cycles. However, these approaches often lack structured mechanisms for knowledge
retention and interoperability, leading to fragmented decision-making, information silos, and strategic
misalignment. This study proposes an alternative approach to knowledge management in Agile
environments by integrating Ikujiro Nonaka and Hirotaka Takeuchi’s theory of knowledge creation—
specifically the concept of Ba, a shared space where knowledge is created and validated—with Jürgen
Habermas’s Theory of Communicative Action, which emphasizes deliberation as the foundation for trust
and legitimacy in organizational decision-making. To operationalize this integration, we propose the
Deliberative Permeability Metric (DPM), a diagnostic tool that evaluates knowledge flow and the
deliberative foundation of organizational decisions, and the Communicative Rationality Cycle (CRC), a
structured feedback model that extends the DPM, ensuring long-term adaptability and data governance.
This model was applied at Livelo, a Brazilian loyalty program company, demonstrating that structured
deliberation improves operational efficiency and reduces knowledge fragmentation. The findings indicate
that institutionalizing deliberative processes strengthens knowledge interoperability, fostering a more
resilient and adaptive approach to data governance in complex organizations.
この資料は、Roy FieldingのREST論文(第5章)を振り返り、現代Webで誤解されがちなRESTの本質を解説しています。特に、ハイパーメディア制御やアプリケーション状態の管理に関する重要なポイントをわかりやすく紹介しています。
This presentation revisits Chapter 5 of Roy Fielding's PhD dissertation on REST, clarifying concepts that are often misunderstood in modern web design—such as hypermedia controls within representations and the role of hypermedia in managing application state.
Introduction to ANN, McCulloch Pitts Neuron, Perceptron and its Learning
Algorithm, Sigmoid Neuron, Activation Functions: Tanh, ReLu Multi- layer Perceptron
Model – Introduction, learning parameters: Weight and Bias, Loss function: Mean
Square Error, Back Propagation Learning Convolutional Neural Network, Building
blocks of CNN, Transfer Learning, R-CNN,Auto encoders, LSTM Networks, Recent
Trends in Deep Learning.
This research presents the optimization techniques for reinforced concrete waffle slab design because the EC2 code cannot provide an efficient and optimum design. Waffle slab is mostly used where there is necessity to avoid column interfering the spaces or for a slab with large span or as an aesthetic purpose. Design optimization has been carried out here with MATLAB, using genetic algorithm. The objective function include the overall cost of reinforcement, concrete and formwork while the variables comprise of the depth of the rib including the topping thickness, rib width, and ribs spacing. The optimization constraints are the minimum and maximum areas of steel, flexural moment capacity, shear capacity and the geometry. The optimized cost and slab dimensions are obtained through genetic algorithm in MATLAB. The optimum steel ratio is 2.2% with minimum slab dimensions. The outcomes indicate that the design of reinforced concrete waffle slabs can be effectively carried out using the optimization process of genetic algorithm.
David Boutry - Specializes In AWS, Microservices And PythonDavid Boutry
With over eight years of experience, David Boutry specializes in AWS, microservices, and Python. As a Senior Software Engineer in New York, he spearheaded initiatives that reduced data processing times by 40%. His prior work in Seattle focused on optimizing e-commerce platforms, leading to a 25% sales increase. David is committed to mentoring junior developers and supporting nonprofit organizations through coding workshops and software development.
2. Dynamic Memory Allocation (DMA)
Since C is a structured language, it has some fixed rules for
programming.
One of it includes changing the size of an array. An array is
collection of items stored at continuous memory locations.
As it can be seen that the length (size) of the array above made is
9. But what if there is a requirement to change this length (size).
For Example,
3. Dynamic Memory Allocation (DMA)
If there is a situation where only 5 elements are needed to be
entered in this array. In this case, the remaining 4 indices are just
wasting memory in this array. So there is a requirement to lessen
the length (size) of the array from 9 to 5.
Take another situation. In this, there is an array of 9 elements with
all 9 indices filled. But there is a need to enter 3 more elements in
this array. In this case 3 indices more are required. So the length
(size) of the array needs to be changed from 9 to 12.
This procedure is referred to as Dynamic Memory Allocation in C.
4. Dynamic Memory Allocation (DMA)
Therefore, C Dynamic Memory Allocation can be defined as a
procedure in which the size of a data structure (like Array) is
changed during the runtime.
C provides some functions to achieve these tasks. There are 4
library functions provided by C defined under <stdlib.h> header
file to facilitate dynamic memory allocation in C programming.
They are:
• malloc()
• calloc()
• free()
• realloc()
5. Dynamic Memory Allocation (DMA)
If memory is allocated at runtime (during execution of program)
then it is called dynamic memory.
It allocates memory from heap (heap: it is an empty area in
memory)
Memory can be accessed only through a pointer.
When DMA is needed?
It is used when number of variables are not known in advance or
large in size.
Memory can be allocated at any time and can be released at any
time during runtime.
6. malloc() function
malloc () is used to allocate a fixed amount of memory during
the execution of a program.
malloc () allocates size_in_bytes of memory from heap,
if the allocation succeeds, a pointer to the block of memory is
returned else NULL is returned.
Allocated memory space may not be contiguous.
Each block contains a size, a pointer to the next block, and the
space itself.
The blocks are kept in ascending order of storage address, and the
last block points to the first.
The memory is not initialized. It initializes each block with default
garbage value.
7. malloc() function
Syntax Description
ptr_var = (cast_type *
) malloc
(size_in_bytes);
This statement returns a pointer to size_in_bytes
of uninitialized storage, or NULL if the request cannot be
satisfied.
Example: fp = (int *)malloc(sizeof(int)
*20);
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory.
And, the pointer ptr holds the address of the first byte in the allocated memory.
Example ::
8. Example(Pointer1.c)
#include <stdio.h>
#include <stdlib.h>
int main()
{
// This pointer will hold the
// base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the array
n = 5;
printf("Enter number of elements: %dn", n);
// Dynamically allocate memory using
malloc()
ptr = (int*)malloc(n * sizeof(int));
// Check if the memory has been successfully
// allocated by malloc or not
if (ptr == NULL) {
printf("Memory not allocated.n");
exit(0);
}
else {
// Memory has been successfully allocated
printf("Memory successfully
allocated using malloc.n");
// Get the elements of the array
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
// Print the elements of the array
printf("The elements of the array
are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
return 0;
}
9. calloc() function
“calloc” or “contiguous allocation” method in C is used to
dynamically allocate the specified number of blocks of memory of
the specified type.
calloc() allocates a region of memory to hold
no_of_blocks of size_of_block each, if the allocation
succeeds then a pointer to the block of memory is returned else
NULL is returned.
It initializes each block with a default value ‘0’.
10. calloc() function
Syntax Description
ptr_var = (cast_type *
) calloc
(no_of_blocks,
size_of_block);
This statement returns a pointer to no_of_blocks of
size size_of_blocks, it returns NULL if the request
cannot be satisfied.
Example:
int n = 20;
fp = (int *)calloc(n, sizeof(int));
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size
of the float.
Example ::
11. Example(calloc1.c)
#include <stdio.h>
#include <stdlib.h>
int main()
{
// This pointer will hold the
// base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the
array
n = 5;
printf("Enter number of elements: %dn",
n);
// Dynamically allocate memory using
calloc()
ptr = (int*)calloc(n, sizeof(int));
// Check if the memory has been
successfully
// allocated by calloc or not
if (ptr == NULL) {
printf("Memory not allocated.n");
exit(0);
}
else {
// Memory has been successfully
allocated
printf("Memory successfully
allocated using calloc.n");
// Get the elements of the array
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
// Print the elements of the
array
printf("The elements of the array
are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
return 0;
12. free() method
“free” method in C is used to dynamically de-allocate the
memory.
The memory allocated using functions malloc() and calloc() is not
de-allocated on their own.
Hence the free() method is used, whenever the dynamic memory
allocation takes place.
It helps to reduce wastage of memory by freeing it.
14. Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
// This pointer will hold the
// base address of the block created
int *ptr, *ptr1;
int n, i;
// Get the number of elements for the
array
n = 5;
printf("Enter number of elements: %dn",
n);
// Dynamically allocate memory using
malloc()
ptr = (int*)malloc(n * sizeof(int));
// Dynamically allocate memory using
calloc()
ptr1 = (int*)calloc(n, sizeof(int));
// Check if the memory has been
successfully
// allocated by malloc or not
if (ptr == NULL || ptr1 == NULL) {
else {
// Memory has been successfully
allocated
printf("Memory successfully
allocated using malloc.n");
// Free the memory
free(ptr);
printf("Malloc Memory
successfully freed.n");
// Memory has been successfully
allocated
printf("nMemory successfully
allocated using calloc.n");
// Free the memory
free(ptr1);
printf("Calloc Memory
successfully freed.n");
}
return 0;
}
15. realloc() method
“realloc” or “re-allocation” method in C is used to dynamically
change the memory allocation of a previously allocated memory.
In other words, if the memory previously allocated with the help
of malloc or calloc is insufficient, realloc can be used
to dynamically re-allocate memory.
re-allocation of memory maintains the already present value and
new blocks will be initialized with default garbage value.
Syntax Description
ptr_var = (cast_type
*) realloc (void *fp,
size_t);
This statement returns a pointer to new space, or NULL if
the request cannot be satisfied.
Example:
fp = (int *)realloc(fp,sizeof(int)*20);
16. Example ::
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.
realloc() method