Memory Handling in c & c++
-
Upload
sreekarscribd -
Category
Documents
-
view
219 -
download
0
Transcript of Memory Handling in c & c++
-
8/9/2019 Memory Handling in c & c++
1/16
IN
C / C++
-
8/9/2019 Memory Handling in c & c++
2/16
ABSTRACTy Gives knowledge about memory handling functions in
c & c++
y
Includes syntax and examples which makes easier tounderstand and easy to program
y Functions explained are
y C C++
Malloc newCalloc delete
Realloc
Free
-
8/9/2019 Memory Handling in c & c++
3/16
MALLOC
y malloc allocates a block of size bytes from the memoryheap. It allows a program to allocate memory explicitly
as it's needed, and in the exact amounts needed.y The heap is used for dynamic allocation of variable-
sized blocks of memory.
y SYNTAX :
yvoid *malloc(size_t size);
-
8/9/2019 Memory Handling in c & c++
4/16
y Example:y #include y #include y #include y #include
y int main(void)y {y char *str;
y /* allocate memory for string */y if ((str = (char *) malloc(10)) == NULL)y {
y printf("Not enough memory to allocate buffer\n");y exit(1); /* terminate program if out of memory */y }
y /* copy "Hello" into string */y strcpy(str, "Hello");
y
/* display string */y printf("String is %s\n", str);
y /* free memory */y free(str);
y return 0;y }
-
8/9/2019 Memory Handling in c & c++
5/16
CALLOC
y calloc provides access to the C memory heap, which isavailable for dynamic
y allocation of variable-sized blocks of memory.
y SYNTAX :
yvoid *calloc(size_t nitems, size_t size);
-
8/9/2019 Memory Handling in c & c++
6/16
y Example:
y #include y #include y #include
y int main(void)y {y char *str = NULL;
y /* allocate memory for string */y str = (char *) calloc(10, sizeof(char));
y /* copy "Hello" into string */y strcpy(str, "Hello");
y /* display string */
y printf("String is %s\n", str);
y /* free memory */y free(str);
y return 0;
y }
-
8/9/2019 Memory Handling in c & c++
7/16
FREE
y free deallocates a memory block allocated by a
previous call to calloc,y malloc, or realloc.
y SYNTAX :
yvoid free(void *block);
-
8/9/2019 Memory Handling in c & c++
8/16
y free example
y #include
y #include
y #include
y int main(void)
y {
y char *str;
y /* allocate memory for string */
y str = (char *) malloc(10);
y /* copy "Hello" to string */
y strcpy(str, "Hello");
y /* display string */
y printf("String is %s\n", str);
y /* free memory */
y free(str);
y return 0;
y }
-
8/9/2019 Memory Handling in c & c++
9/16
REALLOC
y realloc adjusts the size of the allocated block to size,
copying they contents to a new location if necessary.
y SYNTAX :
y void *realloc(void *block, size_t size);
-
8/9/2019 Memory Handling in c & c++
10/16
y reallocexample
y #include y #include y
#include
y int main(void)y {y char *str;
y /* allocate memory for string */y
str = (char *) malloc(10);
y /* copy "Hello" into string */y strcpy(str, "Hello");
y printf("String is %s\n Address is %p\n", str, str);y str = (char *) realloc(str, 20);
y printf("String is %s\n New address is %p\n", str, str);
y /* free memory */y free(str);
y return 0;y }
-
8/9/2019 Memory Handling in c & c++
11/16
MEMCPY
y these functions copies a block of n bytes from src to
dest.yWith memcpy, if src and dest overlap, the behavior is
undefined.
y SYNTAX :yvoid *memcpy (void *dest, const void *src, size_t n);
-
8/9/2019 Memory Handling in c & c++
12/16
y memcpy example
y #include y #include
y int main(void)y {y char src[] = "******************************";y char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";y char *ptr;
y printf("destination before memcpy: %s\n", dest);y ptr = (char *) memcpy(dest, src, strlen(src));y if (ptr)y printf("destination after memcpy: %s\n", dest);y elsey printf("memcpy failed\n");y return 0;y }
-
8/9/2019 Memory Handling in c & c++
13/16
MEMSET
y memset sets the first n bytes of the array s to thecharacter c.
y SYNTAX :
yvoid *memset (void *s, int c, size_t n);
-
8/9/2019 Memory Handling in c & c++
14/16
y Example:
y #include
y #include y #include
y int main(void)
y {
y char buffer[] = "Hello world\n";
y printf("Buffer before memset: %s\n", buffer);
y memset(buffer, '*', strlen(buffer) - 1);
y printf("Buffer after memset: %s\n", buffer);
y return 0;
y
}
-
8/9/2019 Memory Handling in c & c++
15/16
NEW & DELETEy Operators that create and destroy an object
y Syntax:y
= new [ ];y delete ;
y The "new" operator tries to create an object by allocatingy sizeof() bytes in the heap.
y The "delete" operator destroys the object by deallocatingy sizeof() bytes (pointed to by ).
y The storage duration of the new object is from the point of creation untily the operator "delete" deallocates its memory, or until the end of they program.
-
8/9/2019 Memory Handling in c & c++
16/16
y Example:
y name *nameptr; // name is any non-function type
y ...
y if (!(nameptr = new name)) {
y errmsg("Insufficient memory for name");
y exit (1);
y }y // Use *nameptr to initialize new name object
y ...
y delete nameptr; //destroy name; deallocatesizeof(name) bytes