我写了一个小C程序来反转一个字符串.即便如此,我声明str为
char*str
然后
str = (char*)malloc(20);
str = "this is a test";
,如果我使用,我不会得到SEGFAULT
char str[20] = "this is a test"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char *a, char *b)
{
char temp;
temp = *a;
*a = *b;
*b = temp;
}
char* reverse(char *str)
{
int len = strlen(str);
printf("len = %d \t %s\n", len, str);
int i = 0;
if (len == 0)
return NULL;
for (i=0; i<len/2; i++)
{
swap((str+i), (str+len-1-i));
printf("%s\n", str); …Run Code Online (Sandbox Code Playgroud) 以下代码在分配给union成员时产生分段错误。这一定是误解工会的结果,但我无法指出我的错误。
Q1:是否有必要在 T1 的构造函数中为适当的联合成员变量包含一些初始化?
我尝试了类似于建议的解决方案(在下面的代码块中找到)来初始化适当的成员,但没有成功。
Q2:如果T1初始化没问题,为什么分配到这个位置会产生segmentation fault?
struct T1
{
T1(int type)
{
this->type = type;
//Q1 : need to init appropriate union struct here also? Something like:
/*
*if (type == 1)
*{
* union_member1.str = "";
*}
*else
*{
* union_member2.value = 0;
*}
*/
//If so, better alternative to this?
}
int type; //tells which union struct is used
union
{
struct //type 1
{
std::string str;
} union_member1;
struct //type 2
{
int …Run Code Online (Sandbox Code Playgroud) 我不确定这个错误意味着什么“run: line 1:3 Segmentation failure(core dumped) ./a.out”,因为这是我第一次遇到它。看来错误发生在这里
dgemm(40, &A, &B, &C);
Run Code Online (Sandbox Code Playgroud)
这是我的代码
void dgemm (int n, double *A, double *B, double *C) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
double cij = C[i + j * n];
for (int k = 0; k < n; k++) {
cij += A[i + k * n] * B[k + j * n];
}
C[i + j * n] …Run Code Online (Sandbox Code Playgroud) 这段代码有什么问题?我收到Segmentation故障!
#include<stdio.h>
int main()
{
struct {
char* name;
int age;
} *emp;
char* empname = "Kumar";
int empage = 31;
emp->name = empname;
emp->age = empage;
printf("empname :%s\n",emp->name);
printf("empage :%d",emp->age);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以及如何纠正这个程序的工作?
我正在创建一个带有一些设计模式的小程序,我正在尝试实现一个单例.在这个单例类中,我有一个包含我自己定义的File对象的向量.
现在,在进行实际的push_back时,我遇到了分段错误.我尝试删除单例模式,push_back工作得很好.我在一个论坛上看到问题可能是由于矢量尚未初始化而引起的.他们的方式是否允许我使用我的矢量和我的单例模式而不会导致分段错误?
编辑:
标题:
class FileManager
{
public:
static FileManager* GetManager();
~FileManager();
void InitManager();
void LoadAllTitle();
private:
FileManager();
static FileManager* _fileManager;
std::vector<File> _files;
};
Run Code Online (Sandbox Code Playgroud)
资源:
//C++ Header
#include <iostream>
//C Header
//local header
#include "filemanager.h"
#include "settings.h"
#include "defs.h"
#include "file.h"
#include "utilities.h"
FileManager* FileManager::_fileManager = NULL;
FileManager* FileManager::GetManager()
{
if( _fileManager )
{
_fileManager = new FileManager();
}
return( _fileManager );
}
FileManager::FileManager()
{
}
FileManager::~FileManager()
{
}
void FileManager::InitManager()
{
int numberOfFile = Settings::GetSettings()->NumberOfFile() + 1;
for( unsigned …Run Code Online (Sandbox Code Playgroud) 可能重复:
是否可以修改C中的字符串?
#include <stdio.h>
void reverseStr(char *str);
main()
{
reverseStr("abcdef");
}
void reverseStr(char *str) {
char *tmp = str;
char curr;
while (*tmp != '\0') {
tmp++;
}
tmp--;
while (tmp > str) {
curr = *str;
*str = *tmp;
*tmp = curr;
str++;
tmp--;
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到:
/usr/bin/runit/srun_c: line 12: 2809 Segmentation fault /tmp/run_c_executable
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?我正在练习面试,我在我的C中生锈了,想要练习一些简单但不能为我的生活弄清楚这一点.我注意到当我注释掉这*str = *tmp;条线时,seg故障消失了,我不明白为什么会导致seg故障.帮助赞赏.
我试图在c中实现一个重复的cat函数.我收到分段错误,我无法找到原因.
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
char* s; /* input string */
int c;
if(argc==1){
while (gets(s)){
puts(s);
}
}
else{
FILE *file = fopen( "./argv[1]", "r" );
while((c=fgetc(file))!=EOF){
fputc(c,stdout);
}
fclose(file);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我真的坚持使用C++中的高斯消除代码,我需要返回上三角矩阵但仍然只有我得到的是分段错误.我知道必须有一些分配的内存,但我找不到.
码:
#include <iostream>
using namespace std;
double ** allocateDynamicArray(int order){
double ** dynArray = new double *[order];
int cols = order+1;
double *pool = new double [order * cols];
for(int i = 0;i < order; i++, pool += cols){
dynArray[i] = pool;
}
return dynArray;
}
void deallocateDynamicArray(double **dynArray){
delete [] dynArray[0];
delete [] dynArray;
}
void addAndPrintArray(double **dynArray, int order){
cout << "Zadejte prvky pole radu " << order << endl;
for(int i=0; i< order; i++){
for(int j=0; …Run Code Online (Sandbox Code Playgroud) 我想在不同的内存位置输入两个字符串,但在第一次输入后,它显示错误"分段错误(核心转储").我没有弄清楚这段代码有什么问题.
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
char *str;
int i;
scanf("%s",str+0);
scanf("%s",str+1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我只接受一个输入时它工作正常.
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
char *str;
int i;
scanf("%s",str+0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么?
我正在尝试做一个简单的人口统计输入 - >输出程序.输入人员信息并将其写入csv文件.但是我无法获得名称部分.我总是遇到分段错误.下面的代码是有问题的位,不起作用.我知道它与字符串有关,如果我改为int它就可以了.所有其他数据输入(为简单起见已删除)有效.
主要
#include <iostream>
#include "People.h"
using namespace std;
void demographics();
int main()
{
demographics();
return 0;
}
void demographics()
{
short elements = 2;
Names test[elements];
vector<string> name2;
for(int i =0; i<=elements; i++)
{
string name;
cout << "Please enter first name for child " << i+1 << endl;
cin >> name;
name2.push_back(name);
test[i].setName(name2);
}
return;
}
Run Code Online (Sandbox Code Playgroud)
People.h
#ifndef PEOPLE_H_INCLUDED
#define PEOPLE_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Names
{
private:
vector<string> names; …Run Code Online (Sandbox Code Playgroud)