473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

about typecasting malloc (), is bad practice, why ?

If typecasting the return of malloc()is considered to be a bad practice, why Turbo C forces us to it
Dec 15 '13 #1
1 1318
weaknessforcats
9,208 Expert Mod 8TB
Visual Studio.NET also forces you to cast the return of
malloc().

malloc() returns a void* which means no type. When you assign a pointer to "no type" to a pointer to "a real type" it gags the type safety check of the compiler. This forces you to cast so the assignment uses the same type for the RVAL and LVAL.

Supposedly, you can assign a void* to any other pointer without a cast according to the rules of C but apparently your compiler and mine disallow it.

As to the bad practice, you need to remember that casting is a bad practice as is all void* which must be cast in order to be used.

Unfortunately, C is very primitive and uses the void* to work around the absence of function overloading you see in more modern languages.
Dec 15 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Xah Lee | last post by:
Dear functional programing comrades, Among the community of automatons of the IT industry, there is a popular quote about "theory vs practice" that goes something along the lines of "in theory...
7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
8
by: Kamus of Kadizhar | last post by:
This is probably pretty silly for those in the know, but it's been a while since I've muddled about with malloc.... I have the following snippet of code: char *letters_array; ...... int...
16
by: Ronny Mandal | last post by:
Hi! I am writing a program that'll read binary integers from a file, put them into a dynamic array. THe array is first declared int* a = malloc( sizeof( int ) * N ) <-- N is the number of...
7
by: Yuri_Юрий | last post by:
I'm confused about the VARIABLE LENGTH ARRAYS. {scanf("%d",&n);float a;} In which compiler can I use it? I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION! Another question, What...
4
by: vivekian | last post by:
Hi, This is the part of the code am trying to compile to : void Server::respondToClient ( std::string response ) { .... .... if ((numbytes = sendto ( sockFd_ , response , sizeof(response)...
21
by: ramu | last post by:
Hi, Will the memory allocated by malloc and realloc be contiguous? regards
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
21
by: Anton Dec | last post by:
Just curious about this... malloc is defined in stdlib.h, right? But if I write a program without #include<stdlib.hand use malloc, it still works as expected. Why is this? Is malloc...
8
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.