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
Richard Heathfield wrote:
Yevgen Muntyan said:
>Richard Heathfield wrote:
<snip>
>>I fail to see why:
p = ALLOC_A_THING(f oo);
is more convenient than:
p = malloc(sizeof *p);
You fail to see, I don't. I like ALLOC_A_THING because it describes the intent and hides the actual memory allocation.
No, it doesn't, at least no more than malloc does. And even if it did,
which it doesn't, you still don't need the cast.
malloc(sizeof *ptr)
doesn't clearly state what it does. For you it does, you are used
to this construct and it's crystal clear to you. This is a style issue.
No less than
if (foo) {
do_something ();
}
vs
if (foo)
{
do_something ();
}
>
>How about custom_allocato r_here() function?
Yeah, you wouldn't need a cast there either.
Nah, tell how more convenient is
ptr = custom_allocato r_with_some_opt ions (sizeof *ptr, OPTIONS);
than
ptr = ALLOC_A_THING(T ype);
<snip>
>>>Or
#define FOO_NEW() ((Foo*) malloc (sizeof (Foo))) // *must* cast here Why? The cast doesn't do any good,
It saves your butt if you change type,
So let me get this straight - you're saying that if you're daft enough
to use the type in the sizeof, you have to write some otherwise
unnecessary code to give the compiler a fighting chance of warning you
about your daft decision, and only at the risk of suppressing another
warning about your daft decision?
Sorry, which one? Again casting from int to pointer? Oh yeah, compilers
are not obliged by holy standard to emit warnings about missing
prototypes, bummer.
You think that
ptr = malloc (sizeof *ptr)
is a nice workaround for typeless return value of malloc, I don't. I
think
((Type*) malloc (sizeof (Type)))
is better because it has a type. And, say, I like g_new(Foo, 1) better
because it says "allocate one Foo structure".
I can do
return g_new(Foo, 1);
or
Foo *ptr = g_new (Foo, 1);
Anyway, here's another real piece of code: http://svn.gnome.org/viewcvs/glib/tr....h?view=markup
You can claim how much you want that I am stupid, authors
of that code are stupid, that it's nice to use void* when you
allocate memory, but not everyone agrees with that. Now you can
repeat the killer argument about Elvis to complete proof
of you being right.
Yevgen
Yevgen Muntyan wrote:
malloc(sizeof *ptr)
doesn't clearly state what it does. For you it does, you are used
to this construct and it's crystal clear to you. This is a style issue.
No less than
Yes it does clearly state it. Go read up on the C language.
Nah, tell how more convenient is
ptr = custom_allocato r_with_some_opt ions (sizeof *ptr, OPTIONS);
than
ptr = ALLOC_A_THING(T ype);
ALLOC_A_THING() does not take options. They aren't even the same. You're
arguing specific purpose vs generic purpose. Go back and try again.
You think that
ptr = malloc (sizeof *ptr)
is a nice workaround for typeless return value of malloc, I don't. I
think
This is NOT a workaround. It has NOTHING to do with types. It has everything
to do with tying the size to the identifier itself and indirectly that size
being a *resultant value base on the identifier* which means you change
things in *one* place, not *two*.
((Type*) malloc (sizeof (Type)))
is better because it has a type. And, say, I like g_new(Foo, 1) better
because it says "allocate one Foo structure".
I can do
Please go read up on C again or something along those lines. You don't NEED to
cast the value returned. It does not do anything for the type, and providing
an actual type to sizeof does not influence malloc() in any way. malloc()
does not care about types! It allocates dynamic memory, that's it.
It seems at this point your insistance on the applicability of casting is
borne out of a refusal to perceive things in a different way than you have
trained yourself to do.
Richard Heathfield wrote:
Yevgen Muntyan said:
>Richard Heathfield wrote:
<snip>
>>I fail to see why:
p = ALLOC_A_THING(f oo);
is more convenient than:
p = malloc(sizeof *p);
You fail to see, I don't. I like ALLOC_A_THING because it describes the intent and hides the actual memory allocation.
No, it doesn't, at least no more than malloc does. And even if it did,
which it doesn't, you still don't need the cast.
>How about custom_allocato r_here() function?
Yeah, you wouldn't need a cast there either.
It seems I forgot that you're talking here about strictly conforming
code here. Indeed, if you have a strictly conforming program then
you're limited to bunch of C files without any non-standard headers,
and any custom allocator business or libraries or anything like that
is out question. A header like
foo.h:
#define FOO_NEW() ...
simply can't be in a strictly conforming program and you use
only malloc(whatever ) (or calloc, etc.) in your code. Yes, using only
standard features promotes use of raw malloc, and yes, casting
raw malloc in regular code like
int func (..)
{
...
ptr = malloc (anything);
...
}
is not good.
Yevgen
Christopher Layne wrote:
Yevgen Muntyan wrote:
>malloc(sizeo f *ptr)
doesn't clearly state what it does. For you it does, you are used to this construct and it's crystal clear to you. This is a style issue. No less than
Yes it does clearly state it. Go read up on the C language.
>Nah, tell how more convenient is
ptr = custom_allocato r_with_some_opt ions (sizeof *ptr, OPTIONS);
than
ptr = ALLOC_A_THING(T ype);
ALLOC_A_THING() does not take options. They aren't even the same. You're
arguing specific purpose vs generic purpose. Go back and try again.
>You think that
ptr = malloc (sizeof *ptr)
is a nice workaround for typeless return value of malloc, I don't. I think
This is NOT a workaround. It has NOTHING to do with types. It has everything
to do with tying the size to the identifier itself and indirectly that size
being a *resultant value base on the identifier* which means you change
things in *one* place, not *two*.
>((Type*) malloc (sizeof (Type)))
is better because it has a type. And, say, I like g_new(Foo, 1) better because it says "allocate one Foo structure". I can do
Please go read up on C again or something along those lines. You don't NEED to
cast the value returned. It does not do anything for the type, and providing
an actual type to sizeof does not influence malloc() in any way. malloc()
does not care about types! It allocates dynamic memory, that's it.
Exactly, malloc is typeless. And it's good to wrap blind typeless malloc
into a macro which gets an expression which does have type. I NEED to
cast return value to get an expression with type, because I want that
expression to have type. I NEED an expression which reads "allocate one
Foo structure". I don't like an expression which reads "allocate
sizeof(the-type-of-this-pointer-dereferenced) bytes". I want compiler
to warn me if I assign that Foo* to a variable of wrong type or
return it from a function with different return type, or feed it to
a function with different type of argument.
It seems at this point your insistance on the applicability of casting is
borne out of a refusal to perceive things in a different way than you have
trained yourself to do.
I guess you're right here to some extent. Or maybe I refuse to do things
in a worse way than I do them now?
Yevgen
Richard Bos wrote:
"matevzb" <ma*****@gmail. comwrote:
>On Feb 8, 11:51 pm, CBFalconer <cbfalco...@yah oo.comwrote:
>>matevzb wrote: I tend to disagree on this one. If you change the type of "ptr" you *should* examine *all* the places where "ptr" was used anyhow (not just the line with malloc). If you don't, that's promoting bad habits and that may bite harder than the x*sizeof(<type> ). I personally always use the latter and have never had any trouble with it. Not necessary. If you have avoided unnecessary casts, the compiler will point out any harmful type mismatches.
No, it won't in case of void *. A hypothetical case: #include <stdlib.h> #include <string.h>
typedef enum { SHORT = 0, LONG = 1 } mytype;
int fcn (void *array, mytype type, size_t size) { void *ptr;
switch (type) { case SHORT: ptr = malloc (size * sizeof(short));
You didn't read the construct you're criticising, did you? You didn't
even read the reason why that construct is better, because here you're
demonstrating exactly why it is better than this code.
Again:
ptr=malloc(num * sizeof *ptr);
is always less likely to break than
ptr=malloc(num * sizeof(type));
It's likely to break hard when ptr has different type from what
really needs to be allocated. And that happens (and you said "always").
If you choose to break that by using void * for everything and then
casting to random types, that's your problem,
No, it's your problem that you apply this kind of arguments like
"casting to random types", so that the guy you are replying to
sounds like an idiot which really casts stuff to random types.
Yevgen
Yevgen Muntyan wrote:
> ptr=malloc(num * sizeof *ptr);
is always less likely to break than
ptr=malloc(num * sizeof(type));
It's likely to break hard when ptr has different type from what
really needs to be allocated. And that happens (and you said "always").
Which is more likely to happen with the second case. You're flip flopping
around here.
>If you choose to break that by using void * for everything and then casting to random types, that's your problem,
No, it's your problem that you apply this kind of arguments like
"casting to random types", so that the guy you are replying to
sounds like an idiot which really casts stuff to random types.
Okay programmer insecurity counseling time.
Yevgen Muntyan wrote:
>>#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
What are you NOT getting about the fact that the cast is not needed in ISO C?
Do you know the purpose and characteristics of void pointers?
malloc() takes a size_t, that is IT. It does not talk to, worth with, or have
beers with sizeof to figure out what the type is.
Yevgen Muntyan wrote:
It seems I forgot that you're talking here about strictly conforming
code here. Indeed, if you have a strictly conforming program then
[...]
foo.h:
#define FOO_NEW() ...
simply can't be in a strictly conforming program and you use
only malloc(whatever ) (or calloc, etc.) in your code. Yes, using only
standard features promotes use of raw malloc, and yes, casting
raw malloc in regular code like
At this point I can't tell if you're trolling or just don't know what you're
talking about Yevgen.
Richard Bos wrote:
Ryan Ply <th******@earth ling.netwrote:
>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>>I don't use Emac-OS, and even if I did, abrv. mode would not solve the more important problem of making the malloc()-caster less attentive. As for "solving" the warnings, that's like "solving" a stinging pain in your right side by taking morphine. Yes, it _does_ get rid of the pain, but you'll still die of a burst appendix sooner or later. It's better get rid of the cause of the pain (read: warning messages) than to ignore it.
People just like reading whatever they want instead of whats on the screen in front of them. Someone said that by casting it would hide or mask the compilers warnings to me. I put extra flags in so that these warnings return as if nothing happened. Thus "solved".
Wrong.
> I program for real targets, not would be possible maybe in some day embedded targets 20 years form now possibly.
The meaning of that sentence is as muddled as its grammar.
>That is how specific compiler options are safe to use for me.
Wrong.
> When I get a project with a vague target then things will be different, but that hasn't happened to date.
But don't mind me; just don't come whining when your habit of ignoring
valid warnings comes back to bite you in the arse.
Since the discussing went too far from here, let me repeat the MAIN
point. I was replying to this thing, that Richard Bos said Ryan Ply
ignored valid warnings while Ryan Ply actually made his compiler
to warn about real problem. The real (potential) problem is missing
header which leads to UB, not converting int to pointer which is a bogus
side effect.
And what I am saying is: you got to read very very carefully
what you are replying to if you are going to use these stupid
"Wrong period Don't come whining when...".
Yevgen
Christopher Layne wrote:
Yevgen Muntyan wrote:
>>>#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
What are you NOT getting about the fact that the cast is not needed in ISO C?
Do you know the purpose and characteristics of void pointers?
You're not kidding, are you? I said *I need* cast because I want a safer
expression. If I apply cast, the expression acquires a type.
((Type*) malloc (sizeof (Type)))
is a standalone expression of type Type* (and it's an allocated chunk
of memory). Of course I can avoid cast here, but I believe void* is
worse than Type* when I need Type*.
I do get the fact that cast is not needed in ISO C.
malloc() takes a size_t, that is IT. It does not talk to, worth with, or have
beers with sizeof to figure out what the type is.
Yep, and this is why I apply cast to make an expression which has beer
with its argument every Friday.
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: 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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |