Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?




Herbert Rosenau <os2guy@xxxxxxxxxxxxx> wrote in message
news:wmzsGguTDN6N-pn2-ysKU5ddsUS8X@xxxxxxxxxxxxxxxxxxxxxxxxx
On Thu, 17 Aug 2006 23:54:41 UTC, "Bill Reid"
<hormelfree@xxxxxxxxxxxxxxxx> wrote:

I guess I got the last two conditions conflated in my mind; it just
seemed
logical to me that if realloc() failed it would free the previous block.
It SEEMS like it should.

But it does not so because you may need to continue your work with the
old block.

Sure, but right off-hand I can't think of single case where I would
actually want to do anything with "half a loaf". When memory
allocation fails in whole or in part, I just want to get out of there
as quickly as possible, maybe just break out of that particular
module operation, quite often just to exit the whole program.

I suspect that is true of the vast majority of programs out there.
So not automatically freeing the block seems to be a case of "the
needs of the few outweighing the needs of the many".

When you have no need for the old block you have to free() that
yourself, else you should free() it. In any case you needs its
address.

In this imperfect world, I guess so...what a friggin' hassle...

As an orthogonal point, what horrible things happen if you try
to free() a NULL pointer?

Nothing. free(NULL); works like a noop. Nothing occures. That is
guaranteed.

So I've kind of been wasting a little time with these types of
generalized pre-exit memory cleanup routines:

void free_time_series_mem(void) {
unsigned ts_idx;

for(ts_idx=0;ts_idx<TS_MAX;ts_idx++) {
if(time_series[ts_idx]!=NULL) {
free(time_series[ts_idx]);
time_series[ts_idx]=NULL;
}
}

num_series=0;
}

Not only don't I need:
if(time_series[ts_idx]!=NULL)

What the hell is the point of:
time_series[ts_idx]=NULL;

??? Who knows...what the hell was I thinking...oh, wait, I WASN'T
thinking...

However, note the clever use of the "TS_MAX" define; NO "MAGIC
NUMBER" HERE FOR THIS BOY!!!

---
William Ernest Reid



.



Relevant Pages

  • Re: The Chinese Religion
    ... Fong Bin in Hui Neng sutra saw Bodhidharma in India and was ... It is all in the mind. ... the unlucky person's spirit bled. ... The real hell is on earth. ...
    (soc.culture.malaysia)
  • Re: OT: I have a new job and Im depressed.
    ... It's like who am I kidding trying to lead a what society deems a normal life. ... The Buddhist's believe hell is a state of mind. ... ChrisC needs a hug and re-assurance. ... " If you can empty your mind of all thoughts your heart will embrace the tranquility of peace. ...
    (alt.support.stop-smoking)
  • Re: I have a new job and Im depressed.
    ... I always get negative voices, ... Shit why is life so tough. ... Well I'm in hell. ... " If you can empty your mind of all thoughts your heart will embrace the ...
    (uk.people.support.depression)
  • Re: OT: I have a new job and Im depressed.
    ... it because I fear that I'm going to fuck this up, ... Shit why is life so tough. ... Well I'm in hell. ... " If you can empty your mind of all thoughts your heart will embrace the ...
    (alt.support.stop-smoking)
  • OT: I have a new job and Im depressed.
    ... It's like who am I kidding trying to lead a what society deems a normal ... Shit why is life so tough. ... Well I'm in hell. ... " If you can empty your mind of all thoughts your heart will embrace the ...
    (alt.support.stop-smoking)