Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #88719

Re: A string pointer to a static or dynamic string : how to free the dynamic one ?

Path csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c++
Subject Re: A string pointer to a static or dynamic string : how to free the dynamic one ?
Date Sun, 22 Jan 2023 10:57:32 -0800
Organization None to speak of
Lines 60
Message-ID <875ycyiehv.fsf@nosuchdomain.example.com> (permalink)
References <tqgspl$11vb$1@gioia.aioe.org> <61d28ae0-d30f-47f7-adc2-bdadfd20d443n@googlegroups.com> <87a62bie22.fsf@nosuchdomain.example.com> <tqj6jh$48g$3@gioia.aioe.org>
MIME-Version 1.0
Content-Type text/plain
Injection-Info reader01.eternal-september.org; posting-host="a96db803e771599752dfc0089ea20ee1"; logging-data="3383611"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vhFAw3Xe6BQa5x5T8cdM2"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock sha1:9HghClcgBN6WYA6pULijxUJlkDM= sha1:ydIspUgSgCsTYyOQMNtEaqmRuQA=
Xref csiph.com comp.lang.c++:88719

Show key headers only | View raw


"R.Wieser" <address@not.available> writes:
> "Keith Thompson" <Keith.S.Thompson+u@gmail.com> wrote in message 
> news:87a62bie22.fsf@nosuchdomain.example.com...
>
>> A *pointer to a string* is by definition "a pointer to its initial
>> (lowest addressed) character".
>
> To disambiguate :
>
> Assuming that a "pointer to a string" is something a function like strdup() 
> returns, what do you call the (type of) variable that such a result is 
> stored in ?
>
> ... it often confuses the h*ll outof me when the word "stringpointer" is 
> used for both. :-\

Note that we're talking about C; C++, the topic of this newsgroup, is a
related but distinct language.

What C calls a "string" is very close to (probably identical to) what
C++ calls a "null-terminated byte string", or NTBS.  The informal term
"C-style string" is common, to distinguish it from C++'s std::string.

In C, there is no string type (unless you define one yourself).  The
language-defined term "string" is used to refer to a data format, not a
data type.  An object of type "array of char" may or may not contain a
string (or multiple strings).

The C term "pointer to a string" is not analogous to, for example,
"pointer to an int".  It is a language-defined phrase because the term
"pointer to a string" either would not make sense or would have a
different meaning in the absence of that definition.  A pointer to a
string points to the initial (0th) element of the string, and can be
used via array arithmetic, indexing, and calls to library functions to
manipulate the string.

An object containing a *pointer to a string* is normally of type char*
or const char*.

Everything that can be done in C using C-style strings and C-style
string pointers can be done the same way in C++, but it's almost
always easier and safer to use std::string.

To answer your original question, given a char* value (say, a function
parameter), neither C nor C++ gives you a way to determine how the
memory it points to was allocated, and therefore how and whether it
should be deallocated.  If you want to pass pointers around and let the
called function decide whether and how to deallocate it, you'll have to
pass that information somehow, probably as another argument or by
wrapping the pointer in a structure or class.  Note also that memory
allocated by malloc() should be deallocated by calling free(), and
memory allocated by new should be deallocated by delete; mixing the two,
IIRC, has undefined behavior.

But again, std::string takes care of all of this for you.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-21 15:28 +0100
  Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-21 16:40 +0200
    Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-21 15:57 +0100
    Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-21 16:20 +0000
      Re: A string pointer to a static or dynamic string : how to free the Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-21 20:15 +0200
        Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 09:25 +0000
          Re: A string pointer to a static or dynamic string : how to free the Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-23 12:11 +0200
            Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 10:14 +0000
              Re: A string pointer to a static or dynamic string : how to free the Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-23 14:14 +0200
                Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 12:27 +0000
                Re: A string pointer to a static or dynamic string : how to free the Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-23 17:56 +0200
                Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 16:11 +0000
                Re: A string pointer to a static or dynamic string : how to free the Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-23 18:58 +0200
                Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 17:18 +0000
                Re: A string pointer to a static or dynamic string : how to free the Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-23 19:49 +0200
          Re: A string pointer to a static or dynamic string : how to free the Cholo Lennon <chololennon@hotmail.com> - 2023-01-23 16:44 -0300
            Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-24 17:05 +0000
      Re: A string pointer to a static or dynamic string : how to free the Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-21 19:30 +0100
        Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 09:30 +0000
          Re: A string pointer to a static or dynamic string : how to free the Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-23 13:53 +0100
            Re: A string pointer to a static or dynamic string : how to free the scott@slp53.sl.home (Scott Lurndal) - 2023-01-23 14:46 +0000
              Re: A string pointer to a static or dynamic string : how to free the Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-23 16:48 +0100
                Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 16:09 +0000
                Re: A string pointer to a static or dynamic string : how to free the Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-23 17:13 +0100
            Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 16:20 +0000
              Re: A string pointer to a static or dynamic string : how to free the Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-23 17:25 +0100
                Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 16:37 +0000
                Re: A string pointer to a static or dynamic string : how to free the Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-23 17:40 +0100
                Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 17:00 +0000
                Re: A string pointer to a static or dynamic string : how to free the Wuns Haerst <Wuns.Haerst@wurstfabrik.at> - 2023-01-23 20:07 +0100
                Re: A string pointer to a static or dynamic string : how to free the scott@slp53.sl.home (Scott Lurndal) - 2023-01-23 17:30 +0000
                Re: A string pointer to a static or dynamic string : how to free the Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-23 18:39 +0100
              Re: A string pointer to a static or dynamic string : how to free the "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2023-01-23 08:40 -0800
                Re: A string pointer to a static or dynamic string : how to free the Muttley@dastardlyhq.com - 2023-01-23 17:02 +0000
    Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Juha Nieminen <nospam@thanks.invalid> - 2023-01-23 06:34 +0000
      Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-23 12:49 +0200
        Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Juha Nieminen <nospam@thanks.invalid> - 2023-01-23 11:51 +0000
          Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-23 04:05 -0800
          Re: A string pointer to a static or dynamic string : how to free the dynamic one ? wij <wyniijj5@gmail.com> - 2023-01-23 04:45 -0800
  Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Richard Damon <Richard@Damon-Family.org> - 2023-01-21 10:13 -0500
    Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-21 17:53 +0100
  Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-21 08:04 -0800
    Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-21 16:54 -0800
      Re: A string pointer to a static or dynamic string : how to free the dynamic one ? jak <nospam@please.ty> - 2023-01-22 10:46 +0100
        Re: A string pointer to a static or dynamic string : how to free the dynamic one ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-01-22 05:20 -0500
          Re: A string pointer to a static or dynamic string : how to free the dynamic one ? jak <nospam@please.ty> - 2023-01-22 12:17 +0100
            Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Bo Persson <bo@bo-persson.se> - 2023-01-22 12:29 +0100
              Re: A string pointer to a static or dynamic string : how to free the dynamic one ? jak <nospam@please.ty> - 2023-01-22 13:23 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-22 14:53 +0200
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? jak <nospam@please.ty> - 2023-01-22 19:04 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? jak <nospam@please.ty> - 2023-01-22 19:16 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? David Brown <david.brown@hesbynett.no> - 2023-01-23 09:46 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? jak <nospam@please.ty> - 2023-01-23 13:22 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? David Brown <david.brown@hesbynett.no> - 2023-01-23 14:42 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-23 06:15 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2023-01-23 08:22 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? David Brown <david.brown@hesbynett.no> - 2023-01-23 18:02 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-23 11:14 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2023-01-23 11:38 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-23 13:26 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Richard Damon <Richard@Damon-Family.org> - 2023-01-22 13:24 -0500
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-22 11:00 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-01-22 15:39 -0500
            Re: A string pointer to a static or dynamic string : how to free the dynamic one ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-01-22 15:28 -0500
      Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-22 12:27 +0100
        Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Öö Tiib <ootiib@hot.ee> - 2023-01-22 03:42 -0800
          Re: A string pointer to a static or dynamic string : how to free the dynamic one ? David Brown <david.brown@hesbynett.no> - 2023-01-22 15:31 +0100
          Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-22 15:40 +0100
            Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Richard Damon <Richard@Damon-Family.org> - 2023-01-22 13:24 -0500
              Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-22 11:46 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? David Brown <david.brown@hesbynett.no> - 2023-01-23 10:24 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? scott@slp53.sl.home (Scott Lurndal) - 2023-01-23 14:44 +0000
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-23 07:34 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? David Brown <david.brown@hesbynett.no> - 2023-01-23 18:05 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-23 09:37 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? scott@slp53.sl.home (Scott Lurndal) - 2023-01-23 17:37 +0000
              Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-22 21:16 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-22 13:07 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-23 08:33 +0100
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-01-23 04:00 -0500
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-23 12:34 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-02 06:43 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2023-02-02 16:36 -0800
                Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-02 17:38 -0800
        Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-01-22 10:57 -0800
          Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-22 21:23 +0100
        Re: A string pointer to a static or dynamic string : how to free the dynamic one ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-01-22 15:47 -0500
          Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-23 08:04 +0100
            Re: A string pointer to a static or dynamic string : how to free the dynamic one ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-01-23 04:13 -0500
            Re: A string pointer to a static or dynamic string : how to free the dynamic one ? David Brown <david.brown@hesbynett.no> - 2023-01-23 10:33 +0100
  Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2023-01-21 18:29 +0000
    Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-21 20:09 +0100
      Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Richard Damon <Richard@Damon-Family.org> - 2023-01-21 16:32 -0500
        Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-22 10:52 +0100
      Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2023-01-21 22:10 +0000
        Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-22 11:56 +0100
          Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-22 14:31 +0200
            Re: A string pointer to a static or dynamic string : how to free the dynamic one ? "R.Wieser" <address@not.available> - 2023-01-22 15:57 +0100
        Re: A string pointer to a static or dynamic string : how to free the dynamic one ? Juha Nieminen <nospam@thanks.invalid> - 2023-01-23 06:40 +0000

csiph-web