最近碰到的一个关于memcpy的奇葩问题

STM32F407上RT-Thread串口DMA接收不定长数据,我踩过的坑和填坑指南 本文详细介绍了在STM32F407上使用RT-Thread实现串口DMA接收不定长数据的实战经验,包括环境配置、中断优先级设置、内存管理、数据边界处理和稳定性验证等关键技巧。特别针对DMA和空闲中断的常见问题提供了解决方案,帮助开发者避免数据断帧、丢包和死机等问题,提升系统稳定性和性能。 阅读详情


最近写代码,碰到一个奇葩问题,memcpy函数用起来,编译居然提示我stackoverflow,简直是对写C的码农的最大羞辱,WTF!

    UINT8 numBuffers = 0;
    UINT16 cpLength = 0;
//  TPM2B_DIGEST *bufferList;
    if(length%(MAX_DIGEST_BUFFER) != 0)
        numBuffers = length/(MAX_DIGEST_BUFFER) + 1;
    else
        numBuffers = length/(MAX_DIGEST_BUFFER);

    TPM2B_DIGEST *bufferList[numBuffers];
    for(UINT8 i = 0; i < numBuffers; i++)
    {
        (bufferList)[i] = (TPM2B_DIGEST *)calloc(1,sizeof(TPM2B_DIGEST));
        if(i < numBuffers-1)
        {
//          memcpy(bufferList[i]->t.buffer, &buffer[cpLength], MAX_DIGEST_BUFFER);
            for( UINT16 m = 0; m < MAX_DIGEST_BUFFER; m++)
            {
                bufferList[i]->t.buffer[m] = buffer[m + cpLength];
            }
            cpLength = i * MAX_DIGEST_BUFFER;
        }
        if(i == numBuffers-1 )
        {
            for(UINT16 j= 0; j < (length-cpLength); j++)
            {
                bufferList[i]->t.buffer[j] = buffer[cpLength + j];
            }
        }

    }

就是上面注释掉的
memcpy(bufferList[i]->t.buffer, &buffer[cpLength], MAX_DIGEST_BUFFER);

编译的时候,结果如下:

In file included from /usr/include/string.h:639:0,
                 from tpm2_sign.cpp:61:
In function ‘void* memcpy(void*, const void*, size_t)’,
    inlined from ‘int computeDataHash(BYTE*, UINT16, TPMI_ALG_HASH, TPM2B_DIGEST*)’ at tpm2_sign.cpp:669:73:
/usr/include/x86_64-linux-gnu/bits/string3.h:53:71: error: call to void* __builtin___memcpy_chk(void*, const void*, long unsigned int, long unsigned int) will always overflow destination buffer [-Werror]
   return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest));

自查半天,没觉得有什么错,网上查了半天,居然查到一个“GCC Bugzilla – Bug 37060”,翻到最后跟我说是“fixed in GCC 4.7.0”

链接在此   GCC Bugzilla -Bug 37060


我gcc -v 查自己的gcc版本, 妈蛋, 都4.9.2了啊,我勒个去。

只好折衷,选个别的办法解决问题,一位位的复制吧,不用memcpy了。


            for( UINT16 m = 0; m < MAX_DIGEST_BUFFER; m++)
            {
                bufferList[i]->t.buffer[m] = buffer[m + cpLength];
            }


尼玛,现在还没弄明白,到底是个神马情况,有时间好好扒一扒这家伙的老底,奶奶个腿儿的,先记下来!


UE5集成4K RTSP流:VLC、OpenCV与InVideo SDK方案深度评测 实时流媒体协议(RTSP)是安防监控、视频会议等领域广泛使用的标准协议,其核心原理是通过RTP/RTCP在IP网络上传输音视频数据。在游戏开发与数字孪生等工程实践中,将外部RTSP视频流无缝集成到实时3D渲染引擎中,能极大扩展应用场景,如构建虚拟监控大屏或沉浸式演播室。然而,处理高码率的4K分辨率视频流对解码性能、线程管理和GPU资源调度提出了严峻挑战。本文聚焦于在虚幻引擎5(UE5)这一前沿实时渲染平台中,系统评测并实践了三种主流技术方案:基于开源VLC库的深度集成、利用OpenCV的轻量级抓取,以及采用 阅读详情

相关推荐

直流充电桩程序源码拆解:从状态机到BMS通信的实战指南

嵌入式系统开发中,状态机设计是处理复杂业务流程的基石,而充电桩正是这一思想的典型应用场景。直流充电桩作为新能源汽车基础设施的核心设备,其嵌入式软件需要协调BMS通信、电表采集、故障管理、HMI显示等多路任务,同时满足国标GB/T 27930的协议要求。理解充电桩程序的分层架构与状态跳转逻辑,不仅能帮助开发者快速上手源码移植,还能在遇到绝缘故障、握手失败等现场问题时精准定位。本文从工程实践角度,梳理了充电桩程序的典型目录结构、核心状态机设计、三路通信协议(CAN、Modbus-RTU、4G)的适配要点,以及故

weixin_33851429的博客 364

ioctl_cfg80211.c:2495:4: error: incompatible type for argument 2 of ‘cfg80211_scan_done’

环境说明 系统:Centos7 (3.10.0) 驱动版本:00014996-RTL8192EU_linux_v5.6.3.1_34030_COEX20171113-0047.20190613.zip 网卡:Realtek RTL8192EU 错误log /home/w1206/RTL8192EU_linux_v5.6.3.1_34030_COEX20171113-0047.20190613/driver/rtl8192EU_WiFi_l...

wgl307293845的博客 2242

NPP/VIIRS夜间灯光数据:从下载到应用的完整指南

本文提供了NPP/VIIRS夜间灯光数据从下载、处理到应用的完整实战指南。详细介绍了如何从官方源获取数据、解读文件结构、进行坐标校正与数据清洗,并深入探讨了其在城市扩张监测、社会经济指标估算、重大事件影响评估等领域的核心应用。文章旨在帮助研究者与从业者快速掌握这一强大的空间数据,将其转化为研究中的实用工具。

gpu4optimizer的博客 864

memcpy引发的C常见指针问题

本博文为原创,转载请注明出处 http://blog.youkuaiyun.com/lux_veritas/article/details/8227386 前段时间调bug,定位了一个字符串赋值的错误,偶然发现了一个有趣的现象,于是乎抽象出一个特定的场景,把问题扩展开来,分析了一个由memcpy函数引发的c常见指针问题和勿用,形成了本篇流水账。 问题描述: main要打印一个字符串,

Lux_Veritas的专栏 2万+

【UV打印机】PrintExp打印软件教程(二)-主界面介绍

PrintExp软件主界面如下图所示PrintExp软件的主界面主要包括主菜单区,工具栏区,界面显示区,状态栏。以下是上述部分的介绍。

Print World的专栏 6300

【Cadence】Virtuoso@LayoutXL 差分电路版图镜像同步技巧详解

本文详细介绍了在Cadence Virtuoso LayoutXL中实现差分电路版图镜像同步的技巧,包括核心需求、环境配置、分步操作指南及高级技巧。通过使用LayoutXL的Group功能,工程师可以高效保持差分电路的对称性,提升版图迭代效率,确保高速信号处理的稳定性。特别适用于深亚微米工艺下的差分放大器设计。

weixin_29215391的博客 323

memcpy_chk.c

__memcpy_chk memcpy

xkdlzy的专栏 1421

单卡调试OpenGait:从分布式到本地的轻量级改造指南

本文为个人开发者和研究者提供了一份详细的OpenGait框架单卡调试指南。针对该框架默认依赖多GPU分布式训练的问题,文章通过分析核心痛点,逐步讲解了如何从环境搭建、绕过分布式初始化陷阱、处理通信后端报错到最终配置与调试,实现从分布式到本地的轻量级改造,帮助读者在单张GPU上高效进行步态识别模型的验证与学习。

weixin_28223453的博客 138

解决error: incompatible type for argument 1 of ‘************’报错

几乎都是该函数的参数类型不匹配导致的;所以检查并纠正函数参数类型即可。

weixin_45510561的博客 1万+

深入解析ADC采样电容:从充电误差到LSB精度的关键设计

本文深入解析了ADC采样电容在模数转换中的核心作用,重点探讨了充电时间常数与采样误差的博弈关系。通过推导最小采样时间公式,揭示了精度、速度、源阻抗与内部电容之间的制约关系,并提供了从理论计算到外部驱动电路设计的实战指南,是确保ADC达到LSB精度的关键。

android的专栏 1103

【上海大学计算机组成原理实验报告】一、数据传送实验

将58H写入A寄存器//将6BH写入W寄存器//将C3H写入R1寄存器//将C3H写入R1寄存器//了解实验仪器数据总线的控制方式//掌握数据传送的基本原理//掌握各寄存器的结构、工作原理及其控制方法//数据传送实验

HackerKevon的博客 3509

LLVM编译器中的内置(built-in)函数

什么是built-in 函数? 在一些.h头文件中或者实现代码中经常会看到一些以__builtin_开头的函数声明或者调用,比如下面的头文件#include <secure/_string.h>中的函数定义: //这里的memcpy函数的由内置函数__builtin___memcpy_chk来实现。 #if __has_builtin(__builtin___memcpy_chk) |...

weixin_33735077的博客 1225

机器学习【学校智慧食堂及其应用】

本文主要介绍机器学习【智慧食堂及其应用】

qq_201729558 2571

Strcpy,memcpy函数的内存重叠

最近想要还工作,所以复习了下C语言的知识点,在看有些面试题跟博客时发现了些问题,我在这里总结下。 大部分题目是这样的,给定char * strcpy(char * strDest,const char * strSrc);原型,然后接下来是写出他的实现。 char * strcpy(char * strDest,const char * strSrc) {

good1st的专栏 812

AutoGLM-Phone 实现“豆包 AI 手机”的自动化操作手机的实战方案教程 让普通手机秒变ai手机

是一个基于视觉语言模型(VLM)的 AI 手机智能助理框架。❌ 不依赖固定坐标❌ 不需要提前录制脚本✅ 能“看懂”手机屏幕✅ 能理解自然语言指令✅ 能自动规划操作路径并执行让 AI 像人一样看屏幕、想步骤、点手机。从工程角度看,AutoGLM-Phone 已经具备“AI 手机助理”的完整雏形能看能理解能规划能执行能跨 App所谓的「豆包 AI 手机」,本质上就是这类能力的产品化封装。而现在,这套能力已经可以被普通开发者亲手跑起来、改起来、甚至接入自己的业务场景。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 593

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

困扰一周的奇葩bug:重复相似代码多,导致单片机程序跑飞

困扰一周的奇葩bug:重复相似代码多,导致单片机程序跑飞

qq_44597856的博客 542

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

【原创】驱动开发中Memory read error导致的蓝屏问题

最近在看着《windows驱动开发技术详解》这本书,模仿着敲了第七章中的模拟文件读写部分。在Debug过程中,蓝屏了好多次并出现了各种奇葩的问题。在调了快两天之后,问题终于解决了!现在在这里一一再现遇到的问题和解决方法。  【在别人博客的评论中看到这么一句,"能让大家节约时间,就是写博客的目的之一",让我很有很深的感触,希望自己能以这个目的持续将博客进行到底,帮助更多的初学者。】 ...

weixin_33935777的博客 164

深度学习工具包 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")

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

win32编程之俄罗斯方块

因为把目标定在了游戏上,最近在学习win32编程 经过一段时间的学习,开始进行编程实现俄罗斯方块,记录一下其中遇到的一些问题 一开始是看网上的教程学习的,但是做完一部分之后发现   会有闪屏的情况   所以又去网上搜索了一下发现要用到双缓冲。 注意:这段开始正确性有待考证! 我一开始的时候是把 drawTeris(); drawBackGround(); drawBlocke

一蓑烟雨任平生 3469

C语言编译器常见错误

在日常使用Dev c++编译器过程中,会遇到各种样的编译错误,在这篇文章中进行总结,以便以后再次遇到,持续更新

指针不指南吗的博客 1万+

Linux 驱动编写之error: incompatible type for argument问题

出现以下类似情景 问题代码如下: init_timer(timerdev.timer); timerdev.timer.function = timer_function; timerdev.timer.data = (unsigned long)&timerdev; 发现是传参错误;加入取地址符即可。

路过的熊的博客 4541

内存拷贝函数 memcpy 的原理及实现

它的功能是从src的开始位置拷贝n个字节的数据到dest。如果dest存在数据,将会被覆盖。memcpy函数的返回值是dest的指针。自己实现的时候,最简单的方法是用指针按照字节顺序复制即可。sizeof(dst)是4,即大部分数据每次按照4字节拷贝,最后不足4字节的再分别拷贝。但是内存区域出现重叠时,这种方法无法规避内存混乱问题。memcpy是memory copy的缩写,意为内存复制,在写C语言程序的时候,我们常常会用到它。后两种方法正确,第一种方法拷贝时无法规避内存重叠的bug。

youzhangjing_的博客 1921

memset和memcpy使用不当而引起的memory溢出

1、 在使用memset和memcpy时,若使用不当,会出现overflow 的问题,今天我就解决了这种问题。 之前,eeprom 总是调不出来,很奇怪,已经困扰我一周了 今天,发现eeprom的寄存器地址竟然是不对的 正常的eeprom地址 被擦了两个字节的eeprom地址 操作中,两个结构体大小是不匹配的,正好多了两个字节,然后eeprom的寄存器地址,就受了无妄之灾 解决方法是,直接用...

ZSZ的博客 4828

[Error] incompatible type for argument 1 of ‘xxxx‘

括号里加个&

m0_74196016的博客 1017

ubuntu 22.04 安装gcc-7、g++-7

【代码】ubuntu 22.04 安装gcc-7、g++-7。

不能将运气当成战略! 6016

strcpy源码解说

今天上了一节课,Linux操作系统。 课上老师讲到一个问题比较噶

1147
上一篇: 关于gdb的一些用法
下一篇: 编译tpm20lib碰到的问题
Benjamin_Xu
博客等级 码龄14年 46粉丝 54原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值