int *p=(int * )malloc(sizeof(int));
delete p;
Run Code Online (Sandbox Code Playgroud)
当我们使用malloc分配内存时,我们应该使用free释放它,当我们在C++中使用new分配时,我们应该使用delete释放它.
但是如果我们使用malloc分配内存然后使用delete,那么应该会有一些错误.但是在上面的代码中,C++中没有错误或警告.
此外,如果我们使用new进行反向和分配并使用free进行释放,那么也没有错误或警告.
为什么会这样?
我在SO中看到了很多关于在C程序中获取分段错误的问题,我认为在这里引用这些是很好的,这是一些导致分段错误的问题.我的答案发布在下面.
正如在一些答案中所写,对于所有情况,行为都是未定义的,尽管许多人将它们视为 分段错误,因此这个问题是关于导致这种"症状"的原因.
在下面的例子中,当我运行程序时出现分段错误,你能确定原因吗?
1)
char *str = "foo";
str[0] = 'b'; // << Segfault hre
Run Code Online (Sandbox Code Playgroud)
2)
char str[] = "foo";
char *newStr = malloc(strlen(str));
strcpy(newStr, str);
free(newStr); // << Segfault here
Run Code Online (Sandbox Code Playgroud)
3)
char *str = malloc(4 * sizeof(char));
str = "foo";
free(str); // << Segfault here
Run Code Online (Sandbox Code Playgroud)
4)
char *str = malloc(4 * sizeof(char));
strcpy(str, "foo");
free(str);
if (str != NULL)
free(str); // << Segfault here
Run Code Online (Sandbox Code Playgroud)
5)
char *str = "something and then foo";
printf("%s", str[19]); // …Run Code Online (Sandbox Code Playgroud) 我正在上课,并遇到分段错误.根据我的理解,当您访问尚未分配的内存或超出边界时,应该发生seg错误.'当然我要做的就是初始化一个数组(虽然相当大)
我只是误解了如何解析二维数组?错位一个绑定正是导致seg错误的原因 - 我使用嵌套是错误的for-loop吗?
教授提供了时钟功能,所以我希望这不是问题所在.我在Cygwin中运行此代码,这可能是问题吗?源代码如下.也使用c99标准.
要非常清楚:我正在寻找帮助理解(并最终修复)我的代码产生seg错误的原因.
#include <stdio.h>
#include <time.h>
int main(void){
//first define the array and two doubles to count elapsed seconds.
double rowMajor, colMajor;
rowMajor = colMajor = 0;
int majorArray [1000][1000] = {};
clock_t start, end;
//set it up to perform the test 100 times.
for(int k = 0; k<10; k++)
{
start=clock();
//first we do row major
for(int i = 0; i < 1000; i++)
{
for(int j = 0; j<1000; j++)
{
majorArray[i][j] …Run Code Online (Sandbox Code Playgroud) 我知道当你在C程序中做某些事情时,结果是不确定的.但是,编译器不应该生成无效(机器)代码,对吧?如果代码做错了,或者代码生成了段错误或其他什么,那将是合理的......
这应该是根据编译器规范发生的,还是编译器中的错误?
这是我正在使用的(简单)程序:
int main() {
char *ptr = 0;
*(ptr) = 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在编译-O3.那不应该生成无效的硬件指令,对吧?有了-O0,我在运行代码时遇到了段错误.这看起来更加明智.
编辑:它正在生成一条ud2指令......
std::map<int,int> bar;
int foo(int key)
{
bar.erase(key);
return 1;
}
int main()
{
bar[0] = foo(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在使用电栅栏检查内存使用情况时,使用GCC 4.8编译的代码会出现故障.
LD_PRELOAD=libefence.so.0.0 ./a.out
Run Code Online (Sandbox Code Playgroud)
问题来自于编译器生成的代码开始在地图中分配新条目,然后执行foo()以获取要放入的值bar[0].在运行时foo(),条目被破坏,代码最终通过写入未分配的内存结束.
操作的排序方式取决于编译器实现,还是由C++当前标准指定?
这导致分割错误。需要纠正什么?
int main(void)
{
char a_static = {'q', 'w', 'e', 'r'};
char b_static = {'a', 's', 'd', 'f'};
printf("\n value of a_static: %s", a_static);
printf("\n value of b_static: %s\n", b_static);
}
Run Code Online (Sandbox Code Playgroud) 你们在这段代码上给了我很大帮助。首先我要说的是,我不太了解 C,但我正在努力做到这一点。
这是程序应该做的事情:
我只是想测试这个 shell 排序程序与标准库中内置的快速排序。
我尝试过使用和不使用指针。注释掉的部分在完成后应该可以工作。它只会让事情变得更加混乱,哈哈
请帮帮我,到目前为止你们都很棒......
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shellSort(int *A, int n);
void checkSort(int *A, int n);
int main(){
/*Initialize Random Array*/
int unsorted_list[10000000];
int *ptr = &unsorted_list[0];
int random_number;
int i;
srand ( time(NULL) );
for(i=0; i<10000000; i++){
random_number = rand();
unsorted_list[i] = random_number % 10000000;
}
//Do C Shell Sort
double shell_results[10][2];
double clock_diff;
int j=10000000;
clock_t t0, t1;
int …Run Code Online (Sandbox Code Playgroud) 我对C很新,今天我被介绍给Valgrind.我安装它并在我的C计算器/方程解析器上运行它,我正在努力弄清楚为什么我有一个分段错误(核心转储),我得到了这个:
==20== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==20== General Protection Fault
==20== at 0x4008E27: _dl_map_object (dl-load.c:2317)
==20== by 0x40014DD: map_doit (rtld.c:642)
==20== by 0x4010193: _dl_catch_error (dl-error.c:187)
==20== by 0x4002169: do_preload (rtld.c:831)
==20== by 0x4002169: handle_ld_preload (rtld.c:929)
==20== by 0x4004DEE: dl_main (rtld.c:1667)
==20== by 0x40176F4: _dl_sysdep_start (dl-sysdep.c:249)
==20== by 0x4001BB7: _dl_start_final (rtld.c:347)
==20== by 0x4001BB7: _dl_start (rtld.c:573)
==20== by 0x4001267: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==20== by 0x1: ???
==20== by 0x1FFF0008AE: ???
==20== by 0x1FFF0008BB: ??? …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
int main()
{
char *myptr = calloc(500,1);
char *myptr2 = myptr;
*myptr++ = 'A';
*myptr++ = 'B';
*myptr = '\0';
/* This should dereference the beginning of the memory and print until myptr[2] which is '\0' */
printf("Myptr2 points to: %s\n", *myptr2);
free(myptr);
return(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
为什么第13行(printf行)创建了一个SIGSEV?它应该指向内存的开头,然后printf应该打印,直到它到达'\ 0'.