473,769 Members | 3,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IS this a proper way of freeing memory with free()

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 4944
Ryan Ply <th******@earth ling.netwrote:
Lots of stuff that could, possibly, maybe happen. As for the "possibly
harmful for malloc" comment, its if malloc fails to be declared as
returning void *. Is there an place where it isn't? I suppose my point
is that I don't understand why I should program for things that have a
firm chance of a definite maybe of happening.
Good point. So why cast at all? It solves nothing that deserves solving,
it involves more keypresses, and it does degrade the warning value of
real casts. The last one is the killer, IMO: casting malloc() is like
hanging a High Voltage sign on every home outlet. It means that when you
_need_ the warning, you've been trained to think "Oh, just another
meaningless High Voltage sign/pointer cast, let's ignore it", and get
zapped to a crisp/get your program segfaulted. Don't train yourself not
to pay attention; don't cast malloc().

Richard
Feb 2 '07 #31
ri*****@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <bd************ @news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwro te:
>Then that must have been a non-C89 compliant version and the "few
years" is probably more than a decade (or you actually had forgotten
to include <stdlib.h>;-)
Or perhaps the system's stdlib.h didn't declare malloc()? Given the
existence of malloc.h on some systems, that wouldn't be too
surprising.
The the system's stdlib.h does not declare malloc then it is not C89
compliant and hence is covered by that Jens stated.

I read Jens' statement as implying that the version of *gcc* must more
than a decade old. But gcc generally uses the system headers and
libraries, so it might have been the system rather than gcc that was
out-of-date.
Possibly, but that's a poor cop-out, IMO. You work with an
implementation, not with two half-implementations . Ganoo (i.c.) passing
the buck to Aitch Pee (e.g.), and Aitch Pee passing the buck back to
Ganoo, makes both of them guilty for together delivering a broken
implementation, not neither of them. Neither the Standard nor the end
user cares, nor should they care, that parts of the implementation came
from different suppliers.

Richard
Feb 2 '07 #32
In article <45************ *****@news.xs4a ll.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwr ote:
>I read Jens' statement as implying that the version of *gcc* must more
than a decade old. But gcc generally uses the system headers and
libraries, so it might have been the system rather than gcc that was
out-of-date.
>Possibly, but that's a poor cop-out, IMO.
It might be a cop-out if someone from the gcc team said it, but I have
nothing to do with them, so it's just a possible explanation (perhaps
an unlikely one, as some have said).

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 2 '07 #33
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Good point. So why cast at all? It solves nothing that deserves solving,
it involves more keypresses, and it does degrade the warning value of
real casts.
Abbrev mode is your friend, assuming you use emacs that is. Some quick
reading of man pages as to proper compiler flags will solve the
warnings.
Also to Richard Heathfield:
Technique 1 (your way) requires you to include the <stdlib.hhead er
and requires you to type a pointer cast and requires you to fix the
cast if the type changes.
Technique 2 (which is what we like to call "the right way") requires
you to include the header.
So we're suggesting that you might like to do less work and get a
better, more maintainable program as a result. But hey, no pressure -
if you want to keep doing it in some way other than the right way,
that's up to you - I mean, some people just love to type, right?
stdlib.h is has been in my .c template for as long as I can remember,
I'm not sure why this newsgroup assumes it isn't. All I've said that
would allude to it not being there was in reference to an old gcc
compiler, probably 2.95 or earlier.

Its also not more keystrokes for me, I have abbrev lists, it also make
my code cleaner as I can compare the number of 'stars' * in the cast
versus the number inside the malloc and have virtually eliminated my
pointer problems by doing that. I'm suggesting that I'm doing less work
and getting a more maintainable program as a result. That's just me.

Ryan -
Feb 2 '07 #34
Ryan Ply <th******@earth ling.netwrites:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>Good point. So why cast at all? It solves nothing that deserves solving,
it involves more keypresses, and it does degrade the warning value of
real casts.

Abbrev mode is your friend, assuming you use emacs that is.
You have set up a tool to help you add unnecessary casts?
Some quick
reading of man pages as to proper compiler flags will solve the
warnings.
I think you miss Richard Bos's point. I think he means that one huge
advantage of casts is they alert one to problems. If you need one,
then things have got sticky. If you use them where they are not
needed they can't be used as a strong flag, drawing the reader's
attention to important problems.
Also to Richard Heathfield:
>Technique 1 (your way) requires you to include the <stdlib.hhead er
and requires you to type a pointer cast and requires you to fix the
cast if the type changes.
>Technique 2 (which is what we like to call "the right way") requires
you to include the header.
>So we're suggesting that you might like to do less work and get a
better, more maintainable program as a result. But hey, no pressure -
if you want to keep doing it in some way other than the right way,
that's up to you - I mean, some people just love to type, right?

stdlib.h is has been in my .c template for as long as I can remember,
I'm not sure why this newsgroup assumes it isn't. All I've said that
would allude to it not being there was in reference to an old gcc
compiler, probably 2.95 or earlier.

Its also not more keystrokes for me, I have abbrev lists, it also make
my code cleaner as I can compare the number of 'stars' * in the cast
versus the number inside the malloc and have virtually eliminated my
pointer problems by doing that.
What problems? If you use the recommended idiom:

p = malloc(<exp* sizeof *p);

you know the type will be correct, and no casts!
I'm suggesting that I'm doing less work
and getting a more maintainable program as a result. That's just
me.
Yes, just you!

--
Ben.
Feb 2 '07 #35
On 02 Feb 2007 08:33:03 -0800, in comp.lang.c , Ryan Ply
<th******@earth ling.netwrote:
>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>Good point. So why cast at all? It solves nothing that deserves solving,
it involves more keypresses, and it does degrade the warning value of
real casts.

Abbrev mode is your friend, assuming you use emacs that is.
You've programmed your environment to automatically do something
unnecessary and potentially harmful. Ask yourself why you'd want to do
that.
>Some quick
reading of man pages as to proper compiler flags will solve the
warnings.
You miss the point - with the cast in place, warnings are
/suppressed/, even when they would have been valid. With the cast
removed, the only warnings you should get are if you actually did make
a mistake.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 2 '07 #36
Ryan Ply wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:

>>Good point. So why cast at all? It solves nothing that deserves solving,
it involves more keypresses, and it does degrade the warning value of
real casts.


Abbrev mode is your friend, assuming you use emacs that is. Some quick
reading of man pages as to proper compiler flags will solve the
warnings.
Solve the warnings? Warnings are your friend, fix them, don't sweep
them under the the rug.

--
Ian Collins.
Feb 2 '07 #37
Ryan Ply wrote:
Its also not more keystrokes for me, I have abbrev lists, it also make
my code cleaner as I can compare the number of 'stars' * in the cast
versus the number inside the malloc and have virtually eliminated my
pointer problems by doing that. I'm suggesting that I'm doing less work
and getting a more maintainable program as a result. That's just me.

Ryan -
But you're being irrational and refusing to change your ways after being shown
that it is a pointless and sometimes dangerous way.

Go read up on pointers of type void.
Feb 2 '07 #38
Ben Bacarisse wrote:
Ryan Ply <th******@earth ling.netwrites:
>>
.... snip ...
>>
stdlib.h is has been in my .c template for as long as I can
remember, I'm not sure why this newsgroup assumes it isn't.
All I've said that would allude to it not being there was in
reference to an old gcc compiler, probably 2.95 or earlier.
Piggybacking here.

Why should you always include stdio.h? In many cases it is
unnecessary, and even not available (in some embedded systems). An
example is my implementation of strlcpy/cat, which is usable
unchanged on such systems. Just put in the things you need. The
other attitude leads to such abortions as windows.h.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 3 '07 #39
CBFalconer said:
Ben Bacarisse wrote:
>Ryan Ply <th******@earth ling.netwrites:
>>>
... snip ...
>>>
stdlib.h is has been in my .c template for as long as I can
remember, I'm not sure why this newsgroup assumes it isn't.
All I've said that would allude to it not being there was in
reference to an old gcc compiler, probably 2.95 or earlier.

Piggybacking here.

Why should you always include stdio.h?
Why indeed? But he didn't claim you should, or that he does, always include
<stdio.h- he was actually referring to <stdlib.h>, and he doesn't even
claim always to include /that/ - he merely said it was in his ".c
template". Presumably that's some kind of stamp with which he churns out C
source file skeleta - and presumably he can remove <stdlib.hfrom a given
source if it turns out not to be required therein.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 3 '07 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
22976
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...
12
3077
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...
11
2299
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
6
2766
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 -->
4
36539
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.
66
3706
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
9
3327
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);
11
2011
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.
25
4729
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;
0
9423
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,...
0
10210
Oralloy
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...
0
10039
jinu1996
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...
0
8869
agi2029
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...
1
7406
isladogs
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...
0
5297
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...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
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
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.