-1

I saw a tutorial where the instructor dynamically allocates memory for n*int size (n is not known in advance, user would give it as an input). Just out of curiosity, I have changed the code from calloc(n,sizeof(int)) to calloc(1,sizeof(int)) and I was expecting to see an error, but I did not face an error, and the code runs smoothly.

If I increment the pointer and it just continues without a problem, why should I use anything else but calloc(1,sizeof(int))?

#include <iostream>

using namespace std;

int main(){

    int n;
    printf("enter the size of array\n");
    scanf("%d",&n);
    int* A = (int*)calloc(1,sizeof(int));

    for(int i=0; i<n; i++){

        *(A+i) = i+1;
    
    }

    for(int i=0; i<n; i++){

        printf("%d\n", A[i]);
        printf("%d\n", &A[i]);
    }

    // free(A)

    return 0;
}
harman
  • 5
  • 3
  • Show the code with your update. – Vlad from Moscow Mar 04 '22 at 19:19
  • Your code exhibits undefined behavior. – drescherjm Mar 04 '22 at 19:20
  • Since you are using C++, prefer to use operator `new` when allocating memory. The `calloc` and `malloc` functions don't call `struct` or `class` constructors. – Thomas Matthews Mar 04 '22 at 19:20
  • Also, prefer to use `std::vector`. Let the `std::vector` manage memory for you. – Thomas Matthews Mar 04 '22 at 19:21
  • 2
    The mistake is to assume c++ produces errors when your code contains mistakes. For some kinds of mistakes, it is required to produce an error, usually during compilation. But most of the time it may lead to a crash with a useful error message, may lead to the program behaving strangely later, may lead to no observable problems or may lead to anything else, potentially something nobody has yet imagined. And this can change at any time for no obvious reason. – François Andrieux Mar 04 '22 at 19:23
  • 1
    *and I was expecting to see an error but I did not face an error and code runs smoothly.* -- "I was expecting the rope that can only hold 500 pounds to break when I use it to hold 600 pounds, but it didn't. Everything ran smoothly..." -- That is basically what you're doing. The rope may break tomorrow, may never break, break right away, etc. You don't know, as you've gone past what was "defined" and entered into "undefined" land. – PaulMcKenzie Mar 04 '22 at 19:26
  • 1
    "I saw a tutorial where the instructor": The tutorial was probably teaching C. These are different languages and you should not learn C++ from C code. If it really was teaching C++, then it is a really bad tutorial and you should look to learn from somewhere else, for example [these recommended books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user17732522 Mar 04 '22 at 20:01

1 Answers1

2

I was expecting to see an error

Your expectation was misguided. If you access outside the region of allocated storage, then the behaviour of the program is undefined. You aren't guaranteed to get an error.

Don't access memory outside of bounds. Avoid undefined behaviour. It's very bad. The program is broken.


Other advice: Avoid using calloc in C++. Avoid using using namespace std;.

eerorika
  • 232,697
  • 12
  • 197
  • 326