访问其边界之外的数组有多危险(在C中)?有时候我会从数组外部读取(我现在理解我然后访问我的程序的某些其他部分使用的内存,甚至超过它)或者我试图将值设置为数组之外的索引.该程序有时会崩溃,但有时只是运行,只会产生意想不到的结果.
现在我想知道的是,这真的有多危险?如果它损坏我的程序,那就不是那么糟糕了.另一方面,如果它打破了我的程序之外的东西,因为我以某种方式设法访问一些完全不相关的内存,那么它是非常糟糕的,我想.我读了很多"任何可能发生的事情","分割可能是最不好的问题","你的硬盘可能会变成粉红色,独角兽可能会在你的窗下唱歌",这很好,但真正的危险是什么?
我的问题:
我使用OSX 10.7,Xcode 4.6.
我在C++程序中为这样的边界分配值:
#include <iostream>
using namespace std;
int main()
{
int array[2];
array[0] = 1;
array[1] = 2;
array[3] = 3;
array[4] = 4;
cout << array[3] << endl;
cout << array[4] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
程序打印3和4.应该是不可能的.我正在使用g ++ 4.3.3
这是编译和运行命令
$ g++ -W -Wall errorRange.cpp -o errorRange
$ ./errorRange
3
4
Run Code Online (Sandbox Code Playgroud)
只有在分配时才array[3000]=3000会给我一个分段错误.
如果gcc没有检查数组边界,我怎么能确定我的程序是否正确,因为它可能会导致一些严重的问题?
我用上面的代码替换了
vector<int> vint(2);
vint[0] = 0;
vint[1] = 1;
vint[2] = 2;
vint[5] = 5;
cout << vint[2] << endl;
cout …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用malloc和realloc,并针对以下问题提出了一些代码:
我想创建一个未知大小的字符串,不设置任何限制.我可以向用户询问nr个字符,但我更倾向于在用户键入每个字符时调整str的大小.
所以我试图用malloc + realloc做这个,并且想法是每次用户输入一个新的char时,我使用realloc请求+1内存来保存char.
在尝试实现这一点时,我犯了一个错误,最后做了以下事情:
int main () {
/* this simulates the source of the chars... */
/* in reality I would do getch or getchar in the while loop below... */
char source[10];
int i, j;
for (i=0, j=65; i<10; i++, j++) {
source[i] = j;
}
/* relevant code starts here */
char *str = malloc(2 * sizeof(char)); /* space for 1 char + '\0' */
int current_size = 1;
i = 0;
while(i<10) {
char …Run Code Online (Sandbox Code Playgroud) 我正在尝试这段代码,
#include <stdio.h>
main(){
int a[2],i;
a[5] = 12;
for(i=0;i<10;i++){
printf("%d\n", a[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给了我输出:
1053988144
32767
0
3
0
12
-1267323827
32716
0
0
Run Code Online (Sandbox Code Playgroud)
为什么[5]可以访问?不应该通过RunTime错误吗?
这是我的最小可重现示例:
#include <stdio.h>
int main( int argc, char* argv[])
{
printf (" this is the contents of argc:%d\n",argc);
int i;
for (i = 0; i < argc ; i++){
printf(" argv = %d = %s\n",i,argv[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我argc将 for 循环更改为数字时,比方说10,代码在到达之前崩溃10:
$ ./argc one two three
this is the contents of argc:4
argv = 0 = ./argc
argv = 1 = one
argv = 2 = two
argv = 3 = three
argv …Run Code Online (Sandbox Code Playgroud) 我想知道为什么我的分配有效。我创建了ptr哪个是一个指针,ptr2哪个是与ptr.
这是我的代码:
int* ptr = malloc(sizeof(int));
int* ptr2 = ptr;
*ptr2 = 1;
ptr2 = ptr+1;
*ptr2 = 2;
Run Code Online (Sandbox Code Playgroud)
最后,我有一个由两个整数组成的数组:
ptr[0] = 1
ptr[1] = 2
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么我可以在ptr2没有错误或警告的情况下影响值 2 ?我只malloc()为数组中的一个整数位置设置了 -ed,不是吗?
我期望以下代码段使用来为五个成员分配内存calloc。
$ cat calloc.c
// C program to demonstrate the use of calloc()
// and malloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
arr = (int *)calloc(5, sizeof(int));
printf("%x\n", *arr);
printf("%x\n", *(arr+1));
printf("%x\n", *(arr+2));
printf("%x\n", *(arr+3));
printf("%x\n", *(arr+4));
printf("%x\n", *(arr+5));
printf("%x\n", *(arr+6));
// Deallocates memory previously allocated by calloc() function
free(arr);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
但是它似乎分配了五个以上的空间。它分配了六个成员,为什么呢?
./a.out
0
0
0
0
0
0
411
Run Code Online (Sandbox Code Playgroud) 假设我有以下数组
unsigned char array[5];
Run Code Online (Sandbox Code Playgroud)
所以我试图了解访问中到底什么是未定义的
array[6]
Run Code Online (Sandbox Code Playgroud)
据我了解,编译器将尝试读取内部的值*(array+6),因此它被定义。所以,如果我理解正确的话,未定义的是当你尝试读取未初始化的内存时会发生什么。
如果是这种情况,那么这里的最佳答案说,除非有陷阱表示,否则定义了读取未初始化的值,但我们知道 unsigned char 没有这种表示。
那么这里定义得好吗?它总是会读到吗*(array+6)?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *p = malloc(10);
int i;
for(i=0;i<15;i++)
{
p[i]='c';
printf("INDEX:%d %c\n",i,p[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么在上面的代码中,我只分配了 10 块内存,但我仍然能够访问指针的第 15 个索引。
我不确定为什么,但我认为这是因为这个指针指向一些随机的内存块,而我只是覆盖了这部分内存,但我只分配了特定数量的内存,所以我不确定为什么它会起作用。
有人可以确认吗?
我一直在尝试这个简单的代码和C中的二维数组,不明白为什么我的程序不会失败.
假设指数超出范围?
#include <stdio.h>
#include <stdlib.h>
#define row 20
#define cols 20
int main(int argc, char *argv[])
{
int x,y;
int array[row][cols];
int cc = array[40][40];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问候
据我所知,C数组不能动态增长.但是为什么指数= 5和6在下面工作?
char carray[5];
carray[0] = 'a';
printf("carray[0]=%c\n", carray[0]);
carray[5] = 'b';
printf("carray[5]=%c\n", carray[5]);
carray[6] = 'c';
printf("carray[6]=%c\n", carray[6]);
Run Code Online (Sandbox Code Playgroud)
输出:
char_array [0] =
char_array [5] = b
char_array [6] = c
任何人都可以澄清一下吗?
char str[1];
strcpy(str, "HHHHHHHHHHHH");
Run Code Online (Sandbox Code Playgroud)
这里我声明了一个大小为1的char数组,但是程序不会崩溃,直到我输入超过12个字符而且我只有一个数组大小.为什么?