Thread: Malloc how unsuccesfully (not allocating memory)

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    9

    Malloc how unsuccesfully (not allocating memory)

    Bellow I show some code that I got from a tutorial.

    I am interested in understanding what are some changes that I can make to this code so that I will end up in the block:

    Code:
    if (ptr == NULL) { printf("Memory not allocated.\n"); exit(0);
    The code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    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 
        n = 5;
        printf("Enter number of elements: %d\n", 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]);
            }
        }
    }
    So I don't want to receive the message: "Memory successfully allocated using malloc", but "Memory not allocated"

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You might be able to enter that branch by changing n to be a sufficiently large number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc not allocating anything.
    By Jozef in forum C Programming
    Replies: 14
    Last Post: 11-24-2015, 05:20 PM
  2. Regarding allocating memory
    By monkey_c_monkey in forum C++ Programming
    Replies: 2
    Last Post: 08-29-2012, 10:06 PM
  3. malloc not actually allocating right amount of memory?
    By somerandomdude in forum C Programming
    Replies: 4
    Last Post: 10-02-2011, 12:43 PM
  4. allocating memory with malloc
    By steve1_rm in forum C++ Programming
    Replies: 32
    Last Post: 12-18-2008, 06:51 PM
  5. allocating memory
    By viaxd in forum C Programming
    Replies: 2
    Last Post: 10-12-2003, 07:13 PM

Tags for this Thread