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


Groups > comp.lang.c > #150674

Re: allocating two objects in one malloc

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: allocating two objects in one malloc
Date 2020-03-18 21:00 +0000
Organization A noiseless patient Spider
Message-ID <87pnd9gyzk.fsf@bsb.me.uk> (permalink)
References <20301314-7f32-4cab-924d-c55c7a5175d6@googlegroups.com> <r4tl42$ch6$1@news.albasani.net> <d640b96b-9998-4c5b-944d-bfd372fcde93@googlegroups.com> <e05akg-7kp1.ln1@mail.home.palmen-it.de> <31d5ac0f-889d-4918-9313-a6e1b97eebd2@googlegroups.com>

Show all headers | View raw


Thiago Adams <thiago.adams@gmail.com> writes:

> I am doing an exercise and thinking how a reziable 
> array type would be implemented under the hood in C.
<cut>
> I have two approaches:
>
> 1 - (similar of C++ vector) 
>     struct { int size; int capacity; T* data; };

Can you not use a flexible array member:

  struct { int size, capacity; T data[]; };

?
  
> 2 - Pointer, pointing to the object that has size and capacity 
>     allocated together.

<cut>
-- 
Ben.

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


Thread

allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 10:14 -0700
  Re: allocating two objects in one malloc Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 18:17 +0100
    Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 10:55 -0700
      Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 11:01 -0700
      Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 19:14 +0100
        Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 11:35 -0700
          Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 19:44 +0100
            Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 12:07 -0700
              Re: allocating two objects in one malloc Bart <bc@freeuk.com> - 2020-03-18 20:39 +0000
                Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 13:52 -0700
              Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 23:11 +0100
                Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 16:48 -0700
                Re: allocating two objects in one malloc Bart <bc@freeuk.com> - 2020-03-19 01:16 +0000
                Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-24 06:11 -0700
                Re: allocating two objects in one malloc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 14:49 +0000
                Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-24 08:46 -0700
                Re: allocating two objects in one malloc James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-24 07:50 -0700
                Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-24 08:48 -0700
                Re: allocating two objects in one malloc chad <cdalten@gmail.com> - 2020-03-19 05:15 -0700
                Re: allocating two objects in one malloc James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-24 08:11 -0700
                Re: allocating two objects in one malloc Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-03-24 11:35 -0400
                Re: allocating two objects in one malloc Richard Damon <Richard@Damon-Family.org> - 2020-03-24 13:50 -0400
                Re: allocating two objects in one malloc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-24 20:58 +0000
                Re: allocating two objects in one malloc Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-24 13:10 -0700
                Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-24 08:52 -0700
                Re: allocating two objects in one malloc Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-24 13:05 -0700
          Re: allocating two objects in one malloc Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 19:53 +0100
          Re: allocating two objects in one malloc Bart <bc@freeuk.com> - 2020-03-18 20:29 +0000
          Re: allocating two objects in one malloc jadill33@gmail.com - 2020-03-18 13:44 -0700
          Re: allocating two objects in one malloc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-18 21:00 +0000
  Re: allocating two objects in one malloc Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-18 10:18 -0700
  Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 18:43 +0100
    Re: allocating two objects in one malloc Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 18:55 +0100
      Re: allocating two objects in one malloc Thiago Adams <thiago.adams@gmail.com> - 2020-03-18 10:56 -0700
        Re: allocating two objects in one malloc Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 19:06 +0100
      Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 19:01 +0100
    Re: allocating two objects in one malloc James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-18 14:21 -0400
      Re: allocating two objects in one malloc felix@palmen-it.de (Felix Palmen) - 2020-03-18 19:30 +0100
        Re: allocating two objects in one malloc James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-18 15:08 -0400

csiph-web