简体   繁体   中英

Difference between allocating memory and allocating memory for an array

Is there a meaningful difference between kmalloc and kmalloc_array ?

I was under the impression that memory was memory, but as it is described here there appears to be some difference.

kmalloc — allocate memory

kmalloc_array — allocate memory for an array.

Are they just two different ways to accomplish the same thing?

From the kernel-source (slab.h)

/**
* kmalloc_array - allocate memory for an array.
* @n: number of elements.
* @size: element size.
* @flags: the type of memory to allocate (see kmalloc).
*/
static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
{
      if (size != 0 && n > SIZE_MAX / size)
             return NULL;
      return __kmalloc(n * size, flags);
}

I wonder who needed that ;))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM