0

I have a very simple program to demonstrate the use of malloc function in C. I have allocated the size needed to store just one integer and stored the returned pointer in variable ptr.

The problem is, although I have set the malloc size for one integer (4 bytes in my PC), the code runs fine for even a large number of integers (int x = 95;) when printf("%u\n", ptr + j); is uncommented. But it gives an error when printf("%u\n", ptr + j); is commented. Maybe it creates some adjacent memory block when trying to access for printf or something.

I think this has got to do something with the memory heap being used or not, but being a beginner in this, I would like an explanation for this.

Thank you.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr;
    int x = 95;

    ptr = (int *) malloc(sizeof(int));
    for (int j = 0; j < x; ++j) {
        *(ptr + j) = 3;
        //printf("%u\n", ptr + j);
    }

    for (int i = 0; i < x; ++i) {
        printf("%d\n", *(ptr + i));
    }

    return 0;
}
RukshanJS
  • 791
  • 1
  • 7
  • 20
  • `The problem is, although I have set the malloc size for one integer (4 bytes in my PC), the code runs fine for even a large number of integers` The problem _isn't_ that it works (seems to work) but that you're using memory which you didn't allocate. – tkausl Oct 11 '20 at 21:21
  • @tkausl I guess so.. but why so tho? – RukshanJS Oct 11 '20 at 21:26
  • See duplicate post. Accessing invalid memory is Undefined Behaviour. UB means the result is unpredictable - it can appear to "work", it can crash, it can produce wrong results, and any other behaviour. – kaylum Oct 11 '20 at 21:31

3 Answers3

1

In your example, you allocate memory for one integer, and you are trying to write 95 integers. You should always allocate enough space. In this case, you should do a malloc(sizeof(int) * x); and then fill that space up with your numbers :)

Gecal
  • 31
  • 3
  • Thanks for the answer. Exactly, but why does the code work when the printf line (which prints the addresses) is there and doesn't work when it's commented? Seems to me that there's an underlying reason. – RukshanJS Oct 11 '20 at 21:47
  • 1
    @stackerRook Asking "why does the code work when (I break the rules of not allocating enough memory and writ to memory I do not have)" is like asking why breaking a civil law is not always caught by the police. – chux - Reinstate Monica Oct 11 '20 at 22:56
  • Pretty much what Chux said. If I recall correct, when your program is loaded in to memory, your OS knows that it will require a specific amount of memory, and allocates enough pages to fit it. If you need extra space you got the heap, and you got the stack, but both of them are given by the OS when required so. If they have not been required yet, thus not given yet, and you try to access such memory, then the OS wont allow you to access it because it is memory you have no permissions on yet and raise a SIGSEG fault, effectively terminating your program – Gecal Oct 12 '20 at 09:17
1

If you are trying to print the addresses change the %u to %p in your print statement.

Dharman
  • 30,962
  • 25
  • 85
  • 135
noahboa
  • 11
  • 4
1

Your code invokes undefined behaviour (in one of its worst possible forms, overwriting memory, which is the root cause of many vulnerabilities in software).

"Undefined behaviour" means anything can happen. Absolutely anything. You are asking why the result changes when your code is changed: Because you have undefined behaviour, and anything can happen. Different things can happen when you don't see any good reason for different things to happen. Whatever you assume, if your code has undefined behaviour, you are WRONG.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Thanks and that explains a lot. So, if I got it correctly, in order for the program to be correct, I should only set one integer to *ptr for the given malloc code, am I right? – RukshanJS Oct 11 '20 at 22:31