Home > Back-end >  Novice asked a question, a return value of c in the new void * pointer is why
Novice asked a question, a return value of c in the new void * pointer is why

Time:09-16

Run on vs, show is the return value of a new type void *, shouldn't be int * type?

CodePudding user response:

If as you say he is the return value of the int *? Then double * pd=new double [5]; He should be a return to a double * cough up? Why return void *? Because the pointer footprint size are the same, void *, int *, double * is four bytes in a 32-bit system, inside with directed address, just depends on what type of you use pointer to save this address

CodePudding user response:

Allocate a block of memory from the heap, the memory and not sure what type!

CodePudding user response:

Void * is common to other type also more convenient it is ok to just write a return void *
Otherwise int return int double return double... And so on a lot of trouble

CodePudding user response:

Computer memory or file content or transport it is just a one-dimensional binary byte array and its corresponding binary address;
The human brain to a computer memory or file contents or transfer the content of the one-dimensional binary byte array and its corresponding binary address some parts as an integer, number of signed/unsigned number, floating point Numbers, complex Numbers, letters, digits, Chinese/Korean/French... Character/string, assembly instructions, functions, function parameters, heap, stack, arrays, Pointers, array pointer and pointer array, the array of arrays, pointer to pointer, two-dimensional arrays, character lattice, the coordinates of character strokes, black and white binary images and grayscale images, color images, audio, video, fingerprint information, id information...

http://edu.csdn.net/course/detail/2344 C language pointer and assembly - a memory address. The code element

CodePudding user response:

New int [...]. This expression is called operator new [] (... ) to allocate memory, the latter return void *, but in the end, the results are int *, note that the operator new [] (... ) returns the address of the allocated memory block, the new int [...]. Return to create the address of the first array element, the two may not equal in value,

CodePudding user response:

reference 5 floor XSKLLD reply:
new int [...]. This expression is called operator new [] (... ) to allocate memory, the latter return void *, but in the end, the results are int *, note that the operator new [] (... ) returns the address of the allocated memory block, the new int [...]. Return to create the first element of the array address, the two may not equal in value,

But continue to run the array value is 0 x007e9488, namely and operator new [] (... ) values are equal, and can you give an example of unequal?

CodePudding user response:

reference 1st floor wwww200808008 response:
if as you say he is the return value of the int *? Then double * pd=new double [5]; He should be a return to a double * cough up? Why return void *? Because a pointer footprint size are the same, void *, int *, double * is four bytes in a 32-bit system, inside with directed address, just depends on what type you use pointer to save this address

Money can book words: "" new returns a numeric data type of pointer" "operation, do not know how to understand this sentence, my feeling is said is a specific type (such as the int *), rather than a void *

CodePudding user response:

refer to 7th floor k5452055 response:
Quote: refer to 1st floor wwww200808008 response:

If as you say he is the return value of the int *? Then double * pd=new double [5]; He should be a return to a double * cough up? Why return void *? Because a pointer footprint size are the same, void *, int *, double * is four bytes in a 32-bit system, inside with directed address, just depends on what type you use pointer to save this address

Money can book words: "" new returns a numeric data type of pointer" "operation, do not know how to understand this sentence, my feeling is said is a specific type (such as the int *), rather than a void *


New returns a pointer is numerical data type I said not safety means the void * equivalent to you to define the pointer of type...

CodePudding user response:

refer to 6th floor k5452055 response:
Quote: refer to the fifth floor XSKLLD reply:

New int [...]. This expression is called operator new [] (... ) to allocate memory, the latter return void *, but in the end, the results are int *, note that the operator new [] (... ) returns the address of the allocated memory block, the new int [...]. Return to create the first element of the array address, the two may not equal in value,

But continue to run the array value is 0 x007e9488, namely and operator new [] (... ) values are equal, and can you give an example of unequal?

Is said in the standard can is not equal, and this uncertainty behavior I don't give a guarantee not equal example,
The following is a reference example,
Example
For these reasons, if this case under your compiler result is the same, please don't be surprised,

CodePudding user response:

refer to 7th floor k5452055 response:
Quote: refer to 1st floor wwww200808008 response:

If as you say he is the return value of the int *? Then double * pd=new double [5]; He should be a return to a double * cough up? Why return void *? Because a pointer footprint size are the same, void *, int *, double * is four bytes in a 32-bit system, inside with directed address, just depends on what type you use pointer to save this address

Money can book words: "" new returns a numeric data type of pointer" "operation, do not know how to understand this sentence, my feeling is said is a specific type (such as the int *), rather than a void *

"To believe, is not equal to no book," believe that compile results, information run results!

CodePudding user response:

Void * is common type, because the new pointer variable is uncertain to what type of data, void * to transfar for any type of pointer,

CodePudding user response:

You can choose to other types of bai, can also be overloaded new, what do you want what type, type is

CodePudding user response:

refer to 6th floor k5452055 response:
Quote: refer to the fifth floor XSKLLD reply:

New int [...]. This expression is called operator new [] (... ) to allocate memory, the latter return void *, but in the end, the results are int *, note that the operator new [] (... ) returns the address of the allocated memory block, the new int [...]. Return to create the first element of the array address, the two may not equal in value,

But continue to run the array value is 0 x007e9488, namely and operator new [] (... ) values are equal, and can you give an example of unequal?

New B assign A value to A pointer, which inherits A, B but not B first base class

CodePudding user response:

What you see is the operator new () the return values of this function is not new to the return value of new operation can understand into two parts: the first is implemented using the malloc function operator new function, its task is malloc to apply for an empty memory returns a pointer to void * (and of course to empty thrown exception, etc.), the second part is the compiler's own part of the operation: first of all, the compiler calls the constructor initializes the object, to divert void * T * as the new return values, so as long as the understanding of the first part, the operator is new function, the other is as the compiler automatically, (actual should have their own understanding, wrong, but they can understand the operation).

CodePudding user response:

nullnullnull
  • Related