What does the point mean here?

In some memory allocation, I find this. But I don’t get it.

char * mem_alloc() { char *point; char *a; point = (char *)malloc(block_num); a = *(char**) point; return a; } 
+4
source share
6 answers

char * mem_alloc()

In my experience, functions that return a pointer are almost always a sign of improper program design. Such a pointer may indicate the following:

  • local variable (egregious error, UB)
  • global / static (poor design of the program, as well as not thread safe)
  • dynamic memory (poor design of the program, code using memory must handle the distribution, great potential for leaks)
  • or to one of the parameters passed to the function (poor program design, unclear functional interface)

In this case, it points to dynamic memory, so we can probably assume a poor program design and probably a memory leak to go with it.

point = (char *)malloc(block_num);

This code means that the one who wrote it is confused by how malloc works and how void pointers work. The result of malloc should never be typed; see this and this . The desire for type coercion means that the programmer is confused in the C language and / or that they are trying to compile C code in the C ++ compiler.

"block_num" is suspicious, if it is a global, variable variable, then the design of the program leaves much to be desired.

a = *(char**) point;

This means that you take the address of the point that points to uninitialized memory on the heap, then pretend that the point is a pointer to a pointer and thus processes the contents of the garbage on the heap as if it were a pointer. Then return this pointer, pointing to a random location in la la la earth, the caller. And at the same time create a memory leak.

+8
source

This seems to mean that the point value is equal (i.e. the address). a = *(char**) point;
The above statement is a type-point - a "pointer to a pointer" in the "(char **) point" part. After that, you have * for the de-link, which changes it to a value (pointer to pointer)
=> pointer.
Thus, the value (rather the address) stored at the point is copied to a.
I still don't know why this code is written.

0
source

The code you sent is stupid - are you sure it is complete?

"point" and "a" are both pointers.

"a" is initialized with a "dot" ... but the "dot" is completely uninitialized. Bad things will happen if the caller tries to use the return value of "a".

Here is another example:

 struct Point { int x; int y; }; ... char * mem_alloc (int size) { return (char *)malloc (size); } ... Point *my_point = (Point *)mem_alloc (sizeof (struct Point)); ... 

This snippet is also stupid ... but hopefully it illustrates a bit of what might be the basis for the code you are looking for ...

0
source

a contains the value stored in the location pointed to by point . Since point uninitialized, it points to a random location that contains a random value, and therefore, now a indicates the random value from which it should start.

So the code does not work.

0
source

If you look at the concept of malloc , it always returns the base address of the allocated memory space, the actual syntax of the malloc function Syntax:

 pointer_to_store_base_add_of_mem = (data_type_of_allocated_memory)malloc(size_of_array) 

In the above example, you allocated the character type of the memory, so you used (char *), and in block_num you set the size of the character array, and the point pointer stores the base address of the allocated memory.

0
source

Looking at the chenyoufu123 code posted in the Lundin comment, reply:

 ptr = (char *)malloc(block_num * size); for(k=0; k<block_num-1; k++) { *((char **)(ptr + k*size)) = ptr + (k+1) * size; } *((char **)(ptr + k*size)) = NULL; 

This is still bad code, but not completely pointless, like the source code from the question. In particular, along with the comment that it is used to create a linked list from another comment.

The situation - assuming the code is "correct" - you have

 struct Node { struct Node *next; /* More members */ }; 

and size_t size = sizeof(struct Node); (names will be different, maybe). Then

 ptr = (char *)malloc(block_num * size); 

allocates memory for block_num adjacent struct Node s. You can usually distinguish this as

 struct Node *ptr = malloc(block_num * sizeof *ptr); 

Cycle

 for(k=0; k<block_num-1; k++) { *((char **)(ptr + k*size)) = ptr + (k+1) * size; } *((char **)(ptr + k*size)) = NULL; 

then reinterprets the address k * sizeof(struct Node) at the beginning of the memory block, ptr + k*size , as a pointer to a pointer (a char** , but on most PC architectures these days it doesn’t matter, since all object pointers have the same representation - if this is important, the code is violated anyway) and writes the address of the next sizeof(struct Node) memory size to this place. Since the next pointer is the first member of the struct Node , which writes the address of the next struct Node to the list, the next pointer of the current struct Node . Finally, the next pointer of the last struct Node set to NULL .

The usual way to record

 struct Node *ptr = malloc(block_num * sizeof *ptr); int k; for(k = 0; k < block_um - 1; ++k) { ptr[k].next = &ptr[k+1]; // or ptr + (k+1), if you prefer } ptr[block_num-1].next = NULL; 

not only clearer, but also has the advantage of working on platforms where char* and struct Node* have different sizes or representations.

0
source

All Articles