473,778 Members | 1,759 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 4772
On Fri, 1 Sep 2006, Ben Pfaff wrote:
Tak-Shing Chan <t.****@gold.ac .ukwrites:
>On Fri, 1 Sep 2006, Ben Pfaff wrote:
>>Tak-Shing Chan <t.****@gold.ac .ukwrites:

(3) In general, writing pointers to files is always
nonportabl e. So, I am not sure why you are posting this in a
group that values portability.

Writing a pointer to a file is *not* always non-portable. It is
portable to write a pointer to a file, read it back within the
same run of the program, and then use the pointer (as long as the
lifetime of the associated memory has not been reached).

An intervening realloc could mess it up.

realloc terminates the memory's lifetime.
I don't think so. The new object might overlap with the old
one. Perhaps you meant ``object'' rather than ``memory''?

Tak-Shing
Sep 1 '06 #51
Walter Roberson said:
In article <87************ @benpfaff.org>,
Ben Pfaff <bl*@cs.stanfor d.eduwrote:
>>Writing a pointer to a file is *not* always non-portable. It is
portable to write a pointer to a file, read it back within the
same run of the program, and then use the pointer (as long as the
lifetime of the associated memory has not been reached).

Reference, please?

My recollection is that the relevant section says only that
when something is written out to a binary file, that the same
binary value will be read back in. When, though, it comes to a pointer,
that doesn't promise that the reconstituted pointer points to anything.
fread and fwrite work "as if" they are successive calls to fgetc and fputc
respectively. These functions may ostensibly deal in ints, but the Standard
requires that they actually read in and write out unsigned chars. So fread
and fwrite effectively read and write objects as if they were a collection
of unsigned chars. In other words, what is being read/written is the object
representation.

Is it your contention that two objects of the same type with the same object
representation can have different values?

There is a section of the standard that talks about writing out
pointers and reading them back in, but that has to do with using
the %p printf() format, which Richard did not do.
Only because I couldn't face the sheer yukkiness of writing a scanf call.
:-)

--
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 #52
Tak-Shing Chan said:
On Fri, 1 Sep 2006, Ben Pfaff wrote:
>Tak-Shing Chan <t.****@gold.ac .ukwrites:
>> (3) In general, writing pointers to files is always
nonportable . So, I am not sure why you are posting this in a
group that values portability.

Writing a pointer to a file is *not* always non-portable. It is
portable to write a pointer to a file, read it back within the
same run of the program, and then use the pointer (as long as the
lifetime of the associated memory has not been reached).

An intervening realloc could mess it up.
There was no intervening realloc in the code which you said was
non-portable. If you had claimed "in general, writing pointers to files is
always non-portable if you realloc before you read them back in", that
would be different (and utterly irrelevant to the discussion). But you
didn't.

--
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 #53
Tak-Shing Chan <t.****@gold.ac .ukwrites:
On Fri, 1 Sep 2006, Ben Pfaff wrote:
>Tak-Shing Chan <t.****@gold.ac .ukwrites:
>>On Fri, 1 Sep 2006, Ben Pfaff wrote:

Tak-Shing Chan <t.****@gold.ac .ukwrites:

(3) In general, writing pointers to files is always
nonportable . So, I am not sure why you are posting this in a
group that values portability.

Writing a pointer to a file is *not* always non-portable. It is
portable to write a pointer to a file, read it back within the
same run of the program, and then use the pointer (as long as the
lifetime of the associated memory has not been reached).

An intervening realloc could mess it up.

realloc terminates the memory's lifetime.

I don't think so. The new object might overlap with the old
one. Perhaps you meant ``object'' rather than ``memory''?
Yes, that's more precise.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Sep 1 '06 #54
On Fri, 1 Sep 2006, Richard Heathfield wrote:
Tak-Shing Chan said:
>On Fri, 1 Sep 2006, Ben Pfaff wrote:
>>Tak-Shing Chan <t.****@gold.ac .ukwrites:

(3) In general, writing pointers to files is always
nonportabl e. So, I am not sure why you are posting this in a
group that values portability.

Writing a pointer to a file is *not* always non-portable. It is
portable to write a pointer to a file, read it back within the
same run of the program, and then use the pointer (as long as the
lifetime of the associated memory has not been reached).

An intervening realloc could mess it up.

There was no intervening realloc in the code which you said was
non-portable. If you had claimed "in general, writing pointers to files is
always non-portable if you realloc before you read them back in", that
would be different (and utterly irrelevant to the discussion). But you
didn't.
In any case, it would hide serious errors (such as having an
intervening realloc or free) where conforming implementations are
not required to issue a diagnostic.

Tak-Shing
Sep 1 '06 #55
Tak-Shing Chan wrote:
On Fri, 1 Sep 2006, Ben Pfaff wrote:
>Tak-Shing Chan <t.****@gold.ac .ukwrites:
>>On Fri, 1 Sep 2006, Ben Pfaff wrote:

Tak-Shing Chan <t.****@gold.ac .ukwrites:

(3) In general, writing pointers to files is always
nonportable . So, I am not sure why you are posting this in a
group that values portability.
Writing a pointer to a file is *not* always non-portable. It is
portable to write a pointer to a file, read it back within the
same run of the program, and then use the pointer (as long as the
lifetime of the associated memory has not been reached).
An intervening realloc could mess it up.


realloc terminates the memory's lifetime.


I don't think so. The new object might overlap with the old
one. Perhaps you meant ``object'' rather than ``memory''?

Tak-Shing
You raise a very importaznt point here, that furthers the arguments for
an automatic garbage collector.

When you do a

q = realloc(p,2*n);

after a successful realloc the p pointer and ALL THE ALIASES you have
done for that object, "including but not limited to":

o structures that contain that pointer in some field
o functions, that hold aliases to that object address in the stack
in their parameter list
o other local variables that use pointers based on that pointer,
for instance p+5 or &p[78]

ARE ALL INVALID and must be ALL invalidated!!!

Manually.

Using the GC you do

q = GC_malloc(2*n);
if (q) {
memcpy(q,p,n); // Copy the old object
p = NULL;
}
else
//No more memory Handle error

Now q contains the reallocated object but the old
storage will be freed only (and only then) when there are no
pointers to it!

Obviously, if you are 100% sure that there are no aliases to
the object, you can use GC_realloc();

But this is not without problems too, since the aliases will
point to old object copies and not to the good one, and
those copies will NOT be updated.

This can be a problem, or may be is harmless, it depends on the
application. For instance if the new object is updated by
adding members to it (a table of structures for instance) it
can be OK to keep the old shorter version, but that could also be dangerous.

In the case of
struct {
int nbOfElements;
T data;
};

after a realloc of "data", the old pointer stored elsewhere
will be utterly wrong after a realloc, and a GC solution could make
the problem even worse.

But this is a matter of discipline of alias usage and discipline
of pointer usage in general.

Another thread of discussion.
Yes, memory management *is* tricky.

Sep 1 '06 #56
Tak-Shing Chan said:

<snip>
In any case, it would hide serious errors
What would? You seem to be talking about hypothetical code hiding
hypothetical errors. The actual posted code seems to have passed you by
completely.

--
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 #57
Richard Heathfield wrote:
Philip Potter said:
>>You seem to be arguing that because garbage collection can't protect
against all memory leaks without care from the user, it is worthless.


No, I'm sure it has a place in other languages. I just don't think it sits
very well with C, for at least two reasons:

1) As we have already seen upthread, automatic garbage collection (AGC)
encounters serious difficulties when faced with C's heavy use of pointers
for keeping track not only of memory blocks but of particular positions
within them.

2) C is commonly used where not only performance but even timing (in
real-time stuff) is an important criterion for acceptance of the program,
and AGC implementations are notorious for introducing arbitrary delays at
various times through a program's run.

As I have already agreed, AGC is undoubtedly of value to some people in some
situations. I just don't think AGC is a good fit for C programmers, in the
general case. That's not to say that it might not be useful on occasion,
but I don't believe such occasions are sufficiently common to make the
subject a worthwhile diversion away from the topic of this newsgroup, which
is C programming, *not* "C programming + Jacob Navia's AGC extensions".
One situation where GC can be used to good effect with C is as a
diagnostic tool. I often soak test applications linked with a GC
library that logs reclaimed memory and use the library to highlight any
memory leeks that occur. If the application is correctly written, the
GC shouldn't reclaim any blocks.

I would never countenance using the GC library as a band-aid for poor
programming practice, but as another tool in the box, it has its place.

--
Ian Collins.
Sep 1 '06 #58
Ian Collins said:

<snip>
One situation where GC can be used to good effect with C is as a
diagnostic tool.
Absolutely. Likewise automatic bounds checking, with which I would never
burden production code but which has its place in a test rig.
I often soak test applications linked with a GC
library that logs reclaimed memory and use the library to highlight any
memory leeks that occur.
Not broccoli, then? :-)

Seriously, I don't actually use a GC lib for detecting memory leaks, but I
do use a wrapper around *alloc and free which I wrote specifically for
performing such detections. But it really slows the code down, and
generates colossal logfiles, so it gets ripped out of production code via a
#define (or rather, the absence of one).

--
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 #59
Richard Heathfield wrote:
Ian Collins said:

<snip>
>>One situation where GC can be used to good effect with C is as a
diagnostic tool.


Absolutely. Likewise automatic bounds checking, with which I would never
burden production code but which has its place in a test rig.

>>I often soak test applications linked with a GC
library that logs reclaimed memory and use the library to highlight any
memory leeks that occur.


Not broccoli, then? :-)
Must be out of season....
Seriously, I don't actually use a GC lib for detecting memory leaks, but I
do use a wrapper around *alloc and free which I wrote specifically for
performing such detections. But it really slows the code down, and
generates colossal logfiles, so it gets ripped out of production code via a
#define (or rather, the absence of one).
That's one advantage of the GC library, it imposes minimal overhead and
only logs real or potential leaks.

--
Ian Collins.
Sep 1 '06 #60

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.