这是我的最小可重现示例:
#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) 我正在用 C 语言开发一个程序,它使用链表数组(原始哈希表)作为数据类型来表示某些日期信息。该数组有十二个元素对应于一年中的几个月,每个月都有一个包含数据节点的链表。
我开发了使用这种数据类型的模块,它运行良好。后来我发现我正在访问越界的数组元素(例如通过索引 12 而不是 11 访问第 12 个元素)。但该程序始终如一地运行,没有发生任何事故。我从未收到过分段错误。我已经纠正了编码错误。有人能解释为什么访问越界元素不会导致段错误吗?
这已经不是第一次发生了。我创建了一个动态分配的多维数组,为了测试,我尝试访问越界元素。该程序运行良好,产生了准确的结果,并且在大多数情况下不会出现段错误。我唯一一次实现了,我不得不尝试访问实质上越界的元素。
(这些程序目前是用于测试的 Windows 控制台应用程序。我正在使用 MinGW 进行编译。如果有帮助,我可以包含代码。)
我想要一个介于1到6之间的四个值的表.
我正在使用: rand() % 6 + 1;
这应该给出1到6之间的值.
除非rand()生成值0.
我一直得到7分.我不想要任何7个
兰德的范围是多少?我如何阻止它生成任何0值?
替代解决方案非常受欢迎.
我的老师给了我们使用"随机"的线索.
我们在学校使用Borland C++ Builder 5.
我在家里使用Dev-C++ 5.3.0.3.
我发现它们如何工作有一些差异,我觉得很奇怪..
我不能使用random(),它给了我未在范围内声明...
int main (){
int I;
int Fasit[3];
srand (time(NULL) );
for(I=0; I<4; I++) {
Fasit[I]=rand() % 6 + 1;
}
std::cout << Fasit[0] << " " << Fasit[1] << " " << Fasit[2] << " " << Fasit[3] << " ";
return 0;
}
Run Code Online (Sandbox Code Playgroud)我得到的一些价值观:
2 6 1 7
5 2 1 4
5 2 1 4 …Run Code Online (Sandbox Code Playgroud) 为什么这没有任何错误?
int array[2];
array[5] = 21;
cout << array[5];
Run Code Online (Sandbox Code Playgroud)
打印出21就好了.但看看这个!我改变了5到46,它仍然有效.但是当我放47时,它没有打印任何东西.在任何地方都没有显示任 那是怎么回事!?!?
我正在尝试打印一个数组但是我没有得到所需的输出,在循环完成打印预定义数组后会出现奇怪的数字.
代码是:
#include <stdio.h>
int main(){
int intArray[11] = {1,2,8,12,-13,-15,20,99,32767,10,31};
int i=0;
for(i=0;i<sizeof(intArray);i++){
printf("%d\n",intArray[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
8
12
-13
-15
20
99
32767
10
31
11
1629976468
2674040
2665720
1627423265
1
2665616
-2147417856
1629976534
1629976468
2674040
0
1627423172
1629976532
0
1629110043
0
0
0
0
0
0
0
0
0
0
0
1629976538
0
1629956432
2674276
0
1627407935
Run Code Online (Sandbox Code Playgroud) int main()
{
int a[2] = {1,2};
a[2] = 3;
printf("\n\n%d %d %d\n\n",a[0],a[1],a[2]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我输出为 1 2 3
为什么在运行时或编译时没有抛出错误?
为什么char在这种情况下分配0大小的块有效?但如果我写char *string = NULL;它将无法正常工作.
我正在使用Visual Studio.
int main()
{
char *string = (char *)malloc(0);
string[0] = 'a';
string[1] = 'b';
string[2] = 'c';
string[3] = 'd';
string[4] = '\0';
printf("%s\n",string);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在为学校作业编写的C++程序中遇到了一个非常奇怪的错误(最后粘贴的代码),我无法弄清楚它为什么会这样做.特别是,它有时会给出随机错误的输出,有时会提供正确的输出,每次都在相同的输入上运行.如果有人可能对原因有所了解,我会非常感激:
我做了一个C++程序,它有一个简单的MaxHeap数据结构的实现,它支持使用HeapInsert构建堆,从空堆开始逐个地将元素插入堆中,或者从元素数组开始并使用在元素的前半部分冒泡以将其转换为堆 - 程序采用一个命令行参数,HeapInsert将使用第一种构建堆的方法,或BubbleDown将使用第二种方法构建堆.
该程序从cin获取用户输入:首先是为了使堆出来而给出的元素数,然后是要放入堆中的元素.完成后,它输出在冒泡/冒泡中执行的交换次数,然后输出堆的元素,以便它们位于存储堆的数组中.
我们已经给出了一个示例输入(100个随机数)和一个我的代码应该生成的示例输出,以便知道我们的实现是正确的.我在命令行上执行以下操作:
g++ HeapTest.cpp
./a.out BubbleDown < 100.txt > out
diff out s100b.txt
Run Code Online (Sandbox Code Playgroud)
100.txt是样本输入,s100b.txt是正确的样本输出.
执行行
./a.out BubbleDown < 100.txt > out
diff out s100b.txt
Run Code Online (Sandbox Code Playgroud)
反复,我得到不一致的结果.似乎有一半的时间我得到了我的输出完全匹配样本文件,但有一半时间它没有,特别是当我查看我的输出文件时,它看起来像一个随机的大数字已被插入我的堆中没有原因,使我的输出错误.
对我来说,完全没有意义的结果是在使用完全相同的输入重复运行代码时会不一致.这只发生在我在命令行上使用"BubbleDown"选项时.以下是我的代码:
#include <cstdlib>
#include <stdint.h>
#include <iostream>
#include <string>
#include <cstring>
#include <cassert>
#include <cmath>
using namespace std;
struct MaxHeap { //MaxHeap data structure
int n; //size of the heap
int numex; //number of exchanges in building the heap
int* A; //Array storing the actual heap
MaxHeap(int a){ //First …Run Code Online (Sandbox Code Playgroud) 我根据'memcpy'手册页编写了这段小代码.我执行了它,但我没有得到任何错误.通常,我应该得到'seg fault',因为我试图将消息复制到小目的地.
char str1[1];
char str2[] = "Big Message";
memcpy(&str1, &str2, strlen(str2));
Run Code Online (Sandbox Code Playgroud) 我有理解这段代码的问题,尤其是"vet-1"部分.这是什么意思?它返回的数组项目是什么?谢谢
#include <stdlib>
#include <iostream>
using namespace std;
void change( int m, int n[7]);
int main(){
int vet[] = {1,2,3,4,5};
change(vet[4],vet-1);
change(0,&vet[4]);
int i=0;
for (i=0;i<5;i++) cout << vet[i];
return 0;
}
void change( int m, int n[7]) {
(*(n+m))--; m++; n--;
}
Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
int main()
{
int lists[30];
lists[30] = 30;
lists[0] = 31;
cout << lists[30];
cout << "\n";
cout << lists[0];
for(int a = 0; a < lists[30]+lists[0]; a++)
{
cout << "\n";
cout << lists[2];
}
}
Run Code Online (Sandbox Code Playgroud)
在for循环之后,它应该只显示列表[2]的值61次; 列表[30] = 30并列出[0] = 31,所以组合它们是61. for循环中的表达式是否有效?它似乎永远循环.