指针作为参数传入函数的陷阱

C语言中指针传参 目录C语言中指针传参一、如何改变参数的值1、常见的错误写法:2、解决办法使用指针二、与return进行比较1、使用return返回值:2、使用指针改变多个参数的值三、传入的参数是指针如何处理?使用指针的指针 C语言中指针传参 一、如何改变参数的值 1、常见的错误写法: 由于变量会随着栈的退出而销毁,所以下面的函数无法改变a的值。 #include<stdio.h> void f(int b){ //参数b初始化为1 b=3;//改变b的值为3 }//随着函数的结束,变量b被销毁,a 阅读详情

下面以一个例子来引出这种错误:

#include <iostream>
using namespace std;
#include <stdlib.h>
#include <string.h>
void func(int *p)
{
    p = (int *)malloc(sizeof(int) * 10);
    memset(p, 0, sizeof(p));
    p[0] = 1;
}
int main()
{
    int *p = NULL;
    func(p);
    cout << p[0] << endl;
    return 0;
}

一个很简单的函数,就是给*p在函数中分配空间并将p[0]置成1,最后打印输出p[0]。但是运行的结果却是segmengt fault。

我们通过查看这段程序的汇编代码来分析一下出现段错误的原因。

Dump of assembler code for function func(int*):
   0x00000000004008cd <+0>:     push   %rbp
   0x00000000004008ce <+1>:     mov    %rsp,%rbp
   0x00000000004008d1 <+4>:     sub    $0x20,%rsp
   0x00000000004008d5 <+8>:     mov    %rdi,-0x18(%rbp)
   0x00000000004008d9 <+12>:    mov    $0x28,%eax
   0x00000000004008de <+17>:    mov    %rax,%rdi
   0x00000000004008e1 <+20>:    callq  0x400780
C语言指针参数陷阱 本人是一个刚开始学数据结构的大二学生,最近在学习链表的时候出现了一些小问题,一下是这个问题的详细分析,虽然是很简单,很可笑的一个小问题,却也值得深究 #include #include /*这里构造一个结构体,用来表示链表的一个节点*/ typedef struct ysf{ int no,psw; struct ysf *next; }YSF,PYSF; 阅读详情

相关推荐

3 使用指针的7大陷阱

C语言之父丹尼斯·里奇曾说过:“C语言诡异离奇,陷阱重重,却获得了巨大成功!”这句话一方面体现了C语言的灵活性以及广泛的使用,另一方面也体现了C语言的复杂性。在使用C/C++遇到的所有陷阱和问题中,和指针相关的占了绝大部分。C/C++是灵活度和自由度较大的编程语言,指针更是让程序员可以越过安全的栅栏,对某些内存区域进行破坏性的访问,从而引发安全风险和严重问题。C/C++的指针是一把双刃剑,为我们带来了巨大的灵活性,但使用不当的话,也会带来各种各样的问题。

hope_wisdom的技术博客 1619

Zabbix在Ubuntu 22.04上的性能优化与监控实战:从基础安装到高级配置

本文详细介绍了在Ubuntu 22.04系统上部署和优化Zabbix监控系统的完整流程,从基础安装到高级配置。内容包括MySQL数据库设置、Zabbix服务器与Web前端优化、性能调优策略以及实战监控场景应用,帮助用户构建高效的企业级监控解决方案。

sss66的博客 895

玩转Zabbix智能告警:降噪、排班、认领、升级、IM协同

集成Zabbix告警到FlashDuty,获得告警降噪、智能排班、IM协同等能力

n9ecommunity的博客 1040

手把手教你用LIO-SAM搞定激光雷达和IMU联合标定(附避坑指南)

本文详细介绍了使用LIO-SAM进行激光雷达与IMU联合标定的全流程,包括标定原理、工具选型、硬件配置、数据采集、LI_calib标定步骤及LIO-SAM集成验证。特别提供了避坑指南和参数优化技巧,帮助开发者高效完成传感器标定,提升自动驾驶和机器人定位精度。

hadoop5ranger的博客 995

基于STM32开发的智能语音控制系统

本文将介绍如何使用STM32微控制器开发一个智能语音控制系统,通过语音识别模块、OLED显示屏、Wi-Fi模块等实现对家用电器或其他设备的语音控制。智能语音控制系统的核心是STM32微控制器,通过语音识别模块识别用户的语音指令,并根据指令控制家用电器或其他设备。:定期测试语音识别模块、继电器控制模块、OLED显示屏和Wi-Fi模块的工作状态,确保系统能够在各种情况下正常运行,并保持智能语音控制系统的可靠性。本系统同样适用于办公环境,可以通过语音指令控制办公室设备,如照明、空调等,提供智能化的办公体验。

stm32d1219的博客 6911

前端开发学习--登录和注册页面设计(html和css,可完成两个页面点击切换)

今天学习了登录和注册页面的设计,主要是HTML和CSS部分,注册和切换采用了Js代码进行,后面将通过js进行填写内容的验证,进而可通过JSP跳转到Servlet进行DAO数据的校验,这属于后端部分了。 总体设计思路:整个页面设计可以分为主体(content)、头部(content-header)、内容(content-body)三个部分组成,其中是主体是根部,包含了头部和内容,头部和内容各自下设两...

weixin_43691879的博客 1万+

手把手教你在Keil/IAR中移植SEGGER_RTT组件(避坑指南+性能对比)

本文详细介绍了在Keil/IAR开发环境中移植SEGGER RTT调试组件的完整流程与优化技巧。通过对比传统串口调试,SEGGER RTT凭借零硬件占用、高速传输和双向交互等优势,显著提升嵌入式开发效率。文章包含配置步骤、性能调优方案及常见问题解决方案,是嵌入式开发者必备的实用指南。

y7z8a的博客 243

MIPI DSI2接口开发避坑指南:以RK3576的GM8775C桥接芯片调试为例

本文以RK3576平台搭配GM8775C桥接芯片为例,深入剖析MIPI DSI2接口开发中的常见问题与解决方案。文章详细解读了从U-Boot阶段到内核DRM驱动架构的完整显示通路构建,重点分享了电源时序管理、寄存器配置与硬件协同调试等实战经验,为嵌入式显示系统开发者提供了一套系统化的避坑指南和调试方**。

weixin_29290379的博客 329

【32】C# WinForm入门到精通 ——打开文件OpenFileDialog 【属性、方法、事件、实例、源码】

本文介绍了WinForm开发中OpenFileDialog控件的关键属性及其应用。OpenFileDialog是.NET Framework提供的文件对话框控件,主要用于选择、打开文件操作。文章详细讲解了11个核心属性:AddExtension(自动补全扩展名)、AutoUpgradeEnabled(外观升级)、CheckFileExists/CheckPathExists(文件/路径存在验证)、DefaultExt(默认扩展名)、DereferenceLinks(解析快捷方式)、FileName/File

程序员,他们想的是什么?他们想的永远都是技术,他们崇尚的也永远都是技术。 1534

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

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

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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

上一篇: 解析变长结构体的用法和优点
下一篇: C++实现不能被继承的类的两种方法
UKey_
博客等级 码龄10年 258粉丝 186原创
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值