Why do you specify the size when using malloc in C?

后端 未结 17 546
再見小時候
再見小時候 2020-12-03 05:19

Take the following code :

int *p = malloc(2 * sizeof *p);

p[0] = 10;  //Using the two spaces I
p[1] = 20;  //allocated with malloc before.

p[2] = 30;  //U         


        
相关标签:
17条回答
  • 2020-12-03 05:32

    You are asking for space for two integers. p[3] assumes that you have space for 4 integers!

    ===================

    You need to tell malloc how much you need because it can't guess how much memory you need.

    malloc can do whatever it wants as long as it returns at least the amount of memory you ask for.

    It's like asking for a seat in a restaurant. You might be given a bigger table than you need. Or you might be given a seat at a table with other people. Or you might be given a table with one seat. Malloc is free to do anything it wants as long as you get your single seat.

    As part of the "contract" for the use of malloc, you are required to never reference memory beyond what you have asked for because you are only guaranteed to get the amount you asked for.

    0 讨论(0)
  • 2020-12-03 05:35

    Try this:

    int main ( int argc, char *argv[] ) {
      int *p = malloc(2 * sizeof *p);
      int *q = malloc(sizeof *q);
      *q = 100;
    
      p[0] = 10;    p[1] = 20;    p[2] = 30;    p[3] = 40;
      p[4] = 50;    p[5] = 60;    p[6] = 70;
    
    
      printf("%d\n", *q);
    
      return 0;
    }
    

    On my machine, it prints:

    50

    This is because you overwrote the memory allocated for p, and stomped on q.

    Note that malloc may not put p and q in contiguous memory because of alignment restrictions.

    0 讨论(0)
  • 2020-12-03 05:36

    Memory is represented as an enumerable contiguous line of slots that numbers can be stored in. The malloc function uses some of these slots for its own tracking info, as well as sometimes returning slots larger than what you need, so that when you return them later it isn't stuck with an unusably small chunk of memory. Your third int is either landing on mallocs own data, on empty space leftover in the returned chunk, or in the area of pending memory that malloc has requested from the OS but not otherwise parcelled out to you yet.

    0 讨论(0)
  • 2020-12-03 05:36

    Depending on the platform, p[500] would probably "work" too.

    0 讨论(0)
  • 2020-12-03 05:36

    Because malloc() allocates in BYTES. So, if you want to allocate (for example) 2 integers you must specify the size in bytes of 2 integers. The size of an integer can be found by using sizeof(int) and so the size in bytes of 2 integers is 2 * sizeof(int). Put this all together and you get:

    int * p = malloc(2 * sizeof(int));
    

    Note: given that the above only allocates space for TWO integers you are being very naughty in assigning a 3rd. You're lucky it doesn't crash. :)

    0 讨论(0)
  • 2020-12-03 05:37

    You got (un)lucky. Accessing p[3] is undefined, since you haven't allocated that memory for yourself. Reading/writing off the end of an array is one of the ways that C programs can crash in mysterious ways.

    For example, this might change some value in some other variable that was allocated via malloc. That means it might crash later, and it'll be very hard to find the piece of (unrelated) code that overwrote your data.

    Worse yet, you might overwrite some other data and might not notice. Imagine this accidentally overwrites the amount of money you owe someone ;-)

    0 讨论(0)
提交回复
热议问题