我正在用C语言编写一个简单的web服务器,每当我试图写到我在另一个函数main中声明的char[]时,我就会得到一个分段错误。在有人建议之前,我还没有把它声明为字符串文字。我尝试使用malloc()分配数组中的每个字符,在初始化时将数组中的每个字符设置为空终止符,并且没有将其初始化为任何内容(即,在向其写入新的内容之前不会读取它,因此没有理由将其初始为零),所有这些都没有效果。
所发生的事情的语法如下:
#define MAX_BUFFER_LENGTH 20
int some_function(char *buffer) { //many other params as well
我们处理的是一个常量指针,因此它所持有的地址不能更改。但是引用的内存地址的内容应该是可变的.
尽管如此,在尝试这样做时,我仍然会得到编译/分段错误。
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char * const c_ptr = "firstValue"; // now c_ptr is a const ptr to an immutable string literal,
//we can
我是C++新手。这可能很愚蠢,但我在给类中的struct赋值时遇到了分段错误。有什么建议吗?
#include <iostream>
#include<string>
using namespace std;
struct s { std::string s;};
class A {
public:
A(){}
~A(){}
struct s *ss[10];
};
int main(){
A a;
a.ss[0]->s = "test";
cout&l
我已经写了一个C++函数,它以数组及其大小为参数,并执行某些操作。当我尝试运行这段代码时,它显示了分段错误.Can有人告诉我为什么这段代码显示分段错误以及如何删除它?
void rearrange(long long *arr, int n)
{
long long temp[n];
int max = n-1;
int min = 0;
for(int i=0;i<n;i++){
if(i%2 == 0){
temp[i] = arr[max];
max--;
}
我有下面的代码,它最终导致了分段错误。
import ctypes
from random import randint
class STRUCT_2(ctypes.Structure):
#_pack_=2
_fields_ = [('field_1', ctypes.c_short),
('field_2', ctypes.c_short),
('field_3', ctypes.c_short)]
class STRUCT_1(ctypes.Structur
我试图运行以下代码,但它给了我分段错误:-
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
int dp[MAX][MAX];
string s1, s2;
int lcs(int i, int j)
{
int val;
if ( i < 0 || j < 0)
return 0;
else if (dp[i][j] != -1)
{
return dp[i][j];
}
else
{
我对c语言编程很陌生。我有一个程序,它测试计算一系列3个嵌套循环所需的时间,并在其中计算,每当我试图运行这段代码时,它就会导致分段错误。下面是代码和调试信息:
#include <stdio.h>
#include <sys/time.h>
#define EXIT_SUCCESS 0;
int main(void){
struct timeval start,end;
gettimeofday(&start, NULL);
int n = 100;
int sum[n][n][n];
int firstNum[n][n][n];
in
我是一个C编程新手(在很长一段时间后重新学习它)。我正在尝试使用malloc将内存动态分配给一个2D数组。我尝试过像和这样的stackoverflow上的答案。但我仍然会遇到分段错误。
我的代码如下
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
void allocate2DArray(int **subset, int a, int b)
{
subset = (int **)malloc( a * sizeof(int *));
int i,j;
for(i = 0
在C++之旅中,有以下示例:
int count_x(char∗ p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-ter minated array of char (or to nothing)
{
int count = 0;
while (p)
{
if (∗p==x)
++count;
++p;
}
return count;
}
然后我测试了它,但它不起作用。因为它抛出了一个分段错误,所以我认为我所使用的
我有大约5000行代码,它们在C++中操作一个大数组。问题是,当我试图使数组更大,即A10000,gcc编译器抛给我一个分段错误错误。
我认为解决方案是将数组更改为动态数组。仅通过更改数组变量的声明就可以做到这一点吗?还是我必须改变访问内存的方式?
例如,
我有:
int A[10000][10000]; //rest of the code
我是否可以直接将其更改为:
int** A = new int*[size of input]; //rest of the code
在保留其余代码的同时呢?
谢谢
我一直有一个分段错误,但我不知道这意味着什么,也不知道是什么导致了它(我对编程和C非常陌生)。在这个函数中,由main.c调用,我需要确定二维数组中每一行最小数的索引。
这是我的代码:
#include "my.h"
void findfirstsmall (int x, int y, int** a)
{
int i;
int j;
int small;
small = y - 1;
printf("x = %3d, y = %3d\n", x, y); //trying t