The document discusses dynamic memory allocation in C and C++. It describes functions like malloc(), calloc(), free(), and realloc() for allocating and freeing memory dynamically. It also covers the new and delete operators for dynamic memory allocation in C++. Issues like dangling pointers and memory leaks that can occur due to improper use of dynamic memory are explained.
(5) cpp dynamic memory_arrays_and_c-stringsNico Ludwig
Check out these exercises: http://de.slideshare.net/nicolayludwig/5-cpp-dynamic-memoryarraysandcstringsexercises
- The Heap: Dynamic Memory and dynamic Array Allocation
- Automatic versus Dynamic Arrays
- A Glimpse of the Topic "Stack versus Heap"
-- "Geometric" Properties of the Heap and the Stack
- Lost Pointers and Memory Leaks
- Advanced C-strings: Buffers, Concatenation and Formatting
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.
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
After go through this ppt the learners could be able to know the c programming concepts like dynamic memory allocation, pointers and functions and pointers to structures with examples.
A Strategic Approach: GenAI in EducationPeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
More Related Content
Similar to PF UE LEC 7 Pointers programming fundamentals (2).pptx
(5) cpp dynamic memory_arrays_and_c-stringsNico Ludwig
Check out these exercises: http://de.slideshare.net/nicolayludwig/5-cpp-dynamic-memoryarraysandcstringsexercises
- The Heap: Dynamic Memory and dynamic Array Allocation
- Automatic versus Dynamic Arrays
- A Glimpse of the Topic "Stack versus Heap"
-- "Geometric" Properties of the Heap and the Stack
- Lost Pointers and Memory Leaks
- Advanced C-strings: Buffers, Concatenation and Formatting
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.
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
After go through this ppt the learners could be able to know the c programming concepts like dynamic memory allocation, pointers and functions and pointers to structures with examples.
A Strategic Approach: GenAI in EducationPeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
Unit 8 - Information and Communication Technology (Paper I).pdfThiyagu K
This slides describes the basic concepts of ICT, basics of Email, Emerging Technology and Digital Initiatives in Education. This presentations aligns with the UGC Paper I syllabus.
A workshop hosted by the South African Journal of Science aimed at postgraduate students and early career researchers with little or no experience in writing and publishing journal articles.
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
Synthetic fiber production is a fascinating and complex field that blends chemistry, engineering, and environmental science. By understanding these aspects, students can gain a comprehensive view of synthetic fiber production, its impact on society and the environment, and the potential for future innovations. Synthetic fibers play a crucial role in modern society, impacting various aspects of daily life, industry, and the environment. ynthetic fibers are integral to modern life, offering a range of benefits from cost-effectiveness and versatility to innovative applications and performance characteristics. While they pose environmental challenges, ongoing research and development aim to create more sustainable and eco-friendly alternatives. Understanding the importance of synthetic fibers helps in appreciating their role in the economy, industry, and daily life, while also emphasizing the need for sustainable practices and innovation.
Macroeconomics- Movie Location
This will be used as part of your Personal Professional Portfolio once graded.
Objective:
Prepare a presentation or a paper using research, basic comparative analysis, data organization and application of economic information. You will make an informed assessment of an economic climate outside of the United States to accomplish an entertainment industry objective.
2. Dynamic memory allocation
• Dynamic memory allocation in C/C++ refers to performing memory
allocation manually by programmer.
• The most important use is flexibility provided to programmers. We
are free to allocate and deallocate memory whenever we need and
whenever we don’t need anymore.
3. Dynamic memory allocation
• For normal variables like “int a”, “char str[10]”, etc, memory is
automatically allocated and deallocated. For dynamically allocated
memory it is programmers responsibility to deallocate memory when
no longer needed.
4. malloc- C function supported in C++
• “malloc” or “memory allocation” method in C is used to dynamically
allocate a single large block of memory with the specified size. It returns a
pointer of type void which can be cast into a pointer of any form.
• ptr = (cast-type*) malloc(byte-size)
• 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.
• Header file: cstdlib
5. int main()
{
// This pointer will hold the base address of the block
created
int* ptr;
int n, i, sum = 0;
// Get the number of elements for the array
cin>>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) {
cout<<"Memory not allocated.n";
exit();
}
else {
// Memory has been successfully allocated
cout<<"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
cout<<"The elements of the array are: ";
for (i = 0; i < n; ++i) {
cout<<ptr[i];
}
}
return 0;
}
6. Calloc
• “calloc” or “contiguous allocation” method in C is used to
dynamically allocate the specified number of blocks of memory of the
specified type. It initializes each block with a default value ‘0’.
• ptr = (cast-type*)calloc(n, element-size);
• ptr = (float*) calloc(25, sizeof(float));
• This statement allocates contiguous space in memory for 25 elements
each with the size of the float
7. free
• “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.
• free(ptr);
8. realloc
• “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.
• ptr = realloc(ptr, newSize); where ptr is
reallocated with new size 'newSize'.
// use: taking array size again from user in a program
9. new operator
• The new operator denotes a request for memory allocation. If
sufficient memory is available, new operator initializes the memory
and returns the address of the newly allocated and initialized memory
to the pointer variable.
pointer-variable = new data-type;
int *p = NULL; p = new int;
int *p = new int;
10. Initialize the memory
• pointer-variable = new data-type(value);
• Example: int *p = new int(25); float *q = new
float(75.25);
11. Allocate block of memory
• new operator is also used to allocate a block(an array) of memory of
type data-type.
• pointer-variable = new data-type[size];
• Example: int *p = new int[10]
Dynamically allocates memory for 10 integers continuously of type int
and returns pointer to the first element of the sequence, which is
assigned to p(a pointer). p[0] refers to first element, p[1] refers to
second element and so on.
12. delete operator
• // Release memory pointed by pointer-variable delete
pointer-variable;
• Here, pointer-variable is the pointer that points to the data object created
by new.
delete p;
To free the dynamically allocated array pointed by pointer-variable, use
following form of delete:
// Release block of memory pointed by pointer-variable
delete[] pointer-variable;
Example: // It will free the entire array pointed by p.
delete[] p;
13. // C++ program to illustrate dynamic allocation
// and deallocation of memory using new and delete
#include <iostream>
using namespace std;
int main ()
{
// Pointer initialization to null
int* p = NULL;
// Request memory for the variable using new
operator
p = new int;
if (p==NULL)
cout << "allocation of memory failedn";
else
{
// Store value at allocated address
*p = 29;
cout << "Value of p: " << *p << endl;
}
// Request block of memory using new operator
float *r = new float(75.25);
cout << "Value of r: " << *r << endl;
// Request block of memory of size n
int n = 5;
int *q = new int[n];
if (q==NULL)
cout << "allocation of memory failedn";
else
{ for (int i = 0; i < n; i++)
q[i] = i+1;
cout << "Value store in block of memory: ";
for (int i = 0; i < n; i++)
cout << q[i] << " ";
}
// freed the allocated memory
delete p;
delete r;
// freed the block of allocated memory
delete[] q;
return 0; }
14. Dangling pointer
• A situation when a pointer points to the memory location of the
deallocated memory. It is important to make a pointer NULL after de-
allocating its allocated memory.
• Example of that situation could be when two pointers pointing to
same memory location and one deallocates that memory.
15. Memory leak
• A memory leak occurs in C++ when you allocate memory dynamically
and never give it back.
• The memory leak occurs, when a piece of memory which was
previously allocated by the programmer. Then it is not deallocated
properly by programmer. That memory is no longer in use by the
program. So that place is reserved for no reason. That’s why this is
called the memory leak.
• For the memory leak, some block of memory may have wasted. If the
system has enough memory, in that case also this may slow down the
performance.
16. References
• C++ How to Program
By Deitel & Deitel
• The C++ Programming Language
By Bjarne Stroustrup
• Object oriented programming using C++ by Tasleem Mustafa, Imran Saeed, Tariq Mehmood, Ahsan Raza
• https://www.tutorialspoint.com/cplusplus
• http://ecomputernotes.com/cpp/introduction-to-oop
• http://www.cplusplus.com/doc/tutorial
• http://www.c4learn.com/c-programming/c-dangling-pointer-causes/
• https://www.guru99.com/c-loop-statement.html
• www.w3schools.com
• https://www.geeksforgeeks.org/