导航:首页 > 操作系统 > linux数组越界

linux数组越界

发布时间:2022-07-16 00:26:47

linux 脚本无法得到正确的结果 已经验证过文件读取到数组没有问题

#问题在于对于数组file_list的引用错误,修改如下:
#!/bin/sh
#
listname=$1
pathname=$2
#
k=0
whilereadLINE
do
file_list[$k]=$LINE
k=$k+1
done<$listname

cd$pathname

i=0
j=0
forLINEin"${file_list[@]}"#问题在于这里对数组的引用
do
FILE=`find.-name$LINE-print-quit`
if[-n"$FILE"];
then
a[$i]=$FILE
i=$i+1
else
b[$j]=$LINE
j=$j+1
fi
done
echo"foundfiles:"
foriin"${a[@]}";do
echo$i
done
echo"missingfiles:"
forjin"${b[@]}";do
echo$j
done

另外补充一点:

数组下标:最好不要使用k=$k+1表达,应该使用((k+1))或者k=`expr $k + 1`。因为,如果列表文件太多,会引起数组越界,超过系统允许数组下标的最大长度。

希望能够帮助到你,你的好评是我前进的动力,谢谢!

❷ Linux调试c程序, 用yylex()函数,一遇到“{”就 segmentation fault

数组越界,跟 { 没关系, 可能是 你{}的数组成员 过多了
例如
char a[5]={'1','2','c','b','5','6'}, 5个空间 6个 就越界

❸ linux数组Segmentation fault (core mped)求大佬帮忙看看哪里错了

Segmentation fault就是段错误,肯定你是你内存使用的不正确,多部分是指针使用错,越界了

❹ 数组越界

任何的变量只是内存的一种表示而已,你定义了数组int a[2]表示你申请了4字节连续的内存空间,a就代表其首地址,这片内存不会被别的程序使用,只有你的程序会使用它存取数据;a[4〕虽然没有定义,但是这个内存空间是存在的,就是a+4,这片空间可能是被别的程序使用了,也可能是闲置着的,你不一定无权访问,但是你如果访问了很有可能会得不到你想要的结果,甚至会影响系统的运行,很有危险的!!

是啊!!

❺ 请检查是否存在数组越界非法访问等情况,这个怎么解决

1:一般都是非法内存操作,例如数组越界,例如申请a[5],却访问到a[5]或者a[6],这也会有很多情况,可能是循环操作时循环变量控制有问题,可能是字符串拷贝时长度发生溢出;
2:指针指向了非法内存,例如申明一个指针,但是没有对指针进行初始化,直接就引用,或者引用里面的元素或者函数,或者没有分配内存就进行释放等,另外,申请内存还要检查是否申请成功,如果没有申请成功也会出现这种情况;
3:单步调试或者加打印信息,细心一点总可以找到错误的,注意编译成调试版本;
4:如果是linux,可以产生core文件,从core文件查看出错的地方。

❻ linux运行程序段错误··要怎么解决··

编程中以下几类做法容易导致段错误,基本是是错误地使用指针引起的

1)访问系统数据区,尤其是往 系统保护的内存地址写数据
最常见就是给一个指针以0地址, unsigned char *ptr = 0x00;
2)内存越界(数组越界,变量类型不一致等) 访问到不属于你的内存区域
我以前也遇到过这个问题后来就是参考这个帖子找到问题的,希望可以帮助到你
http://blog.csdn.net/yeyuangen/article/details/6822004

❼ linux数组越界漏洞怎么利用

Linux c/c++上常用内存泄露检测工具有valgrind, Rational purify。Valgrind免费。Valgrind可以在 32位或64位 PowerPC/Linux内核上工作。
Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。下面分别介绍个工具的作用:
Memcheck 工具主要检查下面的程序错误:
• 使用未初始化的内存 (Use of uninitialised memory)
• 使用已经释放了的内存 (Reading/writing memory after it has been free’d)
• 使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
• 对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
• 申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
• malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
• src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)
Valgrind不检查静态分配数组的使用情况。
Valgrind占用了更多的内存--可达两倍于你程序的正常使用量。如果你用Valgrind来检测使用大量内存的程序就会遇到问题,它可能会用很长的时间来运行测试
2.1. 下载安装
http://www.valgrind.org
安装
./configure;make;make install
2.2. 编译程序
被检测程序加入–g -fno-inline 编译选项保留调试信息。

2.3. 内存泄露检测
$ valgrind --tool=memcheck --log-file=/root/valgrind_log_all --leak-check=full --error-limit=no --show-reachable=yes --trace-children=yes /usr/local/sdata/sbin/rpcbakupsvr
其中--leak-check=full 指的是完全检查内存泄漏,--show-reachable=yes是显示内存泄漏的地点,--trace-children=yes是跟入子进程。当程序正常退出的时候valgrind自然会输出内存泄漏的信息。
1.内存泄露:
#include <stdio.h>void function()
{ int *p = (int*)malloc(10*sizeof(int)); p[10] = 0;
}int main()
{ function(); return 0;
}

相关日志:
==20220== Memcheck, a memory error detector
==20220== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20220== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for right info
==20220== Command: ./test
==20220== Parent PID: 20160
==20220==
==20220== Invalid write of size 4
==20220== at 0x80483FF: function (in /mnt/Documents/Training/valgrind/test)
==20220== by 0x8048411: main (in /mnt/Documents/Training/valgrind/test)
==20220== Address 0x41be050 is 0 bytes after a block of size 40 alloc'd
==20220== at 0x4028876: malloc (vg_replace_malloc.c:236)
==20220== by 0x80483F5: function (in /mnt/Documents/Training/valgrind/test)
==20220== by 0x8048411: main (in /mnt/Documents/Training/valgrind/test)
==20220==
==20220==
==20220== HEAP SUMMARY:
==20220== in use at exit: 40 bytes in 1 blocks
==20220== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==20220==
==20220== LEAK SUMMARY:
==20220== definitely lost: 40 bytes in 1 blocks
==20220== indirectly lost: 0 bytes in 0 blocks
==20220== possibly lost: 0 bytes in 0 blocks
==20220== still reachable: 0 bytes in 0 blocks
==20220== suppressed: 0 bytes in 0 blocks
==20220== Rerun with --leak-check=full to see details of leaked memory
==20220==
==20220== For counts of detected and suppressed errors, rerun with: -v
==20220== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)
2.使用未初始化的内存
#include <stdio.h>int main()
{ int a; if (a==1)
{ printf("a==%d\n",a);
} return 0;
}

日志分析:
==20345== Memcheck, a memory error detector
==20345== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20345== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for right info
==20345== Command: ./test
==20345==
==20345== Conditional jump or move depends on uninitialised value(s)
==20345== at 0x80483F2: main (in /mnt/Documents/Training/valgrind/test)
==20345==
==20345==
==20345== HEAP SUMMARY:
==20345== in use at exit: 0 bytes in 0 blocks
==20345== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==20345==
==20345== All heap blocks were freed -- no leaks are possible
==20345==
==20345== For counts of detected and suppressed errors, rerun with: -v
==20345== Use --track-origins=yes to see where uninitialised values come from
==20345== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)
可以使用--track-origins=yes 得到更多的信息
3.内存读写越界
#include <stdio.h>int main()
{ int *a = (int*)malloc(5*sizeof(int)); a[5] = 1; return 0;
}

==20368== Memcheck, a memory error detector
==20368== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20368== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for right info
==20368== Command: ./test
==20368==
==20368== Invalid write of size 4
==20368== at 0x8048404: main (in /mnt/Documents/Training/valgrind/test)
==20368== Address 0x41be03c is 0 bytes after a block of size 20 alloc'd
==20368== at 0x4028876: malloc (vg_replace_malloc.c:236)
==20368== by 0x80483F8: main (in /mnt/Documents/Training/valgrind/test)
==20368==
==20368==
==20368== HEAP SUMMARY:
==20368== in use at exit: 20 bytes in 1 blocks
==20368== total heap usage: 1 allocs, 0 frees, 20 bytes allocated
==20368==
==20368== LEAK SUMMARY:
==20368== definitely lost: 20 bytes in 1 blocks
==20368== indirectly lost: 0 bytes in 0 blocks
==20368== possibly lost: 0 bytes in 0 blocks
==20368== still reachable: 0 bytes in 0 blocks
==20368== suppressed: 0 bytes in 0 blocks
==20368== Rerun with --leak-check=full to see details of leaked memory
==20368==
==20368== For counts of detected and suppressed errors, rerun with: -v
==20368== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)
4.内存申请释放管理错误
#include <stdio.h>int main()
{ int *a = new int[5]; /*free(a);*/ delete a; return 0;
}

❽ 谁能解释一下为什么我的数组初始化!linux系统vi编辑器,a[9]越界,不报错也没警告。

这个问题很好!首先明白 数组在栈中分配,到a[8] 已ok a[9]=o;会把下一个地址的值改写为0.(栈溢出)
c对数组越界不报错的,需要程序员自己检查,这也是一些人批评c的原因!这个问题有个更好的代码 手头没有 不过这个问题理解明白就好了 溢出也是被攻击的地方之一

运行起来就一直跑着(像死循环一样,结束不了main函数)解释这个 原本i到8就停止了!!!

但是a[9] =0;恰巧改的位置就是I的值!!!所以i=0;for循环就结束不了,但是这有个前提,栈的方向是从高到低,不懂?这样理解吧.编译器帮你分好地方后是这样的:
a[0] .....a[8] a[9] (i) 其实a[9] 和i一个位置 你打印他们的地址看看 比较下 若栈从低到高 就不会了

❾ 数组越界会导致linux重启吗

应该不会很多重启最多报错,很多人开始编程的时候都有数组越界的问题,不至于重启。

阅读全文

与linux数组越界相关的资料

热点内容
农行app怎么开网银 浏览:649
java迭代器遍历 浏览:301
闽政通无法请求服务器是什么 浏览:48
怎么做积木解压神器 浏览:203
王者荣耀解压玩具抽奖 浏览:49
12位是由啥加密的 浏览:868
程序员编迷你世界代码 浏览:895
php取现在时间 浏览:246
单片机高吸收 浏览:427
怎么区分五代头是不是加密喷头 浏览:244
hunt测试服务器是什么意思 浏览:510
2013程序员考试 浏览:641
毕业论文是pdf 浏览:736
服务器跑网心云划算吗 浏览:471
单片机定时器计数初值的计算公式 浏览:801
win7控制台命令 浏览:567
猫咪成年app怎么升级 浏览:692
360有没有加密软件 浏览:315
清除cisco交换机配置命令 浏览:751
华为删除交换机配置命令 浏览:473