473,769 Members | 5,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

General method for dynamically allocating memory for a string

I have searched the internet for malloc and dynamic malloc; however, I still
don't know or readily see what is general way to allocate memory to char *
variable that I want to assign the substring that I found inside of a
string.

Any ideas?
Aug 30 '06
94 4767
Richard Heathfield wrote:
jacob navia said:

>>CBFalconer wrote:
>>>Revise the example:

T *p;

if (p = malloc(n * sizeof *p) {
T *q;

q = p+1; p = NULL;
dosomethingwith (q);
q--;
....
}

The revised example will not fool the GC since the block can be reached
from q, since it points between the beginning and the end of the block.


<shrugFinding ways to fool it is a moderately trivial exercise:

p = malloc(n * sizeof *p);
pos = ftell(fp);
fwrite(&p, sizeof p, 1, fp);
p = NULL;
fseek(fp, pos, SEEK_SET);
fread(&p, sizeof p, 1, fp);
Obvious.

The GC documentation makes usages like this explicitely off limits.
The GC will not look for pointers:

o in the windows extra bytes provided by Microsoft
o in the hard disk (!!!)
o in your grandmother's mail where she said:

Why did you send me that pointer 0xFF45AC son?

I give it back, I do not needed it.

Sep 1 '06 #31
jacob navia said:
Richard Heathfield wrote:
>>
<shrugFindin g ways to fool it is a moderately trivial exercise:

p = malloc(n * sizeof *p);
pos = ftell(fp);
fwrite(&p, sizeof p, 1, fp);
p = NULL;
fseek(fp, pos, SEEK_SET);
fread(&p, sizeof p, 1, fp);

Obvious.
Indeed. So much for automatic garbage collection.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 1 '06 #32
Richard Heathfield wrote:
jacob navia said:

>>Richard Heathfield wrote:
>>><shrugFindin g ways to fool it is a moderately trivial exercise:

p = malloc(n * sizeof *p);
pos = ftell(fp);
fwrite(&p, sizeof p, 1, fp);
p = NULL;
fseek(fp, pos, SEEK_SET);
fread(&p, sizeof p, 1, fp);

Obvious.


Indeed. So much for automatic garbage collection.
But what are you trying to prove with that nonsense Heathfield?

Imagine I say:

void foo(void)
{
char *p = malloc(1024);

for (int i=0; i<20; i++) {
free(p); // Crash here
}
}

Then I conclude:

"So much for malloc/free usage".
Wouldn't be stupid to conclude that because incorrect usage
the software crashes?

Is it possible to discuss here technical opinions without
trying just to prove at all costs that the other opinion is
just nonsense?

Note that I am not saying that malloc/free should not be used
neither that they are "bad software", but that the GC provides a very
useful alternative for people that do not like to mess with the
associated problems of manual garbage collection (malloc/free).

jacob
Sep 1 '06 #33
jacob navia <ja***@jacob.re mcomp.frwrote:
Richard Heathfield wrote:
<shrugFinding ways to fool it is a moderately trivial exercise:

p = malloc(n * sizeof *p);
pos = ftell(fp);
fwrite(&p, sizeof p, 1, fp);
p = NULL;
fseek(fp, pos, SEEK_SET);
fread(&p, sizeof p, 1, fp);

Obvious.

The GC documentation makes usages like this explicitely off limits.
Well, then it isn't C, is it?

Richard
Sep 1 '06 #34
Richard Bos wrote:
jacob navia <ja***@jacob.re mcomp.frwrote:

>>Richard Heathfield wrote:
>>><shrugFindin g ways to fool it is a moderately trivial exercise:

p = malloc(n * sizeof *p);
pos = ftell(fp);
fwrite(&p, sizeof p, 1, fp);
p = NULL;
fseek(fp, pos, SEEK_SET);
fread(&p, sizeof p, 1, fp);

Obvious.

The GC documentation makes usages like this explicitely off limits.


Well, then it isn't C, is it?

Richard
Of course. Making a restriction in the usage of
a library makes it "not c".

Then, freeing twice an object (a restriction in the usage of free() )
makes free() "not c".

What kind of logic do you use?
Sep 1 '06 #35
Richard Heathfield wrote:
jacob navia said:

<snip>
>>Yes, this can be a problem with the GC.


So let's not do that, then.

>>As you may know, I am not selling anything here,


No, I don't know that. There is considerable evidence to the contrary.
What am I "selling"?

A product for zero dollars and zero cents?

Sep 1 '06 #36
"Richard Heathfield" <in*****@invali d.invalidwrote in message
news:8a******** ************@bt .com...
jacob navia said:
It is interesting that none of the posters considers GC and how
it would enormously SIMPLIFY the writing of this code.

Garbage collection is too important to be left to the system - or, at
least,
in comp.lang.c it is. The Lithp people will no doubt have a different
opinion, but that's their problem.
....and elsethread...

Richard Heathfield wrote:
>jacob navia said:
>Richard Heathfield wrote:
>>>
<shrugFindi ng ways to fool it is a moderately trivial exercise:

p = malloc(n * sizeof *p);
pos = ftell(fp);
fwrite(&p, sizeof p, 1, fp);
p = NULL;
fseek(fp, pos, SEEK_SET);
fread(&p, sizeof p, 1, fp);

Obvious.

Indeed. So much for automatic garbage collection.
Richard, while you have earned a lot of my respect and admiration through
your posting, I do feel that you are on a little bit of a crusade here. You
seem to be arguing that because garbage collection can't protect against all
memory leaks without care from the user, it is worthless. This is a somewhat
ridiculous argument - after all, manual memory management has precisely the
same property.

GCs don't give you a license not to think, but I didn't see anybody arguing
that they do.

Philip

Sep 1 '06 #37
jacob navia said:
Richard Heathfield wrote:
<snip>
>So much for automatic garbage collection.

But what are you trying to prove with that nonsense Heathfield?
If by "nonsense" you mean "correct C code", I am trying to prove, and
succeeding in proving, that automatic garbage collectors will struggle to
cope with correct C code.
Imagine I say:

void foo(void)
{
char *p = malloc(1024);

for (int i=0; i<20; i++) {
free(p); // Crash here
}
}

Then I conclude:

"So much for malloc/free usage".
It's not valid C. The example I gave /is/ valid C.
Wouldn't be stupid to conclude that because incorrect usage
the software crashes?
The example I gave is correct C. The example you gave is not.
Is it possible to discuss here technical opinions without
trying just to prove at all costs that the other opinion is
just nonsense?
Yes, and it happens all the time. Observe the technical discussions that
take place amongst those who actually know what they're talking about, and
you will see this very plainly.
Note that I am not saying that malloc/free should not be used
neither that they are "bad software", but that the GC provides a very
useful alternative for people that do not like to mess with the
associated problems of manual garbage collection (malloc/free).
Since this "very useful alternative" can't cope with correct C code, it
doesn't seem terribly useful to programmers who are using C. It may well be
useful to programmers who are using some other language, but that would be
a topic for some other newsgroup, not the comp.lang.c newsgroup.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 1 '06 #38
jacob navia said:
Richard Bos wrote:
>jacob navia <ja***@jacob.re mcomp.frwrote:
<snip>
>>>
The GC documentation makes usages like this explicitely off limits.


Well, then it isn't C, is it?
Of course. Making a restriction in the usage of
a library makes it "not c".
What he means, I think, is that the kind of automatic garbage collection you
advocate cannot be used reliably in place of manual garbage collection in
arbitrary C programs, because it fails to deal with some C constructs that
are perfectly legal. It may be useful to those who are prepared to code
around its limitations, but unless you are able to persuade ISO to change
the language definition to forbid C constructs that confuse the collector,
that collector cannot reasonably be advanced as a generic replacement for
manual garbage collection.
>
Then, freeing twice an object (a restriction in the usage of free() )
makes free() "not c".
Not so. It means that freeing a block of memory /twice/ is "not C".
What kind of logic do you use?
He uses the normal kind. I'm not so sure what logic /you/ are using but,
whatever kind it is, it doesn't work very well in persuading other people
that you have a valid point.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 1 '06 #39
jacob navia said:
Richard Heathfield wrote:
>jacob navia said:
<snip>
>>>As you may know, I am not selling anything here,

No, I don't know that. There is considerable evidence to the contrary.
What am I "selling"?

A product for zero dollars and zero cents?
I didn't claim you were any good at it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 1 '06 #40

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

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.