strcmp段错误引发的思考

本文探讨了在不同环境中使用strcmp函数导致段错误的现象。通过对Linux环境下的gdb工具进行研究,发现编译器优化是导致该问题的原因之一。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        最近在解bug的时候,涉及到strcmp导致的偶现段错误问题。对于strcmp,自己在linux环境下进行了个简单的测试,

测试代码如下:

                      

代码很简单,但我代码中只写strcmp(),这一行时然后分别在linux环境和windows环境中的vs编译时,vs编译运行立马段错误,但是在linux环境下却没段错误。如果开启上图中,屏蔽的代码,在编译运行时,linux和windows环境都

段错误,对于此现象有点疑惑,后面就想到了gdb工具,于是在linux下就用gdb查看了下测试代码的汇编代码,对于

具体怎么使用gdb查看汇编代码,大家可以自己百度,通过查看汇编代码如下所示:

我们可以看到单独的未做任何操作的一句strcmp代码,是没有任何汇编代码对应的,所以对于我刚刚在linux环境下

单独测试strcmp一条简单语句未出现断错误就找到原因了,对于这未做任何事情的语句,编译器在编译时进行了优化。

问题很简单,这里记录下主要是记录解决问题的一种思路。


void UART0_Handler(void)//串口读取字符串 { uint8_t cnt = 0; int32_t uart0_int_status; uart0_int_status = UARTIntStatus(UART0_BASE, true); // Get the interrrupt status.(ALL) UARTIntClear(UART0_BASE, uart0_int_status); //Clear the asserted interrupts if (uart0_int_status & (UART_INT_RX | UART_INT_RT)) // receive or out of time { while(UARTCharsAvail(UART0_BASE)) { //UARTCharPutNonBlocking(UART0_BASE,UARTCharGetNonBlocking(UART0_BASE));NonBlocking Rec_oRd[cnt] = UARTCharGet(UART0_BASE); GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1,GPIO_PIN_1 ); cnt++; } Rec_oRd[cnt] = '\0'; cnt = 0;//UARTStringPut(Rec_oRd); if(strcmp(Rec_oRd, "?")==0) HL_order= 1; //? if(strcmp(Rec_oRd, "RST")==0) IT_order = 1; if(strcmp(Rec_oRd, "GET:DATE")==0) GD_order = 1; if(strcmp(Rec_oRd, "GET:TIME")==0) Gt_order = 1; if(strcmp(Rec_oRd, "GET:ALARM")==0) AG_order = 1; if(strcmp(Rec_oRd, "GET:DISPlay")==0) YG_order = 1; if(strcmp(Rec_oRd, "GET:FORMAT")==0) FG_order = 1; //UARTStringPutNonBlocking(Rec_oRd); // 添加SET命令解析 if (strncmp(Rec_oRd, "SET:", 4) == 0) { char *p = Rec_oRd; char *tokens[3] = {NULL, NULL, NULL}; int token_count = 0; UARTStringPutNonBlocking(Rec_oRd); // 分割字符串(最多3部分) tokens[0] = p; while (*p && token_count < 3 ) { if (*p == ' ') { *p = '\0'; // 替换空格为结束符 if (token_count < 3 && *(p+1)!=' ' ) {token_count++;tokens[token_count] = p + 1;} } p++; } //if(strcmp(tokens[2], "11")==0) Gt_order = 1; //UARTStringPut(tokens[2]); // 复制到全局变量 if (tokens[0]) strncpy(g_command, tokens[0], sizeof(g_command) - 1); if (tokens[1]) strncpy(g_type, tokens[1], sizeof(g_type) - 1); if (tokens[2]) strncpy(g_value, tokens[2], sizeof(g_value) - 1); // 确保字符串以 '\0' 结尾 g_command[sizeof(g_command) - 1] = '\0'; g_type[sizeof(g_type) - 1] = '\0'; g_value[sizeof(g_value) - 1] = '\0'; //if (strcmp(g_type, "YEAR") == 0) Gt_order = 1; } } } 为什么第三个数组是否赋值成功会和命令的长度相关联
06-02
% 读取地块数据 [~, ~, raw] = xlsread('附件1.xlsx'); landData = raw(2:end, 1:3); landTypes = landData(:, 2); landAreas = cell2mat(landData(:, 3)); % 提取不同类型地块的索引和面积 flatDryLandIndices = strcmp(landTypes, '平旱地'); terraceIndices = strcmp(landTypes, '梯田'); hillsideLandIndices = strcmp(landTypes, '山坡地'); irrigatedLandIndices = strcmp(landTypes, '水浇地'); ordinaryGreenhouseIndices = strcmp(landTypes, '普通大棚'); smartGreenhouseIndices = strcmp(landTypes, '智慧大棚'); flatDryLandAreas = landAreas(flatDryLandIndices); terraceAreas = landAreas(terraceIndices); hillsideLandAreas = landAreas(hillsideLandIndices); irrigatedLandAreas = landAreas(irrigatedLandIndices); ordinaryGreenhouseAreas = landAreas(ordinaryGreenhouseIndices); smartGreenhouseAreas = landAreas(smartGreenhouseIndices); % 读取农作物数据 cropData = xlsread('附件2.xlsx', 1, 'A2:G'); cropIDs = cropData(:, 2); cropYields = cropData(:, 5); cropCosts = cropData(:, 6); cropPrices = cropData(:, 7); % 提取不同作物的索引 beanCropIndices = find(contains(cropData(:, 4), '豆类')); grainCropIndices = find(contains(cropData(:, 4), '粮食') & ~ismember(cropIDs, beanCropIndices)); vegetableCropIndices = find(contains(cropData(:, 4), '蔬菜')); mushroomCropIndices = find(contains(cropData(:, 4), '食用菌')); % 定义时间范围、季节范围 t_range = 2024:2030; j_range = 1:2; % 设定豆类作物索引,假设黄豆为豆类代表作物 k_bean = find(cropIDs == 1, 1); % 设定最小种植面积阈值 min_area = 1; % 设定作物种植分散程度方差上限(简化假设) var_upper_limit = 10; % 目标函数:情况(1)超过部分滞销 function obj = objectiveFunction(x, landData, cropData, landAreas, cropYields, cropCosts, cropPrices) numCrops = length(unique(cropData(:, 2))); numSeasons = length(j_range); numLands = length(landAreas); numYears = length(t_range); x_ijtk = reshape(x(1:numCrops * numSeasons * numLands * numYears), numCrops, numSeasons, numLands, numYears); z_ijtk = reshape(x(numCrops * numSeasons * numLands * numYears + 1:end), numCrops, numSeasons, numLands, numYears); obj = 0; for t = 1:numYears for j = 1:numSeasons for i = 1:numLands for k = 1:numCrops yield = cropYields((cropIDs == k) & (j == 1) & (landData(i, 2) == landData(:, 2))); sale = cropData((cropIDs == k) & (j == 1), 8); price = cropPrices((cropIDs == k) & (j == 1) & (landData(i, 2) == landData(:, 2))); cost = cropCosts((cropIDs == k) & (j == 1) & (landData(i, 2) == landData(:, 2))); obj = obj + price * min(yield * x_ijtk(k, j, i, t), sale) - cost * x_ijtk(k, j, i, t); end end end end obj = -obj; % ga函数求最小值,原问题是求最大值,所以取负 end function [c, ceq] = constraintFunction(x, landData, landAreas, min_area, var_upper_limit) numCrops = length(unique(cropData(:, 2))); numSeasons = length(j_range); numLands = length(landAreas); numYears = length(t_range); x_ijtk = reshape(x(1:numCrops * numSeasons * numLands * numYears), numCrops, numSeasons, numLands, numYears); z_ijtk = reshape(x(numCrops * numSeasons * numLands * numYears + 1:end), numCrops, numSeasons, numLands, numYears); c = []; ceq = []; % 土地资源约束 for t = 1:numYears for j = 1:numSeasons c = [c; sum(x_ijtk(:, j, flatDryLandIndices, t)) - sum(flatDryLandAreas); sum(x_ijtk(:, j, terraceIndices, t)) - sum(terraceAreas); sum(x_ijtk(:, j, hillsideLandIndices, t)) - sum(hillsideLandAreas); sum(x_ijtk(:, j, irrigatedLandIndices, t)) - sum(irrigatedLandAreas); sum(x_ijtk(:, j, ordinaryGreenhouseIndices, t)) - sum(ordinaryGreenhouseAreas); sum(x_ijtk(:, j, smartGreenhouseIndices, t)) - sum(smartGreenhouseAreas)]; end end % 各地块农作物种植面积下限 for t = 1:numYears for j = 1:numSeasons for i = 1:numLands for k = 1:numCrops c = [c; min_area * z_ijtk(k, j, i, t) - x_ijtk(k, j, i, t)]; end end end end % 决策变量的约束关系 for t = 1:numYears for j = 1:numSeasons for i = 1:numLands for k = 1:numCrops c = [c; x_ijtk(k, j, i, t) - landAreas(i) * z_ijtk(k, j, i, t)]; end end end end % 禁止连续重茬种植 for t = 1:numYears-1 for j = 1:numSeasons for i = 1:numLands for k = 1:numCrops c = [c; x_ijtk(k, j, i, t + 1) - (1 - z_ijtk(k, j, i, t)) * landAreas(i)]; end end end end % 豆类作物种植要求 for i = 1:numLands bean_count = 0; for t = 1:numYears for j = 1:numSeasons bean_count = bean_count + z_ijtk(k_bean, j, i, t); end c = [c; floor(numYears/3) - bean_count]; end % 各类地块的种植限制 % 平旱地、梯田、山坡地只能种粮食类作物(水稻除外) for t = 1:numYears for j = 1:numSeasons for i = find(flatDryLandIndices | terraceIndices | hillsideLandIndices) for k = setdiff(1:numCrops, [grainCropIndices; find(cropIDs == 16)]) c = [c; z_ijtk(k, j, i, t)]; end end end end % 水浇地种植限制 for t = 1:numYears for i = find(irrigatedLandIndices) % 第一季可种多种蔬菜(大白菜、白萝卜和红萝卜除外),第二季只能种大白菜、白萝卜和红萝卜中的一种 c = [c; sum(z_ijtk(setdiff(vegetableCropIndices, [35, 36, 37]), 1, i, t)) - 1; 1 - sum(z_ijtk([35, 36, 37], 2, i, t))]; end end % 普通大棚种植限制 for t = 1:numYears for i = find(ordinaryGreenhouseIndices) % 第一季可种多种蔬菜(大白菜、白萝卜和红萝卜除外),第二季只能种食用菌 c = [c; sum(z_ijtk(setdiff(vegetableCropIndices, [35, 36, 37]), 1, i, t)) - 1; 1 - sum(z_ijtk(mushroomCropIndices, 2, i, t))]; end end % 智慧大棚种植限制 for t = 1:numYears for i = find(smartGreenhouseIndices) % 只能种蔬菜(大白菜、白萝卜和红萝卜除外) c = [c; sum(z_ijtk(setdiff(vegetableCropIndices, [35, 36, 37]), :, i, t)) - 2]; end end % 作物种植的分散程度限制(简化处理) for t = 1:numYears for j = 1:numSeasons for k = 1:numCrops x_k_vec = reshape(x_ijtk(k, j, :, t), 1, []); var_x_k = var(x_k_vec); c = [c; var_x_k - var_upper_limit]; end end end end % 决策变量个数 numVars = numCrops * length(j_range) * numLands * length(t_range) * 2; % 设定选项 options = optimoptions('ga','PopulationSize',50,'Generations',100,'CrossoverFraction',0.8,'MutationFcn',@mutationuniform); % 调用ga函数求解 [x, fval, exitflag, output, population, scores] = ga(@(x) objectiveFunction(x, landData, cropData, landAreas, cropYields, cropCosts, cropPrices),... numVars,[],[],[],[],[],[],... @(x) constraintFunction(x, landData, landAreas, min_area, var_upper_limit),[],options); % 解码结果 x_ijtk_opt = reshape(x(1:numCrops * length(j_range) * numLands * length(t_range)), numCrops, length(j_range), numLands, length(t_range)); z_ijtk_opt = reshape(x(numCrops * length(j_range) * numLands * length(t_range) + 1:end), numCrops, length(j_range), numLands, length(t_range)); disp('最优解:'); disp(x_ijtk_opt); disp(z_ijtk_opt); 这段matlab代码有错误,请你帮我修改一下
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值