| | |
type casting
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Most compilers will complain because malloc() returns void* and, in C++, you need to cast void* to the type you are mallocing.
Generally, you should use 'new' and 'delete' rather than malloc, for at least these three good reasons:
1) You don't need to cast
2) As Dave pointed out, malloc(10) allocates 10 BYTES, not 10 ints
3) Constructors are not called. Not a big deal with ints, but a huge deal with objects.
In this case,
int* a = new int[10];
delete [] a;
note the need for [] in the delete; if you used [] in new, use them in delete.
Generally, you should use 'new' and 'delete' rather than malloc, for at least these three good reasons:
1) You don't need to cast
2) As Dave pointed out, malloc(10) allocates 10 BYTES, not 10 ints
3) Constructors are not called. Not a big deal with ints, but a huge deal with objects.
In this case,
int* a = new int[10];
delete [] a;
note the need for [] in the delete; if you used [] in new, use them in delete.
>int *a = malloc(10 * sizeof *a);
>Using C memory allocation routines in C++ programs is BAD FORM.
I see nothing in the original post that suggests either C++ or C. As the answer is directly related to the language, don't you three think it makes sense to ask before going off on your favorite tangent?
>what happened if we do't typecast return value of malloc
In C, nothing because pointers are implicitly converted to and from void without the need for a cast. In fact, it is best to avoid the cast in C because it hides errors.
In C++, you would get a compile-time error because C++ does not bless implicit pointer conversions to and from void. A cast is required, but it's better to use new in C++ for several reasons that I won't mention because that wasn't your question.
•
•
•
•
Originally Posted by Chainsaw
Most compilers will complain because malloc() returns void* and, in C++, you need to cast void* to the type you are mallocing.
Generally, you should use 'new' and 'delete' rather than malloc, for at least these three good reasons:
1) You don't need to cast
2) As Dave pointed out, malloc(10) allocates 10 BYTES, not 10 ints
3) Constructors are not called. Not a big deal with ints, but a huge deal with objects.
In this case,
int* a = new int[10];
delete [] a;
note the need for [] in the delete; if you used [] in new, use them in delete.
I see nothing in the original post that suggests either C++ or C. As the answer is directly related to the language, don't you three think it makes sense to ask before going off on your favorite tangent?
>what happened if we do't typecast return value of malloc
In C, nothing because pointers are implicitly converted to and from void without the need for a cast. In fact, it is best to avoid the cast in C because it hides errors.
In C++, you would get a compile-time error because C++ does not bless implicit pointer conversions to and from void. A cast is required, but it's better to use new in C++ for several reasons that I won't mention because that wasn't your question.
I'm here to prove you wrong.
![]() |
Similar Threads
- Multiple inheritance and cross-class type casting (C++)
- Please help me in Type casting (C)
- help .. using type casting ? (C++)
- Problem while Type Casting (C++)
- Type casting and type conversions (C)
- Type casting (C++)
Other Threads in the C++ Forum
- Previous Thread: Difficulty understanding Object Orientated File Streaming
- Next Thread: Help Writing Fuction to Read Fractions From User
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






