Was wondering what people think of this header file:
#ifdef MALLOC_H
#define MALLOC_H
# ifdef __cplusplus
# include <cstdlib>
struct voidptr_ {
void *x;
template <typename T> inline operator T * () {
return ((T *) x);
}
};
static inline voidptr_ malloc_(size_t n)
{
return (voidptr_) {
malloc(n)
};
}
# define malloc(x) malloc_(x)
template<typename T> static inline void free(T *x) {
free((void *)x);
}
template<typename T> static inline T *realloc(T *x, size_t n) {
return (T *)realloc((void*)x,n);
}
# else /* C */
# include <stdlib.h>
# ifdef __GNUC__
# define realloc(p,s) ((__typeof__(p))(realloc(p,s)))
# endif /* GNU C */
# endif /* C++/C */
#define tcalloc(n,T) ((T*)(calloc(n,sizeof(T))))
#endif /* incl guard */ 9 1329
Jordan Abel wrote: Was wondering what people think of this header file:
#ifdef MALLOC_H #define MALLOC_H [...]
And how do you think it's going to benefit its user?
V
--
Please remove capital As from my address when replying by mail
On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote: Jordan Abel wrote: Was wondering what people think of this header file:
#ifdef MALLOC_H #define MALLOC_H [...]
And how do you think it's going to benefit its user?
Allowing valid C code to compile as valid C++
Allowing one to use malloc/free [if you want to, who should stop you]
without breaking C style rules, yet still preserving the type-safety
that is allegedly the reason void * can't be implicitly converted in
c++.
Jordan Abel wrote: On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote: Jordan Abel wrote: Was wondering what people think of this header file:
#ifdef MALLOC_H #define MALLOC_H [...] And how do you think it's going to benefit its user?
Allowing valid C code to compile as valid C++
Allowing one to use malloc/free [if you want to, who should stop you] without breaking C style rules, yet still preserving the type-safety that is allegedly the reason void * can't be implicitly converted in c++.
Take a closer look at the bit Victor quoted.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Ben Pope wrote: Jordan Abel wrote:
On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote:
Jordan Abel wrote:
Was wondering what people think of this header file:
#ifdef MALLOC_H #define MALLOC_H [...]
And how do you think it's going to benefit its user?
Allowing valid C code to compile as valid C++
Allowing one to use malloc/free [if you want to, who should stop you] without breaking C style rules, yet still preserving the type-safety that is allegedly the reason void * can't be implicitly converted in c++.
Take a closer look at the bit Victor quoted.
Well, that's probably a simple typo. It's good that you've caught it,
but my question was in general, not about those particular two lines.
V
--
Please remove capital As from my address when replying by mail
Jordan Abel wrote: On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote:
Jordan Abel wrote:
Was wondering what people think of this header file:
#ifdef MALLOC_H #define MALLOC_H [...]
And how do you think it's going to benefit its user?
Allowing valid C code to compile as valid C++
Allowing one to use malloc/free [if you want to, who should stop you] without breaking C style rules, yet still preserving the type-safety that is allegedly the reason void * can't be implicitly converted in c++.
I believe that if you want to drive without buckling up, the car should
make noises about it and display the red warning light on the dashboard
that your seat belts are not engaged. What you're proposing is silencing
the warning sound signal and taping over the warning light with a piece of
duct tape. That's wrong. If somebody wants to use 'malloc' instead of
'new', they _should_ be required to also use the explicit cast.
V
--
Please remove capital As from my address when replying by mail
I believe (and sincerely hope) that Jordans hack was a means to ease
the porting of C-code.
Victor Bazarov wrote: Jordan Abel wrote: On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote:
Jordan Abel wrote:
Was wondering what people think of this header file:
#ifdef MALLOC_H #define MALLOC_H [...]
And how do you think it's going to benefit its user?
Allowing valid C code to compile as valid C++
Allowing one to use malloc/free [if you want to, who should stop you] without breaking C style rules, yet still preserving the type-safety that is allegedly the reason void * can't be implicitly converted in c++.
I believe that if you want to drive without buckling up, the car should make noises about it and display the red warning light on the dashboard that your seat belts are not engaged. What you're proposing is silencing the warning sound signal and taping over the warning light with a piece of duct tape. That's wrong. If somebody wants to use 'malloc' instead of 'new', they _should_ be required to also use the explicit cast.
I believe (and sincerely hope) that Jordans hack was a means to ease
the porting of existing C-code. In that case I do find some value in
it, so long as it is not meant to be a permanent solution. If the
purpose is to allow C++ code to call malloc, I agree that this is ugly
and should be forbidden.
/Peter
V -- Please remove capital As from my address when replying by mail
peter koch wrote: I believe (and sincerely hope) that Jordans hack was a means to ease the porting of C-code.
Victor Bazarov wrote:
Jordan Abel wrote:
On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote:
Jordan Abel wrote:
>Was wondering what people think of this header file: > >#ifdef MALLOC_H >#define MALLOC_H >[...]
And how do you think it's going to benefit its user?
Allowing valid C code to compile as valid C++
Allowing one to use malloc/free [if you want to, who should stop you] without breaking C style rules, yet still preserving the type-safety that is allegedly the reason void * can't be implicitly converted in c++. I believe that if you want to drive without buckling up, the car should make noises about it and display the red warning light on the dashboard that your seat belts are not engaged. What you're proposing is silencing the warning sound signal and taping over the warning light with a piece of duct tape. That's wrong. If somebody wants to use 'malloc' instead of 'new', they _should_ be required to also use the explicit cast.
I believe (and sincerely hope) that Jordans hack was a means to ease the porting of existing C-code. In that case I do find some value in it, so long as it is not meant to be a permanent solution.
I am honestly disappointed that you would find "some value in it". If I
were to port existing C code, I would expect the C++ compiler to _scream_
at me for every use of 'malloc' so that I would be *forced* either to
change it to 'new' or at least put an explicit cast that can be searched
later and weeded out...
Of course, I can later search the code for 'malloc' and take care of it...
So the point is moot, I guess. Possibly it's just my knee-jerk reaction
to the original proposal
If the purpose is to allow C++ code to call malloc, I agree that this is ugly and should be forbidden.
/Peter
V
--
Please remove capital As from my address when replying by mail
On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote: Jordan Abel wrote: On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote:
Jordan Abel wrote:
Was wondering what people think of this header file:
#ifdef MALLOC_H #define MALLOC_H [...]
And how do you think it's going to benefit its user?
Allowing valid C code to compile as valid C++
Allowing one to use malloc/free [if you want to, who should stop you] without breaking C style rules, yet still preserving the type-safety that is allegedly the reason void * can't be implicitly converted in c++.
I believe that if you want to drive without buckling up, the car should make noises about it and display the red warning light on the dashboard that your seat belts are not engaged. What you're proposing is silencing the warning sound signal and taping over the warning light with a piece of duct tape. That's wrong. If somebody wants to use 'malloc' instead of 'new', they _should_ be required to also use the explicit cast.
While the reason for forbidding implicit conversion of pointer to void
in general has been explained to my satisfaction, the explanation I was
given does not justify having to do it for malloc in particular. Note
that I did not have realloc return a "voidptr_", and I went to some
effort to provide a reasonable solution for calloc also without doing
so.
On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote: peter koch wrote: I believe (and sincerely hope) that Jordans hack was a means to ease the porting of C-code.
Victor Bazarov wrote:
Jordan Abel wrote:
On 2006-02-17, Victor Bazarov <v.********@comAcast.net> wrote:
>Jordan Abel wrote: > > >>Was wondering what people think of this header file: >> >>#ifdef MALLOC_H >>#define MALLOC_H >>[...] > >And how do you think it's going to benefit its user?
Allowing valid C code to compile as valid C++
Allowing one to use malloc/free [if you want to, who should stop you] without breaking C style rules, yet still preserving the type-safety that is allegedly the reason void * can't be implicitly converted in c++.
I believe that if you want to drive without buckling up, the car should make noises about it and display the red warning light on the dashboard that your seat belts are not engaged. What you're proposing is silencing the warning sound signal and taping over the warning light with a piece of duct tape. That's wrong. If somebody wants to use 'malloc' instead of 'new', they _should_ be required to also use the explicit cast.
I believe (and sincerely hope) that Jordans hack was a means to ease the porting of existing C-code. In that case I do find some value in it, so long as it is not meant to be a permanent solution.
I am honestly disappointed that you would find "some value in it". If I were to port existing C code, I would expect the C++ compiler to _scream_ at me for every use of 'malloc' so that I would be *forced* either to change it to 'new' or at least put an explicit cast that can be searched later and weeded out...
Of course, I can later search the code for 'malloc' and take care of it...
So the point is moot, I guess. Possibly it's just my knee-jerk reaction to the original proposal
Sometimes when interacting with existing libraries written in C, you
_need_ to use malloc/free instead of new/delete. Some library functions
will require something that can be realloc'ed internally, or will return
something that needs to be free'd. If the purpose is to allow C++ code to call malloc, I agree that this is ugly and should be forbidden.
C++ code is already permitted to call malloc. This just provides a way
to do it without adding a cast that can hide warnings that are still
needed in C. It's certainly a better solution than defining a macro that
does the cast for you. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian Blais |
last post by:
Hello,
I saw on a couple of recent posts people saying that casting the return
value of malloc is bad, like:
d=(double *) malloc(50*sizeof(double));
why is this bad? I had always thought...
|
by: hermit_crab67 |
last post by:
Can someone explain to a C newbie why this doesn't work as I expect it
to work? (expectations clearly outlined in the printf statement in
main routine)
OS: Linux 2.4.26
GCC: 2.95.4
void...
|
by: ytrama |
last post by:
Hi,
I have read in one of old posting that don't cast of pointer which
is returned by the malloc. I would like to know the reason.
Thanks in advance,
YTR
|
by: Mirko |
last post by:
Hello, I'm new to this list and to Usenet in general,
so please forgive (and advice) me, if I do something wrong.
Anyway.
I am a bit confused, because I always thought one
_should_ explicitly...
|
by: SRR |
last post by:
Why is it discouraged to explicitly typecast the void pointer returned
by malloc(); function?
For example:
{
int *p;
p = (int*)malloc(2*sizeof(int)); /*Explicit casting is done, therfore
it...
|
by: sophia.agnes |
last post by:
Hi ,
I was going through peter van der linden's book Expert C programming,
in this book there is a section named "How and why to cast"
the author then says as follows
(float) 3 - it's a...
|
by: alex.j.k2 |
last post by:
Hello all,
I have "PRECISION" defined in the preprocessor code
and it could be int, float or double, but I do not know in the
code what it is.
Now if I want to assign zero to a "PRECISION"...
|
by: Tinkertim |
last post by:
Hi,
I have often wondered if casting the return value of malloc() (or
friends) actually helps anything, recent threads here suggest that it
does not .. so I hope to find out.
For instance :
...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
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...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |