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


Groups > comp.lang.c++ > #88676 > unrolled thread

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

Started by"R.Wieser" <address@not.available>
First post2023-01-21 15:28 +0100
Last post2023-01-23 06:40 +0000
Articles 20 on this page of 99 — 20 participants

Back to article view | Back to comp.lang.c++


Contents

  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

Page 1 of 5  [1] 2 3 4 5  Next page →


#88676 — A string pointer to a static or dynamic string : how to free the dynamic one ?

From"R.Wieser" <address@not.available>
Date2023-01-21 15:28 +0100
SubjectA string pointer to a static or dynamic string : how to free the dynamic one ?
Message-ID<tqgspl$11vb$1@gioia.aioe.org>
Hello all,

I'm a rather newbie to C++ programming who is trying figure out how to deal 
with string pointers - or rather, with what they point at.

Case in point :

  char* message = "hello world";
  char* message = strdup("hello world");

I can user either of those in a function and return the pointer, and the 
caller will be none-the-wiser which form (the static or dymnamic string) it 
gets.

The problem is that the dynamic one needs to be "free"d but tyhe static one 
not.   How do I look at that "message" pointer whats "in" it so I can take 
the correct action.

By the way: the same thing goes for when I want to replace a string. I don't 
think that C++ has a garbage-collector running, so I need to do it myself. 
:-)

Regards,
Rudy Wieser

[toc] | [next] | [standalone]


#88677

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-01-21 16:40 +0200
Message-ID<tqgtgk$2kndm$1@dont-email.me>
In reply to#88676
21.01.2023 16:28 R.Wieser kirjutas:
> Hello all,
> 
> I'm a rather newbie to C++ programming who is trying figure out how to deal
> with string pointers - or rather, with what they point at.
> 
> Case in point :
> 
>    char* message = "hello world";
>    char* message = strdup("hello world");
> 
> I can user either of those in a function and return the pointer, and the
> caller will be none-the-wiser which form (the static or dymnamic string) it
> gets.

This is C++, so just return a std::string from your function, and forget 
everything about C-style strings, malloc, free and strdup. Good riddance!



[toc] | [prev] | [next] | [standalone]


#88678

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-21 15:57 +0100
Message-ID<tqguf9$2ktod$1@dont-email.me>
In reply to#88677
Am 21.01.2023 um 15:40 schrieb Paavo Helde:

> This is C++, so just return a std::string from your function, and forget 
> everything about C-style strings, malloc, free and strdup. Good riddance!

And local variables which are top-level variables (f.e. not part of
a pair) are implicitly move constructed. So you won't have to move
the return.

[toc] | [prev] | [next] | [standalone]


#88684 — Re: A string pointer to a static or dynamic string : how to free the

FromMuttley@dastardlyhq.com
Date2023-01-21 16:20 +0000
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqh3d7$376$1@gioia.aioe.org>
In reply to#88677
On Sat, 21 Jan 2023 16:40:20 +0200
Paavo Helde <eesnimi@osa.pri.ee> wrote:
>21.01.2023 16:28 R.Wieser kirjutas:
>> Hello all,
>> 
>> I'm a rather newbie to C++ programming who is trying figure out how to deal
>> with string pointers - or rather, with what they point at.
>> 
>> Case in point :
>> 
>>    char* message = "hello world";
>>    char* message = strdup("hello world");
>> 
>> I can user either of those in a function and return the pointer, and the
>> caller will be none-the-wiser which form (the static or dymnamic string) it
>> gets.
>
>This is C++, so just return a std::string from your function, and forget 
>everything about C-style strings, malloc, free and strdup. Good riddance!

Unless you want to parse the string which may involve chopping it about. Then
its simpler to use a C string and pointers rather than mess about with 
inefficient substr() etc. 

[toc] | [prev] | [next] | [standalone]


#88690 — Re: A string pointer to a static or dynamic string : how to free the

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-01-21 20:15 +0200
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqha4b$2mq2c$1@dont-email.me>
In reply to#88684
21.01.2023 18:20 Muttley@dastardlyhq.com kirjutas:
> On Sat, 21 Jan 2023 16:40:20 +0200
> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>> 21.01.2023 16:28 R.Wieser kirjutas:
>>> Hello all,
>>>
>>> I'm a rather newbie to C++ programming who is trying figure out how to deal
>>> with string pointers - or rather, with what they point at.
>>>
>>> Case in point :
>>>
>>>     char* message = "hello world";
>>>     char* message = strdup("hello world");
>>>
>>> I can user either of those in a function and return the pointer, and the
>>> caller will be none-the-wiser which form (the static or dymnamic string) it
>>> gets.
>>
>> This is C++, so just return a std::string from your function, and forget
>> everything about C-style strings, malloc, free and strdup. Good riddance!
> 
> Unless you want to parse the string which may involve chopping it about. Then
> its simpler to use a C string and pointers rather than mess about with
> inefficient substr() etc.

If you want to speed up substring processing, then there is 
std::string_view for you. As fast as plain pointers and much cleaner.


[toc] | [prev] | [next] | [standalone]


#88737 — Re: A string pointer to a static or dynamic string : how to free the

FromMuttley@dastardlyhq.com
Date2023-01-23 09:25 +0000
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqljqs$s93$1@gioia.aioe.org>
In reply to#88690
On Sat, 21 Jan 2023 20:15:39 +0200
Paavo Helde <eesnimi@osa.pri.ee> wrote:
>21.01.2023 18:20 Muttley@dastardlyhq.com kirjutas:
>> On Sat, 21 Jan 2023 16:40:20 +0200
>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>> 21.01.2023 16:28 R.Wieser kirjutas:
>>>> Hello all,
>>>>
>>>> I'm a rather newbie to C++ programming who is trying figure out how to deal
>
>>>> with string pointers - or rather, with what they point at.
>>>>
>>>> Case in point :
>>>>
>>>>     char* message = "hello world";
>>>>     char* message = strdup("hello world");
>>>>
>>>> I can user either of those in a function and return the pointer, and the
>>>> caller will be none-the-wiser which form (the static or dymnamic string) it
>
>>>> gets.
>>>
>>> This is C++, so just return a std::string from your function, and forget
>>> everything about C-style strings, malloc, free and strdup. Good riddance!
>> 
>> Unless you want to parse the string which may involve chopping it about. Then
>
>> its simpler to use a C string and pointers rather than mess about with
>> inefficient substr() etc.
>
>If you want to speed up substring processing, then there is 
>std::string_view for you. As fast as plain pointers and much cleaner.

Not really. This is fast, simple and clear:

for(p=str;*p;)
{
	switch(*p)
	{
	case '[':
		p = parseList(p+1);
		break;
	case '{':
		p = parseBlock(p+1);
		break;
	case '(':
		p = parseArray(p+1);
		break;
	default:
		<do something else>
		++p;
	}
}

Not sure how using indexes and substrings would improve it.

[toc] | [prev] | [next] | [standalone]


#88741 — Re: A string pointer to a static or dynamic string : how to free the

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-01-23 12:11 +0200
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqlmgs$3io52$1@dont-email.me>
In reply to#88737
23.01.2023 11:25 Muttley@dastardlyhq.com kirjutas:
> On Sat, 21 Jan 2023 20:15:39 +0200
> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>> 21.01.2023 18:20 Muttley@dastardlyhq.com kirjutas:
>>> On Sat, 21 Jan 2023 16:40:20 +0200
>>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>>> 21.01.2023 16:28 R.Wieser kirjutas:
>>>>> Hello all,
>>>>>
>>>>> I'm a rather newbie to C++ programming who is trying figure out how to deal
>>
>>>>> with string pointers - or rather, with what they point at.
>>>>>
>>>>> Case in point :
>>>>>
>>>>>      char* message = "hello world";
>>>>>      char* message = strdup("hello world");
>>>>>
>>>>> I can user either of those in a function and return the pointer, and the
>>>>> caller will be none-the-wiser which form (the static or dymnamic string) it
>>
>>>>> gets.
>>>>
>>>> This is C++, so just return a std::string from your function, and forget
>>>> everything about C-style strings, malloc, free and strdup. Good riddance!
>>>
>>> Unless you want to parse the string which may involve chopping it about. Then
>>
>>> its simpler to use a C string and pointers rather than mess about with
>>> inefficient substr() etc.
>>
>> If you want to speed up substring processing, then there is
>> std::string_view for you. As fast as plain pointers and much cleaner.
> 
> Not really. This is fast, simple and clear:
> 
> for(p=str;*p;)
> {
> 	switch(*p)
> 	{
> 	case '[':
> 		p = parseList(p+1);
> 		break;
> 	case '{':
> 		p = parseBlock(p+1);
> 		break;
> 	case '(':
> 		p = parseArray(p+1);
> 		break;
> 	default:
> 		<do something else>
> 		++p;
> 	}
> }
> 
> Not sure how using indexes and substrings would improve it.

This looks like it either assumes the string is syntactically correct 
(all parens are balanced correctly) or the parsed string is terminated 
by a null byte. So when parsing a substring of some larger text or 
binary file I would need to copy it out and append a null byte, just to 
be sure my error detection works properly.

When using string_view, you always know how long your to-be-processed 
piece is, so you don't need to make pointless copies just to know where 
to stop processing. With raw pointers, you would need to pass a separate 
end pointer, which would make the interfaces more complex.

[toc] | [prev] | [next] | [standalone]


#88742 — Re: A string pointer to a static or dynamic string : how to free the

FromMuttley@dastardlyhq.com
Date2023-01-23 10:14 +0000
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqlmn3$7so$1@gioia.aioe.org>
In reply to#88741
On Mon, 23 Jan 2023 12:11:40 +0200
Paavo Helde <eesnimi@osa.pri.ee> wrote:
>23.01.2023 11:25 Muttley@dastardlyhq.com kirjutas:
>> On Sat, 21 Jan 2023 20:15:39 +0200
>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>> 21.01.2023 18:20 Muttley@dastardlyhq.com kirjutas:
>>>> On Sat, 21 Jan 2023 16:40:20 +0200
>>>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>>>> 21.01.2023 16:28 R.Wieser kirjutas:
>>>>>> Hello all,
>>>>>>
>>>>>> I'm a rather newbie to C++ programming who is trying figure out how to
>deal
>>>
>>>>>> with string pointers - or rather, with what they point at.
>>>>>>
>>>>>> Case in point :
>>>>>>
>>>>>>      char* message = "hello world";
>>>>>>      char* message = strdup("hello world");
>>>>>>
>>>>>> I can user either of those in a function and return the pointer, and the
>>>>>> caller will be none-the-wiser which form (the static or dymnamic string)
>it
>>>
>>>>>> gets.
>>>>>
>>>>> This is C++, so just return a std::string from your function, and forget
>>>>> everything about C-style strings, malloc, free and strdup. Good riddance!
>>>>
>>>> Unless you want to parse the string which may involve chopping it about.
>Then
>>>
>>>> its simpler to use a C string and pointers rather than mess about with
>>>> inefficient substr() etc.
>>>
>>> If you want to speed up substring processing, then there is
>>> std::string_view for you. As fast as plain pointers and much cleaner.
>> 
>> Not really. This is fast, simple and clear:
>> 
>> for(p=str;*p;)
>> {
>> 	switch(*p)
>> 	{
>> 	case '[':
>> 		p = parseList(p+1);
>> 		break;
>> 	case '{':
>> 		p = parseBlock(p+1);
>> 		break;
>> 	case '(':
>> 		p = parseArray(p+1);
>> 		break;
>> 	default:
>> 		<do something else>
>> 		++p;
>> 	}
>> }
>> 
>> Not sure how using indexes and substrings would improve it.
>
>This looks like it either assumes the string is syntactically correct 
>(all parens are balanced correctly) or the parsed string is terminated 
>by a null byte. So when parsing a substring of some larger text or 

Or the given function works up to the matching close bracket and returns
the pointer to that + 1. In a real parser this would be in a recursive function
because of nested blocks/lists etc.

>binary file I would need to copy it out and append a null byte, just to 
>be sure my error detection works properly.
>
>When using string_view, you always know how long your to-be-processed 
>piece is, so you don't need to make pointless copies just to know where 

Your to-be-processed piece will be the entire program/text to be parsed at
the start.

>to stop processing. With raw pointers, you would need to pass a separate 
>end pointer, which would make the interfaces more complex.

I think we can assume you've never written a parser and leave it at that.

[toc] | [prev] | [next] | [standalone]


#88746 — Re: A string pointer to a static or dynamic string : how to free the

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-01-23 14:14 +0200
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqltn2$3k0vv$1@dont-email.me>
In reply to#88742
23.01.2023 12:14 Muttley@dastardlyhq.com kirjutas:
> On Mon, 23 Jan 2023 12:11:40 +0200
> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>> 23.01.2023 11:25 Muttley@dastardlyhq.com kirjutas:
>>> On Sat, 21 Jan 2023 20:15:39 +0200
>>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>>> 21.01.2023 18:20 Muttley@dastardlyhq.com kirjutas:
>>>>> On Sat, 21 Jan 2023 16:40:20 +0200
>>>>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>>>>> 21.01.2023 16:28 R.Wieser kirjutas:
>>>>>>> Hello all,
>>>>>>>
>>>>>>> I'm a rather newbie to C++ programming who is trying figure out how to
>> deal
>>>>
>>>>>>> with string pointers - or rather, with what they point at.
>>>>>>>
>>>>>>> Case in point :
>>>>>>>
>>>>>>>       char* message = "hello world";
>>>>>>>       char* message = strdup("hello world");
>>>>>>>
>>>>>>> I can user either of those in a function and return the pointer, and the
>>>>>>> caller will be none-the-wiser which form (the static or dymnamic string)
>> it
>>>>
>>>>>>> gets.
>>>>>>
>>>>>> This is C++, so just return a std::string from your function, and forget
>>>>>> everything about C-style strings, malloc, free and strdup. Good riddance!
>>>>>
>>>>> Unless you want to parse the string which may involve chopping it about.
>> Then
>>>>
>>>>> its simpler to use a C string and pointers rather than mess about with
>>>>> inefficient substr() etc.
>>>>
>>>> If you want to speed up substring processing, then there is
>>>> std::string_view for you. As fast as plain pointers and much cleaner.
>>>
>>> Not really. This is fast, simple and clear:
>>>
>>> for(p=str;*p;)
>>> {
>>> 	switch(*p)
>>> 	{
>>> 	case '[':
>>> 		p = parseList(p+1);
>>> 		break;
>>> 	case '{':
>>> 		p = parseBlock(p+1);
>>> 		break;
>>> 	case '(':
>>> 		p = parseArray(p+1);
>>> 		break;
>>> 	default:
>>> 		<do something else>
>>> 		++p;
>>> 	}
>>> }
>>>
>>> Not sure how using indexes and substrings would improve it.
>>
>> This looks like it either assumes the string is syntactically correct
>> (all parens are balanced correctly) or the parsed string is terminated
>> by a null byte. So when parsing a substring of some larger text or
> 
> Or the given function works up to the matching close bracket and returns
> the pointer to that + 1. 

This is relying on the often-not-so-guaranteed assumption the string is 
syntactically correct and actually contains this matching close bracket 
- that's what I wrote.

[...]

> I think we can assume you've never written a parser and leave it at that.

You can assume what you like. Meanwhile, I have written my part of 
pointer-based parsers and now switching over to string_view.

[toc] | [prev] | [next] | [standalone]


#88748 — Re: A string pointer to a static or dynamic string : how to free the

FromMuttley@dastardlyhq.com
Date2023-01-23 12:27 +0000
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqlufq$1v3m$1@gioia.aioe.org>
In reply to#88746
On Mon, 23 Jan 2023 14:14:26 +0200
Paavo Helde <eesnimi@osa.pri.ee> wrote:
>23.01.2023 12:14 Muttley@dastardlyhq.com kirjutas:
>> On Mon, 23 Jan 2023 12:11:40 +0200
>> Or the given function works up to the matching close bracket and returns
>> the pointer to that + 1. 
>
>This is relying on the often-not-so-guaranteed assumption the string is 
>syntactically correct and actually contains this matching close bracket 
>- that's what I wrote.

If its not correct the parser throws an error. Thats what parsers do.

>> I think we can assume you've never written a parser and leave it at that.
>
>You can assume what you like. Meanwhile, I have written my part of 
>pointer-based parsers and now switching over to string_view.

If you say so.

Another reason to use char* is that a lot of parsers will memory map a file
R/W and MAP_PRIVATE which gives you a char* pointing to the beginning of the 
file and which you can manipulate as you see fit.

[toc] | [prev] | [next] | [standalone]


#88759 — Re: A string pointer to a static or dynamic string : how to free the

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-01-23 17:56 +0200
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqmamv$3m5bt$1@dont-email.me>
In reply to#88748
23.01.2023 14:27 Muttley@dastardlyhq.com kirjutas:

> Another reason to use char* is that a lot of parsers will memory map a file
> R/W and MAP_PRIVATE which gives you a char* pointing to the beginning of the
> file and which you can manipulate as you see fit.

Seems like a non-portable hack.

[toc] | [prev] | [next] | [standalone]


#88761 — Re: A string pointer to a static or dynamic string : how to free the

FromMuttley@dastardlyhq.com
Date2023-01-23 16:11 +0000
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqmbjl$iv3$1@gioia.aioe.org>
In reply to#88759
On Mon, 23 Jan 2023 17:56:15 +0200
Paavo Helde <eesnimi@osa.pri.ee> wrote:
>23.01.2023 14:27 Muttley@dastardlyhq.com kirjutas:
>
>> Another reason to use char* is that a lot of parsers will memory map a file
>> R/W and MAP_PRIVATE which gives you a char* pointing to the beginning of the
>> file and which you can manipulate as you see fit.
>
>Seems like a non-portable hack.

Portable where? Its been standard posix functionality for decades and is
anything but a hack. The whole point of a private map is so you can manipulate
the file contents in memory without having to labouriously read it all in
first and without changing the file itself. Its extremely useful.

[toc] | [prev] | [next] | [standalone]


#88769 — Re: A string pointer to a static or dynamic string : how to free the

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-01-23 18:58 +0200
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqmeci$3mn0g$1@dont-email.me>
In reply to#88761
23.01.2023 18:11 Muttley@dastardlyhq.com kirjutas:
> On Mon, 23 Jan 2023 17:56:15 +0200
> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>> 23.01.2023 14:27 Muttley@dastardlyhq.com kirjutas:
>>
>>> Another reason to use char* is that a lot of parsers will memory map a file
>>> R/W and MAP_PRIVATE which gives you a char* pointing to the beginning of the
>>> file and which you can manipulate as you see fit.
>>
>> Seems like a non-portable hack.
> 
> Portable where? 

This is a C++ group.

> Its been standard posix functionality for decades and is
> anything but a hack. 


> The whole point of a private map is so you can manipulate
> the file contents in memory without having to labouriously read it all in
> first 

This is achieved by a read-only memory map. On which a string_view would 
work fine, coincidentally.

> and without changing the file itself. Its extremely useful.

Why should I want to manipulate the file contents when parsing it? Ah, I 
know the answer, it comes from the camp who thinks copying a virtual 
main memory page will be faster than passing some extra register 
variables for keeping better track about the parsing process. Maybe 40 
years ago on some hardware it had a point.

[toc] | [prev] | [next] | [standalone]


#88774 — Re: A string pointer to a static or dynamic string : how to free the

FromMuttley@dastardlyhq.com
Date2023-01-23 17:18 +0000
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqmfi3$nfe$1@gioia.aioe.org>
In reply to#88769
On Mon, 23 Jan 2023 18:58:58 +0200
Paavo Helde <eesnimi@osa.pri.ee> wrote:
>23.01.2023 18:11 Muttley@dastardlyhq.com kirjutas:
>> On Mon, 23 Jan 2023 17:56:15 +0200
>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>> 23.01.2023 14:27 Muttley@dastardlyhq.com kirjutas:
>>>
>>>> Another reason to use char* is that a lot of parsers will memory map a file
>
>>>> R/W and MAP_PRIVATE which gives you a char* pointing to the beginning of
>the
>>>> file and which you can manipulate as you see fit.
>>>
>>> Seems like a non-portable hack.
>> 
>> Portable where? 
>
>This is a C++ group.

And? You are allowed to use standard OS C APIs in C++ in case you were
unaware.

>> The whole point of a private map is so you can manipulate
>> the file contents in memory without having to labouriously read it all in
>> first 
>
>This is achieved by a read-only memory map. On which a string_view would 
>work fine, coincidentally.

Maybe it would. But since you get char* out the box why bother?

>> and without changing the file itself. Its extremely useful.
>
>Why should I want to manipulate the file contents when parsing it? Ah, I 
>know the answer, it comes from the camp who thinks copying a virtual 
>main memory page will be faster than passing some extra register 
>variables for keeping better track about the parsing process. Maybe 40 
>years ago on some hardware it had a point.

Putting a \0 at the end of some text you want to pass to a sub function is
standard practice. But the main use is having the entire file (from the programs
perspective) available as text in memory without having to some or all of it 
yourself manually or do file seeking with fstream or some kind of file pointer 
which is far less efficient and still involves paging into memory anyway.

Shall we assume you've never heard of mmap() because you know nothing about
unix systems programming and got confused?

[toc] | [prev] | [next] | [standalone]


#88779 — Re: A string pointer to a static or dynamic string : how to free the

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-01-23 19:49 +0200
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqmhc5$3mn0g$2@dont-email.me>
In reply to#88774
23.01.2023 19:18 Muttley@dastardlyhq.com kirjutas:
> On Mon, 23 Jan 2023 18:58:58 +0200
> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>> 23.01.2023 18:11 Muttley@dastardlyhq.com kirjutas:
>>> On Mon, 23 Jan 2023 17:56:15 +0200
>>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>>> 23.01.2023 14:27 Muttley@dastardlyhq.com kirjutas:
>>>>
>>>>> Another reason to use char* is that a lot of parsers will memory map a file
>>
>>>>> R/W and MAP_PRIVATE which gives you a char* pointing to the beginning of
>> the
>>>>> file and which you can manipulate as you see fit.
>>>>
>>>> Seems like a non-portable hack.
>>>
>>> Portable where?
>>
>> This is a C++ group.
> 
> And? You are allowed to use standard OS C APIs in C++ in case you were
> unaware.
> 
>>> The whole point of a private map is so you can manipulate
>>> the file contents in memory without having to labouriously read it all in
>>> first
>>
>> This is achieved by a read-only memory map. On which a string_view would
>> work fine, coincidentally.
> 
> Maybe it would. But since you get char* out the box why bother?
> 
>>> and without changing the file itself. Its extremely useful.
>>
>> Why should I want to manipulate the file contents when parsing it? Ah, I
>> know the answer, it comes from the camp who thinks copying a virtual
>> main memory page will be faster than passing some extra register
>> variables for keeping better track about the parsing process. Maybe 40
>> years ago on some hardware it had a point.
> 
> Putting a \0 at the end of some text you want to pass to a sub function is
> standard practice. 

And ... we are arriving back to where we were before. I clarify now for 
better addressing your points:

When using string_view, you always know how long your to-be-processed 
piece is, so you don't need to make pointless copies or rely on 
non-portable hacks (which incidentally also make pointless copies (of VM 
pages)) just to know where to stop processing.

[toc] | [prev] | [next] | [standalone]


#88784 — Re: A string pointer to a static or dynamic string : how to free the

FromCholo Lennon <chololennon@hotmail.com>
Date2023-01-23 16:44 -0300
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqmo1a$vn8$1@gioia.aioe.org>
In reply to#88737
On 1/23/23 06:25, Muttley@dastardlyhq.com wrote:
> On Sat, 21 Jan 2023 20:15:39 +0200
> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>> 21.01.2023 18:20 Muttley@dastardlyhq.com kirjutas:
>>> On Sat, 21 Jan 2023 16:40:20 +0200
>>> Paavo Helde <eesnimi@osa.pri.ee> wrote:
>>>> 21.01.2023 16:28 R.Wieser kirjutas:
>>>>> Hello all,
>>>>>
>>>>> I'm a rather newbie to C++ programming who is trying figure out how to deal
>>
>>>>> with string pointers - or rather, with what they point at.
>>>>>
>>>>> Case in point :
>>>>>
>>>>>      char* message = "hello world";
>>>>>      char* message = strdup("hello world");
>>>>>
>>>>> I can user either of those in a function and return the pointer, and the
>>>>> caller will be none-the-wiser which form (the static or dymnamic string) it
>>
>>>>> gets.
>>>>
>>>> This is C++, so just return a std::string from your function, and forget
>>>> everything about C-style strings, malloc, free and strdup. Good riddance!
>>>
>>> Unless you want to parse the string which may involve chopping it about. Then
>>
>>> its simpler to use a C string and pointers rather than mess about with
>>> inefficient substr() etc.
>>
>> If you want to speed up substring processing, then there is
>> std::string_view for you. As fast as plain pointers and much cleaner.
> 
> Not really. This is fast, simple and clear:
> 
> for(p=str;*p;)
> {
> 	switch(*p)
> 	{
> 	case '[':
> 		p = parseList(p+1);
> 		break;
> 	case '{':
> 		p = parseBlock(p+1);
> 		break;
> 	case '(':
> 		p = parseArray(p+1);
> 		break;
> 	default:
> 		<do something else>
> 		++p;
> 	}
> }
> 

OMG! that's why we still have plenty of CVEs in C/C++ applications and 
also new programming languages like Rust... :-O

> Not sure how using indexes and substrings would improve it.
> 

--
Cholo Lennon
Bs.As.
ARG

[toc] | [prev] | [next] | [standalone]


#88807 — Re: A string pointer to a static or dynamic string : how to free the

FromMuttley@dastardlyhq.com
Date2023-01-24 17:05 +0000
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqp35f$7kst$1@dont-email.me>
In reply to#88784
On Mon, 23 Jan 2023 16:44:01 -0300
Cholo Lennon <chololennon@hotmail.com> wrote:
>On 1/23/23 06:25, Muttley@dastardlyhq.com wrote:
>> Not really. This is fast, simple and clear:
>> 
>> for(p=str;*p;)
>> {
>> 	switch(*p)
>> 	{
>> 	case '[':
>> 		p = parseList(p+1);
>> 		break;
>> 	case '{':
>> 		p = parseBlock(p+1);
>> 		break;
>> 	case '(':
>> 		p = parseArray(p+1);
>> 		break;
>> 	default:
>> 		<do something else>
>> 		++p;
>> 	}
>> }
>> 
>
>OMG! that's why we still have plenty of CVEs in C/C++ applications and 
>also new programming languages like Rust... :-O

It was example code. It would have far more checks in a real program.

[toc] | [prev] | [next] | [standalone]


#88692 — Re: A string pointer to a static or dynamic string : how to free the

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-21 19:30 +0100
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqhav4$2mv2i$1@dont-email.me>
In reply to#88684
Am 21.01.2023 um 17:20 schrieb Muttley@dastardlyhq.com:

> Unless you want to parse the string which may involve chopping it about. Then
> its simpler to use a C string and pointers rather than mess about with
> inefficient substr() etc.

What's wrong with sth. like this ?

string substr()
{
	string hw( "hello world!" );
	hw.erase( hw.begin(), hw.begin() + 6 );
	hw.resize( 5 );
	return hw;
}

[toc] | [prev] | [next] | [standalone]


#88738 — Re: A string pointer to a static or dynamic string : how to free the

FromMuttley@dastardlyhq.com
Date2023-01-23 09:30 +0000
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqlk2t$108s$1@gioia.aioe.org>
In reply to#88692
On Sat, 21 Jan 2023 19:30:48 +0100
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>Am 21.01.2023 um 17:20 schrieb Muttley@dastardlyhq.com:
>
>> Unless you want to parse the string which may involve chopping it about. Then
>
>> its simpler to use a C string and pointers rather than mess about with
>> inefficient substr() etc.
>
>What's wrong with sth. like this ?
>
>string substr()
>{
>	string hw( "hello world!" );
>	hw.erase( hw.begin(), hw.begin() + 6 );
>	hw.resize( 5 );
>	return hw;
>}

char *hw = "hello world!";
:
return hw + 5;

[toc] | [prev] | [next] | [standalone]


#88750 — Re: A string pointer to a static or dynamic string : how to free the

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-23 13:53 +0100
SubjectRe: A string pointer to a static or dynamic string : how to free the
Message-ID<tqlvvd$3kevs$1@dont-email.me>
In reply to#88738
Am 23.01.2023 um 10:30 schrieb Muttley@dastardlyhq.com:
> On Sat, 21 Jan 2023 19:30:48 +0100
> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> Am 21.01.2023 um 17:20 schrieb Muttley@dastardlyhq.com:
>>
>>> Unless you want to parse the string which may involve chopping it about. Then
>>
>>> its simpler to use a C string and pointers rather than mess about with
>>> inefficient substr() etc.
>>
>> What's wrong with sth. like this ?
>>
>> string substr()
>> {
>> 	string hw( "hello world!" );
>> 	hw.erase( hw.begin(), hw.begin() + 6 );
>> 	hw.resize( 5 );
>> 	return hw;
>> }
> 
> char *hw = "hello world!";
> :
> return hw + 5;

You're returning a newly created string object, thereby inducing
the issue a second allocation Muttley@dastardlyhq.com mentioned.
In this case this might not make a difference due to the short
string optimizations, in others this might be a peformance pro-
blem. And you're returning the exclamation mark I also stripped.

[toc] | [prev] | [next] | [standalone]


Page 1 of 5  [1] 2 3 4 5  Next page →

Back to top | Article view | comp.lang.c++


csiph-web