变长结构体的使用

ncode控件使用总结 数据格式txt转s3t #HEADER #TITLES Absolute_Time,COG_Ax,COG_Ay,COG_Az,LF_SPINDLE_AZ #UNITS ,g,g,g,g, #DATATYPES Double,Float,Float,Float,Float #DATA 2. 导入s3t数据 3. 输入数据控件 直接拖入数据源或者添加控件数据 4. 阅读详情

在分析安卓源码过程中看到几处使用变长结构体的例子,比如下面的结构体:

struct command
{
        /* list of commands in an action */
    struct listnode clist;

    int (*func)(int nargs, char **args);
    int nargs;
    char *args[1];
};

下面介绍安卓是如何使用这个结构的,在解析init.rc文件的过程中,会使用这个结构体记录某些命令。

static void parse_line_action(struct parse_state* state, int nargs, char **args)
{
    struct command *cmd;
    struct action *act = state->context;
    int (*func)(int nargs, char **args);
    int kw, n;

    if (nargs == 0) {
        return;
    }

    kw = lookup_keyword(args[0]);
    if (!kw_is(kw, COMMAND)) {
        parse_error(state, "invalid command '%s'\n", args[0]);
        return;
    }

    n = kw_nargs(kw);
    if (nargs < n) {
        parse_error(state, "%s requires %d %s\n", args[0], n - 1,
            n > 2 ? "arguments" : "argument");
        return;
    }
    cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
    cmd->func = kw_func(kw);
    cmd->nargs = nargs;
    memcpy(cmd->args, args, sizeof(char*) * nargs);
    list_add_tail(&act->commands, &cmd->clist);
}

比如对于如下的section:

on early-init
    # Set init and its forked children's oom_adj.
    write /proc/1/oom_adj -16

    # Set the security context for the init process.
    # This should occur before anything else (e.g. ueventd) is started.
    setcon u:r:init:s0

    start ueventd

会调用parse_line_action函数来解析write /proc/1/oom_adn -16,传入的参数中nargs=3,而cmd结构体中的args就是用来保存write /proc/1/oom_adj -16的。我们来看下,当解析完毕上面section时的组织结构。




华为OD机考双机位C卷 - 风险投资计划(Java & Python& JS & C/C++ & GO ) 华为OD机考双机位C卷 - 风险投资计划(OD上机考试双机位C卷)。提供Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。 阅读详情

相关推荐

macOS Jdk1.8安装(目前主流版本的jdk)

添加环境环境变量,类似于windows的高级设置中path配置的环境变量。查看 JAVA_HOME 目录。查看 JDK 版本信息。一直下一步安装就好了。在终端的根目录下运行。

m0_50207524的博客 9352

变长结构体

文章目录什么是变长结构体定义变长结构体变长结构体实例参考资料 什么是变长结构体 变长结构体是由gcc扩展的一种技术,它是指其最后一个成员的长度不固定(flexible array member,也叫柔性数组)。 使用范围:数据长度不固定,例如协议对接中有固定的头结构体,数据结构体不固定。 定义变长结构体 struct MyData { int nLen; char data[0]...

mayue_web的博客 1796

企业级AI Agent平台建设方案

本报告将深入剖析构成一个顶级AI Agent平台的各个功能层面,为构建者提供一份全面的战略蓝图。

分享AI和大数据领域的知识和研究 1582

C语言结构体

- 今天复习一下struct,顺便挖掘一下以前没注意的小细节: 基本定义:结构体,通俗讲就像是打包封装,把一些有共同特征(比如同属于某一类事物的属性,往往是某种业务相关属性的聚合)的变量封装在内部,通过一定方法访问修改内部变量。 结构体定义: 第一种:只有结构体定义[cpp] view plain copy print?struct stuff{          char job[20];

Innocence_y的博客 4072

国产SR8201F以太网PHY芯片调试避坑指南:从机贴失败到杜邦线救场的实战记录

本文详细介绍了国产SR8201F以太网PHY芯片的调试避坑指南,涵盖硬件设计、PCB布局、SMT贴片及软件驱动等关键环节。通过实战案例解析,帮助工程师解决MII/RMII接口配置、电源设计、信号完整性等典型问题,提升以太网通信稳定性与EMC性能。

c6d7e8f9g的博客 1212

ESP32-P4实战:LVGL V9.4 PPA加速配置与性能对比

本文详细介绍了如何在ESP32-P4平台上配置LVGL V9.4的PPA硬件加速功能,通过启用PPA加速显著提升图形处理性能并降低CPU占用率。文章包含环境准备、配置步骤、性能对比及常见问题解决方案,帮助开发者高效利用ESP32-P4的PPA加速器优化嵌入式GUI应用。

rice5的博客 1189

解析变长结构体的用法和优点

变长结构体:在接触变长结构体之前,以为会是一个很难理解的东西,但是这其实算是C里面的一种技巧吧,优点是:分配了一段连续的内存,防止内存碎片化以及方便内存的管理。使用变长结构体的格式如下:struct Test { .... int a; .... char b[0]; }; 重点是结构体的最后一个成员char b[0],是个空数组。在我们不知道结构体内的某个成员大小是多少的时候,我们

Move_now的博客 6428

基于Mind+、MOTT实现人工智能

@基于Mind+、MOTT实现人工智能 人工智能实验 一、实验目的及要求 【一】实现人脸识别 【二】实现云数据库应用 【三】实现测温数据mqtt上传至物联网云平台 二、实验原理与内容 基于二哈实现多人人脸学习和识别 基于TinywebDB实现云数据库应用 基于EasyIoT实现mqtt数据上传。 三、实验软硬件环境 硬件:掌控板 软件:Mind+ 四、实验过程(实验步骤、记录、数据、分析) 1.基于二哈实现多人人脸学习和识别 ①在Mind+扩展上的传感器选项选择HUSKYLENS AI 摄像头 ②配置哈士

weixin_44606930的博客 2372

数据结构 ——循环双链表—变长结构体的使用

如果你直接使用指针而不使用数组,那么,你在分配内存缓冲区时,就必须分配结构体一次,然后再分配结构体内的指针一次,(而此时分配的内存已经与结构体的内存不连续了,所以要分别管理即申请和释放)。概念:变长结构体是由gcc扩展的一种技术,它是指其最后一个成员的长度不固定(flexible array member,也叫柔性数组)。应用: Socket通信数据包的传输、解析数据包、其他可以节省空间,连续存储的地方等。下面是基于循环双链表的基础上,使用变长结构体,以项目工程的形式记录。

m0_55967108的博客 344

从12摄像头到纯视觉:小鹏AI鹰眼方案如何用LOFIC技术省掉激光雷达?

本文深入解析小鹏汽车AI鹰眼方案如何通过LOFIC技术实现纯视觉自动驾驶,替代传统激光雷达。LOFIC摄像头在动态范围、噪声控制和HDR优化方面表现卓越,结合800万像素双目广角摄像头,达到激光雷达级测距精度。该方案不仅降低成本,还提升系统可靠性和用户体验,展示了纯视觉路线的技术潜力与边界。

t8u9v0w1x的博客 932

Halcon像素比例标定(误差大)-相机标定

一种简易标定: 像素比例标定,如果需要精确一些则需要在高度和宽度上都需要计算,然后求出比例系数。需要高精度标定板和相机,以及多次标定才能达到稳定的状态,这种标定简单,但是误差大。 read_image (Img0008, 'D:/00项目代码/000视觉软件/02Halocn算法20190501/04测量20190424/07_比例计算测量标定/IMG0008.jpg') rgb1_to_...

SSSS的博客 3984

【C/C++】结构体中使用变长数组问题分析

今天在结构体里面使用了变长数组来封装消息体,运行程序时遇到了一些错误,问题已经解决,现在通过一个实例来复现分析问题。

NoBack7--For the dream 2520

从零开始利用树莓派+扬声器,实现简单的蓝牙音箱,手机连接放歌

树莓派4B自带蓝牙和Wifi, 无需外接 USB dongle;蓝牙最常见的应用是近距离传输数据,比如蓝牙传文件,蓝牙音箱等。正好家里有个普通的usb供电的便携音箱;本文用树莓派蓝牙+普通音箱,实现简单的蓝牙音箱。怎样理解这三层:可以类比Linux网络层:ALSA 类似网络驱动框架A2DP 类似TCP/UDP层PulseAudio 类似HTTP层的服务器,类比Apache而蓝牙连接类似http连接和会话;

南七小僧的学海无涯 2221

可变长结构体使用小记

C接口,可变长结构体,内存管理

xiaosier_D的博客 854

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

C++变长结构体使用方法

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <string.h> #define HexPrint(_buf, _len) \ {\ int _m_i = 0;\ char *_m_buf = (char *)(_buf);\ int _m_le.

云淡风轻ing的博客 539

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

C-数据结构-双向环链-变长结构体的使用-面向对象的思想

是一个函数指针,指向一个接受LLIST *和int参数并返回int的函数。通过这种声明方式,可以灵活地在程序中使用函数指针,以实现动态函数调用和回调机制。

aqiangdeba的博客 430

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

90.Go语言中实现可选参数的几种方法:可变长参数、使用Map、结构体和函数选项模式

这篇博文介绍了在Go可变长参数、使用Map、结构体和函数选项模式。每种方法都有其适用场景和优缺点,你可以根据自己的需要选择合适的方法。

YouMing_Li的博客 3657

c语言 变长结构体,变长结构体的用法 - BOOMLZF的个人空间 - OSCHINA - 中文开源技术交流社区...

今天闲来无聊到网站上刷题,其中一道题就是在一个结构体中有这么一个语句:char data[0],当时我就一脸懵逼。学过C了,为何我没有见过这样的表示,这样的表示应该是错的吧!看了大神的解答后才发现这个变长结构体,于是百度变长结构体,理解了什么事变长结构体。C99允许结构体最后使用0或1的长度数组的原因主要是为了方便的管理内存缓冲区,如果你直接使用指针而不使用数组,那么,你在分配内存缓冲区时,就必须...

weixin_39580042的博客 553

深入浅出变长结构体

深入浅出变长结构体 1、 问题的引出 项目中用到数据包的处理,但包的大小是不固定的,其长度由包头的2字节决定。比如如下的包头:88 0f 0a ob cd ef 23 00。长度由头2个字节880f决定,考虑字节序,转为0f88,转为10进制3976个字节的包长度。 这个时候存储包的时候,一方面可以考虑设定包的大小固定:如4K=4*1024=4096个字节,因为最大包长不可能超过4k,但该方法的有缺陷,存在一种极端就是包最小仅含包头不含数据域,此时包为8个字节,浪费了...

weixin_39094034的博客 488

结构体中的变长数组

#include"stdio.h" #include"malloc.h" #include //int 4字节,char 1个字节 struct node{ int member1; char member2[10]; int member3[0];//变长数组 }; void main() { struct node *xiaobo1; //printf("%d",sizeof( struct node)); xiaobo1 = malloc(sizeof(stru

xiaobo620的专栏 5036

变长数组特性

typedef struct st_type { int nCnt; int item[0]; }type_a; 变长数组作为结构体最后一个成员 item 不占用结构体空间,内存大小为0,即sizeof(type_a)的值为nCnt的大小 变长数组的地址紧接着结构体后面 分配空间时一次性分配结构体和变长数组的空间 int num; ... type_a *p = (type_a *)malloc(sizeof(type_a) + num * sizeof(int)); ... free

肥宅不死的博客 200

深入浅出变长结构体【转】

深入浅出变长结构体 1、 问题的引出 项目中用到数据包的处理,但包的大小是不固定的,其长度由包头的2字节决定。比如如下的包头:88 0f 0a ob cd ef 23 00。长度由头2个字节880f决定,考虑字节序,转为0f88,转为10进制3976个字节的包长度。 这个时候存储包的时候,一方面可以考虑设定包的大小固定:如4K=4*1024=4...

289

变长结构体之结构体指针申请内存

什么叫变长结构体? typedef struct { int a; char b[0]; }Student; 那结构体是怎么实现可变长的呢?如上所示,请注意看结构体中的最后一个元素,一个没有元素的数组。我们可以通过动态开辟一个比结构体大的空间,然后让buffer去指向那些额外的空间,这样就可以实现可变长的结构体了。 下面我们来探讨动态开辟一个比结构体大的空间的问题,一般的实现思路都是开辟一个比结构体大的空间,如下: #include <stdio.h> #include

bj318318的博客 734

变长结构体用法总结

C99允许。结构体最后使用0或1的长度数组的原因,主要是为了方便的管理内存缓冲区,如果你直接使用指针而不使用数组,那么,你在分配内存缓冲区时,就必须分配结构体一次,然后再分配结构体内的指针一次,(而此时分配的内存已经与结构体的内存不连续了,所以要分别管理即申请和释放)而如果使用数组,那么只需要一次就可以全部分配出来,(见下面的例子),反过来,释放时也是一样,使用数组,一次释放,使用指针,得先释放结

zjy900507的博客 1731

结构体变长数组的妙用——0个元素的数组

结构体变长数组的妙用——0个元素的数组有时我们需要产生一个结构体,实现了一种可变长度的结构。如何来实现呢?看这个结构体的定义:typedef struct st_type{int nCnt;int item[0];}type_a;(有些编译器会报错无法编译可以改成:)typedef struct st_type{int nCnt;int item[];}type_a;这样我们就可以定义一个...

Ubuntu 14.04配置nfs服务 345
上一篇: Android的init过程:init.rc解析流程
下一篇: win7在64位系统下编译openssl
OS技术解密
博客等级 码龄16年 3760粉丝 189原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值