0

The title may not be accurate. Please excuse me for being a completely new programmer in c. But it is a genuine question which I believe will benefit others who were as confused by memory and pointers as I was when learning my first low-level programming language, that is C.

Here is what I know in regard to this:

  • Pointers are variables that store memory addresses.
  • You can allocate a place in memory using the malloc function from the stdlib.h header file, which returns a pointer to the memory allocated.
  • The malloc function takes the size of what you want to store in bytes as a parameter

Which leads me to ask: What if you store something of a bigger size in the place in memory allocated by the malloc function, where you passed a smaller size as the parameter for the malloc function?

Naturally, the first thing I did was obviously try it. I take input using scanf, which then stores the input in the allocated memory. Here is the code:

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

int main(){
    char *string_pointer;

    string_pointer = malloc(sizeof(char)*24);
    if (string_pointer == NULL){
        puts("Memory allocation failed:(");
        return 1;
    }else{}

    scanf("%s",string_pointer);

    printf("%s",string_pointer);

    return 0;
}

You can see that I allocated a place in memory, passing in sizeof(char)*24 as the parameter. Then I stored the pointer of this memory in the string_pointer variable.

Now if I feed scanf with a string that is more than 24 characters (bigger size than the allocated memory), it still works, When I print out the contents of the memory, I get the whole string as it is, even though that means that it stored a string of a bigger size than what it can hold. This shows that I have an obvious misconception of how memory allocation works. It might be because that malloc doesn't allocate a memory that can only hold the size that I passed to malloc, and the whole size parameter thing is for a totally different purpose.

I am completely confused? How come I just stored a string in a memory allocated that can hold less than the size of the string?

  • 3
    "it still works" No. You just do not notice what got broken in some situations. Use you favorite search engine on the keywords "segmentation fault", "undefined behaviour", "nasal demons". Note that "nasal demons" is an idiom to describe "anything can happen, it might even seem to work". – Yunnosch Feb 09 '18 at 08:19
  • 2
    You most likely overwrote something else or overwrote memory that hasn't been allocated by anything just yet but will soon be. There is no fine grained memory access on most CPUs and unless the programming environment holds your hand by keeping track of array sizes for you and checking those sizes on each access you can touch memory that doesn't belong to you. C doesn't hold your hand, if you do something that you aren't supposed to do bad things will happen. We call it "undefined behavior". – Art Feb 09 '18 at 08:20
  • @Yunnosch Wow. Thank you for the immediate response. I barely refreshed when I saw 2 comments and an upvote. I searched it and there is a whole lot of material on google. Thank you for a good starting point :) – Richard Hawkling Feb 09 '18 at 08:32
  • 2
    If you write far enough beyond the bounds of what was allocated, you will eventually cause problems (program crashing problems) when you try to free the memory allocated, or when you try allocate more memory. It is not unusual for the problem to encountered far from the source of the problem. Don't do it. Simply don't write even one byte beyond the end of the memory allocated. – Jonathan Leffler Feb 09 '18 at 08:33

0 Answers0