V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
icemanpro
V2EX  ›  C++

在结构体中如何使用动态数组?

  •  
  •   icemanpro · 282 天前 · 1650 次点击
    这是一个创建于 282 天前的主题,其中的信息可能已经有所发展或是发生改变。
    struct a1{
    int a;
    int b;
    };
    
    struct bb{
    a1 list[];
    }
    

    像这样在结构体 bb 中定义了 a1 类型的动态数组。

    应该如何使用 bb 这个结构体?有没有代码

    16 条回复    2023-07-20 01:07:58 +08:00
    liyang5945
        1
    liyang5945  
       282 天前
    需要知道 a1 的 length ,不然没法搞
    codehz
        2
    codehz  
       282 天前
    c 里有 flexible array member ( https://en.cppreference.com/w/c/language/struct ),c++里可没有,不建议搞
    ArcanusNEO
        3
    ArcanusNEO  
       282 天前
    如果不能用 STL 容器的话,可以试试零长度数组这个 gcc 扩展,缺点是 msvc 用不了、会引入很多 C 风格代码、一个结构体里只能存在一个零长度数组
    ArcanusNEO
        4
    ArcanusNEO  
       282 天前
    @ArcanusNEO stackoverflow 上 flexible array member [相关的讨论]( https://stackoverflow.com/a/67894135)
    ysc3839
        5
    ysc3839  
       282 天前 via Android
    @ArcanusNEO MSVC 好像能用?印象中 Windows SDK 里面就用到了。去搜索了一下,只是会产生 warning 。
    thorneLiu
        6
    thorneLiu  
       282 天前 via Android
    土问这个 a1 list[]合法吗
    为啥不用 STL?
    ArcanusNEO
        7
    ArcanusNEO  
       282 天前
    @ysc3839 搜了一下似乎确实可以,不过印象里曾经因为相关的写法编译失败。可能比 gcc 多一些限制,也可能只是我记错了。。。
    tyzandhr
        8
    tyzandhr  
       282 天前
    为什么不用指针?
    ArcanusNEO
        9
    ArcanusNEO  
       282 天前
    @thorneLiu
    在 C 里面合法,C++里不合法;
    正常情况下 C++用 STL 应该更合适一点
    ysc3839
        10
    ysc3839  
       282 天前 via Android
    @thorneLiu 有的时候想要扁平可变长的,比如开头一个 size ,后面重复多个元素,用 STL 的话就不能一个指针传递全部数据了。
    yolee599
        11
    yolee599  
       282 天前
    放一个指针,然后 malloc [doge]
    Perfect1zsh1t
        12
    Perfect1zsh1t  
       282 天前
    ```cpp
    /*************************************************************************
    > File Name: test.c
    > Author: JerryChen
    > Created Time: 三 7/19 17:27:42 2023
    > Description:
    ************************************************************************/
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>

    #define N 10 // the length of softarray

    typedef struct A1 {
    int a;
    int b;
    }a1;

    typedef struct bb {
    int len;
    a1 list[];
    }a1_array;

    void init_function(a1_array *t) {
    int i = 0;
    for(i = 0; i < N; ++i) {
    t->list[i].a = i;
    t->list[i].b = i << 1;
    }
    }

    void print_array(a1_array *t) {
    int i = 0;
    for(i = 0; i < N; ++i) {
    printf("%d %d\n", t->list[i].a, t->list[i].b);
    }
    }

    int main() {
    a1_array *t = (a1_array *)malloc(sizeof(a1_array) + sizeof(*(t->list)) * N);
    memset(t, 0, sizeof(*(t->list)));
    t->len = N;

    init_function(t); // 初始化 list

    print_array(t);

    return 0;
    }

    ```
    cnbatch
        13
    cnbatch  
       282 天前 via Android
    struct bb { a1 list[1]; };

    然后

    std::unique_ptr<int64_t[]> ptr = std::make_unique<int64_t[]>(100);

    bb *bb_ptr = (bb*)ptr.get();
    bb_ptr->list[50].a = 100;
    cnbatch
        14
    cnbatch  
       282 天前 via Android
    如果不介意引入第三方 header ,那么可以试试这个:
    https://github.com/cnbatch/dynarray
    我在两年前做的动态数组库
    bfjm
        15
    bfjm  
       282 天前 via iPhone
    柔性数组吧
    changz
        16
    changz  
       282 天前 via Android
    看看 php 内核的字符串怎么实现的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2802 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:52 · PVG 13:52 · LAX 22:52 · JFK 01:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.