Question

Even if we call the correct number of malloc’s and free’s, all the dynamically allocated memory...

Even if we call the correct number of malloc’s and free’s, all the dynamically allocated memory may not be released. Discuss three possible reasons for this.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In C, the exact size of array is unknown until compile time, i.e., the time when a compiler compiles your code into a computer understandable language. So, sometimes the size of the array can be insufficient or more than required.

Dynamic memory allocation allows your program to obtain more memory space while running, or to release it if it's not required.

In simple terms, Dynamic memory allocation allows you to manually handle memory space for your program.

Although, C language inherently does not have any technique to allocate memory dynamically, there are 4 library functions under "stdlib.h" for dynamic memory allocation.

Function Use of Function
malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space
calloc() Allocates space for an array elements, initializes to zero and then returns a pointer to memory
free() deallocate the previously allocated space
realloc() Change the size of previously allocated space

C malloc()

The name malloc stands for "memory allocation".

The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form.

Syntax of malloc()

ptr = (cast-type*) malloc(byte-size)

Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.

ptr = (int*) malloc(100 * sizeof(int));

This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory.

C calloc()

The name calloc stands for "contiguous allocation".

The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.

Syntax of calloc()

ptr = (cast-type*)calloc(n, element-size);

This statement will allocate contiguous space in memory for an array of nelements. For example:

ptr = (float*) calloc(25, sizeof(float));

This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes.

C free()

Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own. You must explicitly use free() to release the space.

syntax of free()

free(ptr);
example 
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num, i, *ptr, sum = 0;

    printf("Enter number of elements: ");
    scanf("%d", &num);

    ptr = (int*) malloc(num * sizeof(int));  //memory allocated using malloc
    if(ptr == NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(0);
    }

    printf("Enter elements of array: ");
    for(i = 0; i < num; ++i)
    {
        scanf("%d", ptr + i);
        sum += *(ptr + i);
    }

    printf("Sum = %d", sum);
    free(ptr);
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Even if we call the correct number of malloc’s and free’s, all the dynamically allocated memory...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • c++ question MyClass has an internal 2-D array of dynamically allocated doubles, pointed to by the...

    c++ question MyClass has an internal 2-D array of dynamically allocated doubles, pointed to by the member named data: class MyClass { private: double **data; int width, height; // other stuff } Assume width and height are set by constructors and mutators, and the class deals with them and all other data management correctly. Here is the method we are interested in analyzing: void MyClass::allocateDynArray(int newHeight, int newWidth) { int row; if ( !valid( newHeight, newWidth ) ) return; height...

  • Let EE be the set of all positive even integers. We call a number e∈Ee∈E "eprime"...

    Let EE be the set of all positive even integers. We call a number e∈Ee∈E "eprime" if ee cannot be expressed as a product of two other members of EE. a) Give an example of an eprime that is greater than 100 and has two different eprime factorizations. b) Are the eprimes dense in E? c) What is the proportion of eprimes in EE? (meaning that if I were to take a random, contiguous, really large set of values in...

  • Both questions #1(1-6) and #2 Question t. A die was rolled twice. The first time it landed on a number that we will call “x". The second time it landed on a number that we will call "y&#34...

    Both questions #1(1-6) and #2 Question t. A die was rolled twice. The first time it landed on a number that we will call “x". The second time it landed on a number that we will call "y" We will symbolize some events as follows: x is odd. B x is a prime. γ¡s odd. A: : D: y is a prime. that x andy are both odd may be expressed as P(АЛ C). (1) The chance that x is...

  • Call stack question! Long one... The call stack is part of main memory that is reserved...

    Call stack question! Long one... The call stack is part of main memory that is reserved for function calling. Like all r memory it is finite, so can be exhausted resulting in a stack overflow. Recursive functions allocate space on the stack for each recursive call: if there are many such recursive calls a stack overflow can result. The questions that follow ask you to investigate recursive functions and stack overflows. Note that when running programs in the Linux terminal...

  • Implement a C program unique.c that recreates the functionality of the uniq tool by reading the...

    Implement a C program unique.c that recreates the functionality of the uniq tool by reading the input line by line and dropping each l ine that is identical to the line immediately before it. On the input abc abc abc your program should output abc abc the same output that uniq would produce. While not strictly necessary for this step, it is important as a basis for subsequent steps that you read the entire input and store it as a...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Suppose that a seller has one item, which we will call item x. There are three...

    Suppose that a seller has one item, which we will call item x. There are three buyers, whom we will call A, B, and C. The values that these buyers (A, B, and C) have for the item are 6, 3, and 1, respectively. (a) Suppose that the seller runs a second price auction for the item. Which buyer will win the auction and how much will this buyer pay? Make it clear what are the bids. (b) Now let...

  • Suppose that a seller has one item, which we will call item x. There are three buyers, whom we wi...

    Suppose that a seller has one item, which we will call item x. There are three buyers, whom we will call A, B, and C. The values that these buyers (A, B, and C) have for the item are 6, 3, and 1, respectively. (a) Suppose that the seller runs a second price auction for the item. Which buyer will win the auction and how much will this buyer pay? Make it clear what are the bids. (b) Now let...

  • Implement the histogram function to complete the desired program. You must use dynamically allocated arrays for...

    Implement the histogram function to complete the desired program. You must use dynamically allocated arrays for this purpose. For your initial implementation, use ordered insertion to keep the words in order and ordered sequential search when looking for words. Note that the array utility functions from the lecture notes are available to you as art of the provided code. Although we are counting words in this program, the general pattern of counting occurrences of things is a common analysis step...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT