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


Groups > comp.lang.c > #162510

Re: Freeing a dynamically allocated array of structs within a struct in C

From Kaz Kylheku <563-365-8930@kylheku.com>
Newsgroups comp.lang.c
Subject Re: Freeing a dynamically allocated array of structs within a struct in C
Date 2021-08-31 07:50 +0000
Organization A noiseless patient Spider
Message-ID <20210831003134.197@kylheku.com> (permalink)
References <1348a974-54cd-47cf-bf40-7034feaa1f78n@googlegroups.com> <sggi2c$jkm$1@dont-email.me> <sghvo3$jra$1@dont-email.me>

Show all headers | View raw


On 2021-08-30, Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Am 29.08.2021 um 20:01 schrieb Bonita Montero:
>> Use a proper language:
>> 
>> #include <memory>
>> #include <string>
>> 
>> using namespace std;
>> 
>> struct deck_entry
>> {
>>      string key,
>>             value;
>> };
>> 
>> using up_de_t = unique_ptr<deck_entry>;
>> 
>> up_de_t parse_deck_entry( string const &parse )
>> {
>>      size_t delimiterBegin = parse.find( ": " );
>>      if( delimiterBegin == string::npos )
>>          return up_de_t();
>>      up_de_t de = make_unique<deck_entry>();
>>      de->key    = string( parse.begin(), parse.begin() + delimiterBegin );
>>      de->value  = string( parse.begin() + (delimiterBegin + 2), 
>> parse.end() );
>>      return de;
>> }
>
> And now this ugly C-pendant:
>
> #include <stddef.h>
> #include <string.h>
> #include <stdlib.h>
>
> typedef struct deck_entry
> {
> 	char *key,
> 	     *value;
> } deck_entry;
>
> deck_entry *parseDeckEntry( char const *parse )
> {
> 	size_t      len       = strlen( parse );
> 	char const *delimiter = strstr( parse, ": " );
> 	if( !delimiter || delimiter == parse || delimiter == parse + len - 2 )
> 		return NULL;
> 	deck_entry *de = NULL;
> 	if( !(de = malloc( sizeof(deck_entry) )) )
> 		return NULL;
> 	char *key, *value;
> 	if( !(de->key = malloc( delimiter - parse + 1 )) )
> 		goto undoDE;
> 	if( !(de->value = malloc( len - (delimiter - parse + 2) + 1 )) )
> 		goto undoKey;
> 	strncpy( de->key, parse, delimiter - parse );
> 	strcpy( de->value, delimiter + 2 );
> 	return de;
> undoKey:
> 	free( de->key );
> undoDE:
> 	free( de );
> 	return NULL;
> }

That's fine, but when you're dealing with manual resource management,
a structure like the following structure is nice:

deck_entry *parseDeckEntry(char const *parse)
{
  size_t      len       = strlen(parse);
  char const *delimiter = strstr(parse, ": ");

  if (!delimiter || delimiter == parse || delimiter == parse + len - 2)
    return NULL;

  deck_entry *de        = malloc(sizeof *de);
  char       *key       = malloc(delimiter - parse + 1);
  char       *value     = malloc(len - (delimiter - parse + 2) + 1);

  if (de && key && value) {
    strncpy(key, parse, delimiter - parse);
    strcpy(value, delimiter + 2);
    de->key = key;
    de->value = value;
    return de;
  }

  free(value);
  free(key);
  free(de);

  return NULL;
}
}
-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

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


Thread

Freeing a dynamically allocated array of structs within a struct in C Bithov Vinu Student <vinub@calday.co.uk> - 2021-08-29 08:34 -0700
  Re: Freeing a dynamically allocated array of structs within a struct in C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-29 15:43 +0000
    Re: Freeing a dynamically allocated array of structs within a struct in C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-29 16:12 +0000
      Re: Freeing a dynamically allocated array of structs within a struct in C Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-09-06 05:46 -0700
  Re: Freeing a dynamically allocated array of structs within a struct in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-08-29 17:17 +0100
  Re: Freeing a dynamically allocated array of structs within a struct in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-08-29 20:01 +0200
    Re: Freeing a dynamically allocated array of structs within a struct in C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-29 18:21 +0000
      Re: Freeing a dynamically allocated array of structs within a struct in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-08-29 20:34 +0200
    Re: Freeing a dynamically allocated array of structs within a struct in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-08-30 09:01 +0200
      Re: Freeing a dynamically allocated array of structs within a struct in C Robert Latest <boblatest@yahoo.com> - 2021-08-31 05:44 +0000
      Re: Freeing a dynamically allocated array of structs within a struct in C Kaz Kylheku <563-365-8930@kylheku.com> - 2021-08-31 07:50 +0000
        Re: Freeing a dynamically allocated array of structs within a struct in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-08-31 10:59 +0200
    Re: Freeing a dynamically allocated array of structs within a struct in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-08-30 09:45 +0200
      Re: Freeing a dynamically allocated array of structs within a struct in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-08-30 10:15 +0200
  Re: Freeing a dynamically allocated array of structs within a struct in C Kaz Kylheku <563-365-8930@kylheku.com> - 2021-08-29 18:14 +0000
  Re: Freeing a dynamically allocated array of structs within a struct in C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-29 18:18 +0000
  Re: Freeing a dynamically allocated array of structs within a struct in C Barry Schwarz <schwarzb@delq.com> - 2021-08-29 11:44 -0700
    Re: Freeing a dynamically allocated array of structs within a struct in C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-29 19:04 +0000
      Re: Freeing a dynamically allocated array of structs within a struct in C Barry Schwarz <schwarzb@delq.com> - 2021-08-29 14:15 -0700
        Re: Freeing a dynamically allocated array of structs within a struct in C scott@slp53.sl.home (Scott Lurndal) - 2021-08-29 21:19 +0000
          Re: Freeing a dynamically allocated array of structs within a struct in C Barry Schwarz <schwarzb@delq.com> - 2021-08-29 18:14 -0700
        Re: Freeing a dynamically allocated array of structs within a struct in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-08-30 18:27 +0200
      Re: Freeing a dynamically allocated array of structs within a struct in C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-30 16:17 +0000
        Re: Freeing a dynamically allocated array of structs within a struct in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-08-30 14:14 -0400
          Re: Freeing a dynamically allocated array of structs within a struct in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-30 11:39 -0700
            Re: Freeing a dynamically allocated array of structs within a struct in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-08-31 10:08 -0400
            Re: Freeing a dynamically allocated array of structs within a struct in C Kaz Kylheku <563-365-8930@kylheku.com> - 2021-09-01 04:40 +0000
              Re: Freeing a dynamically allocated array of structs within a struct in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-01 00:57 -0700
          Re: Freeing a dynamically allocated array of structs within a struct in C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-30 19:16 +0000
            Re: Freeing a dynamically allocated array of structs within a struct in C Manfred <noname@add.invalid> - 2021-08-30 21:49 +0200
          Re: Freeing a dynamically allocated array of structs within a struct in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-08-30 12:49 -0700
          Re: Freeing a dynamically allocated array of structs within a struct in C scott@slp53.sl.home (Scott Lurndal) - 2021-08-30 22:31 +0000
            Re: Freeing a dynamically allocated array of structs within a struct in C Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-09-30 07:04 -0700
    Re: Freeing a dynamically allocated array of structs within a struct in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-29 15:38 -0700
      Re: Freeing a dynamically allocated array of structs within a struct in C Manfred <noname@add.invalid> - 2021-08-30 14:21 +0200
      Re: Freeing a dynamically allocated array of structs within a struct in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-08-30 18:29 +0200
        Re: Freeing a dynamically allocated array of structs within a struct in C Bart <bc@freeuk.com> - 2021-08-30 18:30 +0100
          Re: Freeing a dynamically allocated array of structs within a struct in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-08-31 04:22 +0200

csiph-web