dont konw what to do next.

This is a discussion on dont konw what to do next. within the C Programming forums, part of the General Programming Boards category; Just even one "potentially serious error" will typcasting malloc() hide? If there's no prototype in scope then C89 will assume ...

  1. #16
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Just even one "potentially serious error" will typcasting malloc() hide?
    If there's no prototype in scope then C89 will assume malloc() returns int. Let's go back a decade or so to the 16-bit x86 architecture. For the x86 it was possible that you would have 16 bit integers and 32 bit pointers. An int would be returned with a single register and a pointer with two. That alone is a "potentially serious error" if malloc() is assumed to return int because knowing nothing else, it would mangle the pointer to an int and then back to a pointer. But it gets worse. What if the registers used to return a pointer are completely different from the register used to return an int?

    That's just one processor you say? Okay, same time period think about the Motorola 68000 series. The problem is the same because pointers would be passed using the address registers and integers using the data registers. Both of which were distinct, so the second problem described for the x86 was guaranteed for the 68000.

    But that's a long time ago, you say. Come back to the present day and give me a "potentially serious error" Narf, you say. Okay. With the advent of 64-bit systems, it's reasonable to assume that the same problem will arise, except with 32/64 instead of 16/32. In fact, I've heard whispers of that very issue popping up with 64-bit Solaris crashing unexpectedly because stdlib.h wasn't included and malloc() had a cast. But I don't have a 64-bit system, so I can't test it. In my mind the modern problem is all theory and heresay, but I can easily see it happening. That's why I only add the cast after I'm sure that it's both needed and won't hide errors.
    Just because I don't care doesn't mean I don't understand.

  2. #17
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    all those problems you mentioned occur because stdlib.h was not included. You are hung up on that one issue. Assume all the correct header files are included, I can't imagine any other objections to typcasting. I always include these three header files in every *.c program, so I never experience the problem you are describing about malloc() not being prototyped.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

  3. #18
    FOX
    Join Date
    May 2005
    Posts
    188
    But why should you when it's not necessary and simply introduces another point of failure? The reasons for not casting malloc may be weak according to you, but can you give us a single good reason for why the casting approach is favourable?

  4. #19
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by ^xor
    but can you give us a single good reason for why the casting approach is favourable?
    Portability to C++. Have you ever tried to port a C program to C++? If not then you should try it some time -- it gets pretty painful and ugly fixing up all C's shortcomings, like lack of typcasting. Just change the file extension from *.c to *.cpp and compile with a c++ compiler. You will find out very quickly the porting problems.

    Other than that, its really just personal preference. I wouldn't think of changing existing C code unless actually porting to c++ because it isn't worth the effort.

  5. #20
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Assume all the correct header files are included, I can't imagine any other objections to typcasting.
    Yes! Finally we're on the same page. If you do everything right, there's nothing wrong with typecasting. But since nobody is perfect, you can't expect everyone to do everything right all the time. That's why I have a routine that protects me from my stupidity when I need to use typecasts.
    I always include these three header files in every *.c program
    Even when all you need is stddef.h?
    Just because I don't care doesn't mean I don't understand.

  6. #21
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Narf
    Yes! Finally we're on the same page. If you do everything right, there's nothing wrong with typecasting. But since nobody is perfect,
    I am

    Quote Originally Posted by Narf
    Even when all you need is stddef.h?
    Yes. I don't recall writing a *.c file that only needed stddef.h. Most (if not all) use something from all three headers. 25 years ago I would have given more thought to it because of the amount of time to compile the program on that speedy 1.2 MG CPU computer with 64K memory, 2 single-sided floppy drives an no hard drive. But today compile time is not an issue on most modern computers.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > Portability to C++. Have you ever tried to port a C program to C++?
    And if a simple cast of malloc made all valid C programs valid C++ programs, then you might have a point. But it isn't.

    Writing code which attempts to be compatible with multiple languages (why stop at C++, why not make it compilable with fortran while you're at it (yes, people do this for sport)) simply results in code which is neither good C nor good C++.

    In fact, I think I'll stick to using 'new' and 'class' as perfectly valid 'C' variable names thank you very much.

    > I always include these three header files in every *.c program
    OK, I suppose when you forget to include say time.h, a random cast on the result of say localtime() is just another mere inconvenience to you.
    Why stop at 3? why not include all the standard headers (oh wait, I mentioned that already).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #23
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Salem
    > Portability to C++. Have you ever tried to port a C program to C++?
    And if a simple cast of malloc made all valid C programs valid C++ programs, then you might have a point. But it isn't.
    Yes, I know there are several (many?) other differences too. Typcasting functinos that return void* is just one of them.

    Quote Originally Posted by Salem
    Writing code which attempts to be compatible with multiple languages (why stop at C++, why not make it compilable with fortran while you're at it (yes, people do this for sport)) simply results in code which is neither good C nor good C++.
    I am not concerned with other languages. C programs that need to be ported to fortran, cobol, C#, or a host of other languages will probably requires a (nearly) complete rewrite anyway. Only portability between C and C++ is the issue.

    Quote Originally Posted by Salem
    In fact, I think I'll stick to using 'new' and 'class' as perfectly valid 'C' variable names thank you very much.
    I would probably do that too, but only after the code can be cleanly compiled with 0 errors and 0 warnings.

    Quote Originally Posted by Salem
    Why stop at 3? why not include all the standard headers (oh wait, I mentioned that already).
    LOL! I take it you have never used VC++ 6.0/7.0 to compile a MS-Windows application ? just take a look at the contents of stdafx.h!

  9. #24
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    just take a look at the contents of stdafx.h!
    Code:
    #define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
    
    #include <windows.h>
    Wow, that was heavy! Regardless, unchecking the "use precompiled headers" box is the first thing I do in any MSVC++ project

  10. #25
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Here is mine (after I added some to it. The ones in RED are the ones I added. Everything else was added by VC++ 6.0 project generator.

    Code:
    // stdafx.h : include file for standard system include files,
    //      or project specific include files that are used frequently,
    //      but are changed infrequently
    
    #if !defined(AFX_STDAFX_H__470832EA_892F_487D_A270_5735AB3AB775__INCLUDED_)
    #define AFX_STDAFX_H__470832EA_892F_487D_A270_5735AB3AB775__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    #define STRICT
    #ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0400
    #endif
    #define _ATL_APARTMENT_THREADED
    #include <afxdisp.h>
    
    
    #include <atlbase.h>
    
    // I added these myself
    #include <winsock.h>
    #include <sys\types.h>
    #include <sys\stat.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <io.h>
    #include <errno.h>
    #include <share.h>
    #include <time.h>
    #include <string>
    #include <vector>
    #include <algorithm>
    //#include <vector>
    #include <list>
    #include <fstream>
    #include "ctpublic.h"
    #include <cstypes.h>
    using namespace std;
    
    //You may derive a class from CComModule and use it if you want to override
    //something, but do not change the name of _Module
    
    #define VERSION		"2.08.04"
    
    class CServiceModule : public CComModule
    {
    public:
    	HRESULT RegisterServer(BOOL bRegTypeLib, BOOL bService);
    	HRESULT UnregisterServer();
    	void Init(_ATL_OBJMAP_ENTRY* p, HINSTANCE h, UINT nServiceNameID, const GUID* plibid = NULL);
        void Start();
    	void ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv);
        void Handler(DWORD dwOpcode);
        void Run();
        BOOL IsInstalled();
        BOOL Install();
        BOOL Uninstall();
    	LONG Unlock();
    	void LogEvent(LPCTSTR pszFormat, ...);
        void SetServiceStatus(DWORD dwState);
        void SetupAsLocalServer();
    
    //Implementation
    public:
    	static HANDLE m_hChangePasswordThread;
    	static DWORD  m_dwChangePasswordID;
    private:
    	static void WINAPI _ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv);
        static void WINAPI _Handler(DWORD dwOpcode);
    // data members
    public:
        TCHAR m_szServiceName[256];
        TCHAR m_szDisplayName[256];
        SERVICE_STATUS_HANDLE m_hServiceStatus;
        SERVICE_STATUS m_status;
    	DWORD dwThreadID;
    	BOOL m_bService;
    
    private:
    	void InitGatesIniFile(void);
    
    	string m_targetPath;
    	string m_server;
    	string m_database;
    	BOOL	m_rgates;
    	long	m_loginSequence;
    	HANDLE	m_userLock;
    	string	m_timeHost;
    
    
    
    };
    
    extern CServiceModule _Module;
    #include <atlcom.h>
    #include <comdef.h>
    
    
    #ifndef DEBUG_NEW
    #define DEBUG_NEW new
    #endif
    
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_STDAFX_H__470832EA_892F_487D_A270_5735AB3AB775__INCLUDED)

  11. #26
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    The bottom line is this is a C forum. In C, typecasting functions that return void pointers is never necessary and just confuses new programmers. If you want to typecast those functions in your programs then go ahead, but it has no place in the C forum. C is C and C++ is C++. Creating your C program so it can compile as C++ is just ridiculous. The whole point of porting a C program to C++ is usually to take advantage of things that require code rewrites and relocations. Adding a stupid typecast to malloc() takes far less than 1% of the porting workload. Getting a C program to compile in C++ hardly qualifies as porting it. How long does it really take you to search the source code for malloc() calls and add the typecasts? This is just ludicrous.
    Last edited by itsme86; 09-09-2005 at 02:39 PM.
    If you understand what you're doing, you're not learning anything.

  12. #27
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by itsme86
    This is just ludicrous.
    Sorry if I am pi$$ing anybody off. This whole discussing started because (I think) the OP posted one little line of code that typecast the return value of malloc. malloc() itself is not the only problem -- every function that returns void* has the same problem whether it is a function in standard C library or one you write yourself. All I intended to say was there is nothing at all wrong with typcasting it, nor is there anything wrong with NOT typcasting it. -- while others disagreeded and said it should never be done!

  13. #28
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    I'm just tired of hearing the same argument over and over and over again. In C, there is no more sense in typecasting malloc() then there is in typecasting any other function. Would you ever think to typecast strlen()? No, and I think it's more important for new programmers to learn to compile their C programs with a C compiler and stop typecasting those functions to incorrectly compile it as C++.
    If you understand what you're doing, you're not learning anything.

  14. #29
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by itsme86
    I'm just tired of hearing the same argument over and over and over again. .
    So don't read it.

  15. #30
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Right. I guess you have some kind of psychic power and can tell ahead of time from a subject like "dont konw what to do next" that it's going to be another one of these stupid arguments. I don't know about you, but I need to read something that's been written before I know what it is.

    A more logical solution is for people to actually stop with this stupid paper-thin C++ porting argument and just supply helpful, meaninful answers when someone has a question, don't you think? Because we are concerned when someone posts here with typecasted malloc() calls that they're doing something wrong. Either not including stdlib.h or compiling with the wrong compiler which can lead to even more problems for the poster. We already know there's "nothing wrong with it," but it's usually an indication of a real problem.

    But with your powers I'm sure you already knew I was going to type that.
    Last edited by itsme86; 09-09-2005 at 04:11 PM.
    If you understand what you're doing, you're not learning anything.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21