简体   繁体   中英

Why asterisk (*) is used after int? ptr = (int*) malloc (100 * sizeof(int));

malloc() allocates memory and returns its starting address , so if we want to assign that address to an int pointer, shouldnt we type-cast with only (int) instead of (int*) ? It is just returning an address of void type, and its an address, so it will definitely be assigned to a pointer, so why do we add * after int in type-casting?

so why we add * after int in typecasting?

Because the * is part of the type. int* is a pointer while int is an integer. They're two different things.

Also, if you take a look at the documentation for malloc ( for example ) you'll find that it returns a void* . There too the * is being used to declare that it's a pointer. So casting from one type of pointer to another type of pointer, uses the * in the type.

The (int *) before malloc is a cast - it means "treat the return value of malloc as a pointer to int ".

Since the 1989 standard, that cast is not necessary in C 1 - malloc returns a value of type "pointer to void " ( void * ), which can be assigned to objects of different pointer types, and most of us will recommend not using it (it adds clutter for little benefit and increases the maintenance burden).

Before the 1989 standard malloc returned "pointer to char " ( char * ), which cannot be assigned to other pointer types without the cast. Anyone who wrote C in the early '80s (or wrote a reference manual at the time) was accustomed to using the cast, and the practice still persists.


  1. This is not the case in C++ - C++ does not allow implicit conversion from void * to other pointer types, so a cast is required. However, if you're writing C++, you shouldn't be using malloc in the first place.

I'm sure everyone has their favorite answer to this question, but I think it helps if you realize that basically everything on a CPU is just a pointer to a memory address. The CPU itself has a very small set of 'registers' it can work with, and the way you get any data structure at all is to follow a memory pointer. The type only has meaning to you the programmer and the computer language enforced by the compiler, and is just a way to organize and keep track of things. It doesn't mean anything at all to the CPU.

In C/C++, the design of the language tries to make this reality (unavoidable in assembly language and machine code) easily accessible. That's one reason why it has been such a popular "systems language" for so long.

All malloc does is set aside a chunk of system memory that is at least as large as the size in bytes you asked for, and returns the start address of that memory. It does not have any idea at all what type it is. It could be space you use for a structure in heap memory. It could be an array of ints, an array of floats, or an array of some structure. The 'cast' after calling malloc is there to let the language and the programmer remember what type you intend to use that memory for.

int* ptr = (int *)malloc(100 * sizeof(int));

You know it's a pointer to enough memory for 100 ints, so you can use it as ptr[x] where x is 0..99 safely. You could use it as *ptr , and you can use it as *ptr++ as long as you only do so 100 times. In this case, the computer language itself doesn't have any more information about the ptr other than it's a pointer-to-an-int.

While you can use this syntax in C++ as well, there's a slightly more type-safe way to do it which avoids some of the clutter, but in the end it does exactly the same thing: int* ptr = new int[100]; .

A C++ programmer would recommend you use stuff like std::array<int, 100> , std::vector<int> , and/or std::unique_ptr<int[]> which is more 'organized' and 'type-safe', but in the end the CPU is still doing the same thing with memory addresses.

The original The C Programming Language by Brian Kernighan & Dennis Ritchie is a great way to get started on any version of C or C++, or frankly any of the many other languages that all borrow heavily from the C language design.

Memory check is required when this code is implemented in the embedded process or controller

int *ptr;  // pointer declaration

ptr = malloc(100 * sizeof(int));  // create an array of 100 int pointers

if(prt == null)
{
  printf("memory failed \n");  //can't process
  return ;
}

for (i = 0; i < 25; i++)  // fill it like a standard array
    ptr[i] = i;

for (i = 0; i < 25; i++)  // process it
    printf("%d\n", ptr[i]);     

free(ptr);  // clean up

With malloc you are defining an array, in your case, you are defining an array of 100 elements of type integer. You are reserving memory for what uses 100 elements of type integer.

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