❶ 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重啟嗎
應該不會很多重啟最多報錯,很多人開始編程的時候都有數組越界的問題,不至於重啟。