Hi All,
Here is a small Code,
int main(void)
{
char *p=(char *) malloc(100);
strcpy(p,"Test1 234567890");
p=p+10;
free(p);
/*** Is here a memory Leak, because p is now
pointing 10 location past to the start of allocated memory
****/
/** some stuff with p again**/
}
Thanks and Regards,
Raman Chalotra
Feb 1 '07
171 4953
"matevzb" <ma*****@gmail. comwrote:
On Feb 9, 8:27 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
If you choose to break that by using void * for everything and then
casting to random types, that's your problem, and it demonstrates
exactly nothing about the need to check anything in well-written C.
It's not my choice, see the reply to Bill Pursell. And of course it's
my problem, I have to use such code, be it well-written C or not.
If it's foisted on you, it's not a very good example in a discussion
about what is and is not good design, though, is it?
I'm using
int WINAPI WinMain(HINSTAN CE instance, HINSTANCE previnst__,
PSTR cmdline, int showmode)
because I have to. That doesn't mean I think it's good design to include
a function parameter you never use and haven't for years, or even that
the above is valid C.
Richard
Yevgen Muntyan wrote, On 09/02/07 00:34:
Flash Gordon wrote:
>Yevgen Muntyan wrote, On 08/02/07 19:14:
>>Flash Gordon wrote: Yevgen Muntyan wrote, On 08/02/07 13:34: Christoph er Layne wrote: >Yevgen Muntyan wrote:
<snip>
>>>However, the *habit* of casting is always a bad habit and the habit of casting malloc is always a bad habit.
Yes, such a *habit* is bad. Did I tell it's not?
You seem to be saying there is nothing wrong with doing something even though if it becomes a habit it is a bad habit. Habits are formed by repeatedly doing things, so if you want to avoid having bad habits do not engage in the practices which are bad habits.
I do not engage in those practices to my best knowledge. At least
not with casting return value of malloc. I don't cast it normally.
I do cast it when I need to (usually it's C++ or allocation
macros in headers). And I am not saying it's okay to cast stuff
here and there. I am saying that some folks in this newsgroup
must apply some consideration and thinking before spitting
stupid "Wrong."
You have yet to come up with a convincing reason for doing it, so there
is so far no convincing reason for people not to say it is the wrong
thing to do.
>>>I have seen exactly one person come up with a convincing reason why he should cast the value returned by malloc in his specific situation. That reason is at least in part due to commercial reality rather than technical merit.
Note the compatibility with C++ is a bad reason because: 1) In C++ there are better methods than using malloc to get memory
You lose compatibility with C then ;)
Actually, than is not necessarily true. There are ways of creating interfaces for C code to call C++ code.
Tenth time: *compiling* C code with C++ compiler. It is necessarily
true that C compiler won't accept "new Foo" (we don't talk crazy
"C/C++" compilers and funny macros, right?).
In that case, for the 11th time there is no reason to compile C code
with a C++ compiler. If you want to access C code from C++ then use the
methods defined in C++ otherwise don;t, but either way compile your C
code as C and your C++ code as C++.
>>>2) C++ defines mechanisms to interface to C code that has been compiled as C code.
And I am talking about compiling *C* code with *C++* compiler.
Simple solution, don't do it. Apart from anything else there are subtle differences between C and C++ such that a program can be valid as both but behave differently.
And there is lot of C code which behaves in the same way. E.g.
foo.h:
---------------------------
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Foo Foo;
void *alloc_some_mem (size_t n);
#define ALLOC_FOO() ((Foo*) alloc_some_mem (sizeof (Foo)))
#ifdef __cplusplus
}
#endif
---------------------------
Yes, it is the same. Whether presented to someone who know C or C++ they
will tell you it is the wrong thing to do. Given the above delete the
#define and just call alloc_some_mem when you want to use it, calling
from C code without the cast calling from C++ code with the cast
(although using it from C++ code would probably be a bad thing). So by
deleting one line the problem goes away.
I won't try to talk about other cases when one really wants to compile
his C code with C++ compiler, since there is an easy solution, "don't do
it". I really love these simple solutions: the problem is to do this
and this; the solution is not to do this and this and do that instead.
Works like a charm in an argument.
It is also the best solution. The languages *are* different. You don't
try to compile you C code with a Java compiler or run it in a Perl
interpreter, so why try to compile it with a C++ compiler?
--
Flash Gordon
Yevgen Muntyan wrote, On 09/02/07 04:47:
Keith Thompson wrote:
>Yevgen Muntyan <mu************ ****@tamu.eduwr ites:
>>Mark McIntyre wrote:
[...]
>>>If you're compiling it with a C++ compiler, its C++ code. It may look like C, it may also compile as C, but its C++, and probably badly written C++ at that. Okay, let's use this term: "C code which is intended to be compiled both with C and C++ compilers". I mean "C code" by "C code", i.e. the code which is C according to C standard and is compilable with C compiler in conforming mode. Not sure it's strict enough, but hope you get the idea (which you already got anyway, I believe). And no, C code doesn't become C++ code because you feed it to g++, even if C++ folks think it is :)
No, C code doesn't become C++ code because you feed it to g++. If g++ accepts it (assuming g++ to be a correctly working C++ compiler), then it was *already* C++ code.
What you're talking about is code that is simultaneously C code and C++ code.
Yes.
Now tell us why you want code that is both. A good technical reason not,
"well it means I can feed it to a C or a C++ compiler" since that is a
circular argument.
>I don't want to get too deeply into this argument, but I will say that, in my opinion, there is rarely a real need to write such code. (I said "rarely", not "never".)
And this is the key point: "rarely" is not "never".
Though it's not really that rare, just look at /usr/include/*.h or
whatever_path\i nclude\*.h. Lots of C code which is
intentionally made C++-compatible. Lately only die-hard
hate-c-plus-plus folks make public C headers not C++-compatible
if they want their libraries to be used
How many times can you find casts of the return value of malloc in the
*headers*. Those headers are for defining interfaces and so should not
in general include code. After all, how would you use those libraries
from Ada, Fortran etc if it relies on code in C header files?
I indeed was
talking not about this (except the parts about headers
of course). I did talk about doing "g++ foo.c". Anyway,
nobody is really talking about technical stuff. Technical
stuff is used in this discussion to justify blind stupid
"Wrong period". Whatever.
You have yet to provide a good reason for feeding your C code to a C++
compiler so you have yet to back up your argument that it is not the
wrong thing to do.
--
Flash Gordon
On Feb 9, 10:36 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"matevzb" <mate...@gmail. comwrote:
On Feb 9, 8:27 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
If you choose to break that by using void * for everything and then
casting to random types, that's your problem, and it demonstrates
exactly nothing about the need to check anything in well-written C.
It's not my choice, see the reply to Bill Pursell. And of course it's
my problem, I have to use such code, be it well-written C or not.
If it's foisted on you, it's not a very good example in a discussion
about what is and is not good design, though, is it?
I'm using
int WINAPI WinMain(HINSTAN CE instance, HINSTANCE previnst__,
PSTR cmdline, int showmode)
because I have to. That doesn't mean I think it's good design to include
a function parameter you never use and haven't for years, or even that
the above is valid C.
Note that I didn't say that using sizeof *ptr is bad and that
sizeof(<type>) is good design. I was pointing out that in my opinion
sizeof(<type>) doesn't promote bad habits per se. To me, it's a
stylistic issue, and I haven't (yet) been convinced otherwise =) For
others of course, it may be more than that...
--
WYCIWYG - what you C is what you get
Yevgen Muntyan wrote:
Perhaps I should have said that I don't think casting return value of
malloc is a good idea? Instead of not-saying which gets interpreted
as saying-the-contrary.
Yevgen
Dude, cast malloc until the day you die then. Freedom for all malloc()
casters, la la la. It's not big enough to argue for a week, so I'm done with
this discussion.
Richard Bos wrote:
I'm using
int WINAPI WinMain(HINSTAN CE instance, HINSTANCE previnst__,
PSTR cmdline, int showmode)
because I have to. That doesn't mean I think it's good design to include
a function parameter you never use and haven't for years, or even that
the above is valid C.
Richard
Ugh. Win32.
You've got to love API calls that take 13 parameters - 7 of which can randomly
be set to 0 or NULL (just decide at the flip of a coin) and no effect will be
noticed.
matevzb wrote:
Note that I didn't say that using sizeof *ptr is bad and that
sizeof(<type>) is good design. I was pointing out that in my opinion
sizeof(<type>) doesn't promote bad habits per se. To me, it's a
stylistic issue, and I haven't (yet) been convinced otherwise =) For
others of course, it may be more than that...
--
WYCIWYG - what you C is what you get
I guess you think it looks cleaner or more advanced or something of that
nature. When in the long run it's just more freakin' work with zero gain.
Yevgen Muntyan wrote:
You snipped the piece where I said when I need to. Sure, you
can say that I can just avoid using malloc and alike in headers.
The same "simple solution" as was offered elsewhere: avoid
situations where you need cast and you will not need cast.
But I still believe that
#define ALLOC_A_THING(T ype) ((Type*) malloc (sizeof (Type)))
is not too bad.
Yevgen
I think you have a penchant for being needlessly contrary.
Yevgen Muntyan wrote:
Even with malloc this macro is more convenient than malloc(sizeof *foo)
or malloc(sizeof(F oo)) (given the name is better than
"custom_allocat or_here"). Some people even believe it's safer than
raw malloc. Or
#define FOO_NEW() ((Foo*) malloc (sizeof (Foo))) // *must* cast here
No. It's not. It's not clear, it's obtuse and pointless monkeying that
*reduces* visible intent.
Christopher Layne wrote:
Yevgen Muntyan wrote:
>Even with malloc this macro is more convenient than malloc(sizeof *foo) or malloc(sizeof(F oo)) (given the name is better than "custom_alloca tor_here"). Some people even believe it's safer than raw malloc. Or
#define FOO_NEW() ((Foo*) malloc (sizeof (Foo))) // *must* cast here
No. It's not. It's not clear, it's obtuse and pointless monkeying that
*reduces* visible intent.
What do you do if you need to replace malloc() with another allocator?
Grep? While this macro is indeed stupid, it was an example of where
cast is needed.
*If* you write a macro which allocates a Foo structure (this macro can
do arbitrary nice or complex things, and there *are* macros like that
in real code) you better make it of type Foo*.
Yevgen This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: disco |
last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th
edition - p. 672. There is no erata on this problem at the publisher's
website.
1) Is it a violation of copyright laws to post example code from a
book to a newsgroup?
2) The program crashes as it tries to free memory and I would like to
know the best way to correct THIS section of the code. By assigning
current = head, current now points to the first structure in...
|
by: f.oppedisano |
last post by:
Hi,
i would like to allocate two structures making only one malloc call.
So i do prt=malloc(sizeof(struct1)+sizeof(struct2));
After this operation i make two pointers one to the first struct (ptr1=ptr),
the other to second struct(ptr2=ptr+sizeof(struct2)).
These two pointer are later used by two threads in mutual exclusion so
thread1 can't access ptr2 and thread2 can't access ptr1.
At some time thread2 wants to free his part of memory but...
|
by: Rodrigo Dominguez |
last post by:
there are sometimes that I use third party libraries, I use some functions
that returns char * or structs, etc. sometimes the memory that is returned
by those libraries, when I try to free this memory whith the function free,
it brokes my application, and sometimes it's ok, why? how do I realize when
I have to free the memory that is allocated by third party libraries and
why sometimes I don't have to free this memory?
Thank you
|
by: Fernando Cacciola |
last post by:
Help me out here please:
While watching Brad Abraham's MSDN TV talk about the Dispose pattern,
refering to:
public virtual void Dispose ( bool disposing )
{
if ( disposing )
{
<-- WHAT GOES HERE -->
|
by: Atul Sureka |
last post by:
Hi,
I want to free the object memory in C# - like we do using 'delete' keyword
in C++.
Lets say I have an object of some class and I want to explicitly free the
memory.
C# do not have any free or delete keyword to do so.
Also I don't want to wait for the GC to be called.
| |
by: karthikbalaguru |
last post by:
Hi,
Will 'free' return the memory Immediately to the OS ?
Thx in advans,
Karthik Balaguru
|
by: david |
last post by:
I will past only two segments from the code it should be enough to see
what I did wrong, I think I know there I made a mistake, but how to
fix it I can not tell. This why I need help from you all.
Main code:
----------------
/* duomenu rasymas i faila */
dst = fopen(argv, "w");
if (dst != NULL) {
wordDBSize = sizeStack(wordDB);
|
by: vivek |
last post by:
Hello,
I have a pointer to a main structure which again consists of
structures, enums, char, int, float and again complex structures.
When i free all the contents of the main structure, it takes me a lot
of time (since i have to loop determining the data type and freeing
it). Is there any idea to free all the contents of the structure in
shortest possible time.
|
by: Andreas Eibach |
last post by:
Hi again,
one of the other big woes I'm having...
typedef struct perBlockStru /* (structure) Long words per block
*/
{
unsigned long *lword;
} lwperBlockStru_t;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |