首页 \ 问答 \ 无法弄清楚分段错误(Cant figure out segmentation fault)

无法弄清楚分段错误(Cant figure out segmentation fault)

我无法弄清楚我的分段错误来自哪里。 我把它缩小到代码的功能和片段,但我很难过。 介意看看吗?

PS我要求你们对负面投票不要太苛刻。 我是一名学生,我正在教我自己。 更不用说我操纵字符串这是一个痛苦的c所以我理解我的代码看起来像垃圾。 但请给我一个休息时间。 这就是我在这里学习的原因。

无论如何,让我知道你还需要什么,并提前感谢。

这是控制台输出。

   rpf0024@cse02:~/cse2610$ gcc -std=gnu99 main.c
   rpf0024@cse02:~/cse2610$ ./a.out

   # A demonstration of some simple MIPS instructions
   Loop: sll $t1, $s3,
   add $t1, $t1, $s6
   lw $t0, 0($t1)
   bne $t0, $s5, Exit
   addi $s3, $s3, 1
   j Loop
   Exit:

   Line: 0 #
   Line: 1
   Line: 2 Loop
   Line: 3 add
   Line: 4 lw
   Line: 5 bne
   Line: 6 addi
   Line: 7 j
   Line: 8 Exit
   WompWompWompSplitting string:

   add
   $t1
   $t1
   $s6
   Segmentation fault (core dumped)
   rpf0024@cse02:~/cse2610$

Assem.c - 我已经验证了分段错误是在我的一批“if”语句中的某个地方。 我删除了整个代码块,我的程序运行得很好。

int checkRformat(char *LineArray, struct assem item)
{
    int i = 0;
    char *str;
    char *temp;
    item.opcode = NULL;
    item.shamt = NULL;
    item.funct = NULL;
    item.rs = NULL;
    item.rt = NULL;
    item.rd = NULL;

    printf("Splitting string:\n");
    temp = strdup(LineArray);
    str = strtok(temp, " ,.-#\n\t");
    //this helper function gets called by another.It primarily serves to convert the MIPS code
    //instructions or labels into their binary values. These global variables will then concatenated
    // and be printed onto a .txt file. 
    while(str != NULL)
    {
        printf("%s\n", str);
        str = strtok(NULL, " ,.-#\n\t");

        if(strcmp(str, "add"))
        {
            item.opcode = "000000";
            item.shamt = "00000";
            item.funct = "10100";
        }
        else if(strcmp(str, "$t0"))
        {
            if(strcmp(item.rs, NULL)){item.rs = "01000";}
            else if(item.rt, NULL){item.rt = "01000";}
            else if(item.rd,NULL){item.rd = "01000";}
        }
        else if(strcmp(str, "$t1"))
        {
            if(strcmp(item.rs,NULL)){item.rs = "01001";}
            else if(item.rt,NULL){item.rt = "01001";}
            else if(item.rd,NULL){item.rd = "01001";}
        }
        else if(strcmp(str, "$s6"))
        {
            if(strcmp(item.rs,NULL)){item.rs = "11110";}
            else if(item.rt,NULL){item.rt = "11110";}
            else if(item.rd,NULL){item.rd = "11110";}
        }
    }
}

assem.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct assem
{
        char myString[101];                     //buffer
        char *myLines[20];                      //this will store the lines from a text file..up to 20 lines
        char *myLines2[20];
        char *myWord[20];                       //this stores the 1st words individually.
        char *opcode;
        char *rs;
        char *rt;
        char *rd;
        char *shamt;
        char *funct;
        char counter;                           //counter for number of lines
                                                //printing stuff...prints file directly from whats STORED IN THE ARRAY
};

int readFile(FILE *FileToBeRead, struct assem *item);                                   //takes in the file that needs to be read and splits it into lines
int firstCheck(struct assem item);
int secondCheck(struct assem item);
int checkRformat(char *LineArray, struct assem item);
int checkFormat1(char *LineArray,struct assem item, int offset);
int checkFormat2(char *LineArray, struct assem item, int offset);
int checkFormat3(char *LineArray, struct assem item, int offset);
int checkFormat4(char *LineArray, struct assem item, int offset);
int checkFormat5(char *LineArray, struct assem item, int offset);
int checkFormat6(char *LineArray, struct assem item);
int checkFormat7(char *LineArray, struct assem item);
int checkLabel1(char *Array, struct assem item);
int checkLabel2(char *Array, struct assem item);
int checkLabel3(char *Array, struct assem item);
//int checkSyntax1(char *LineArray, struct assem item);
void removeSpaces(char* str_trimmed, const char* str_untrimmed, struct assem *item);    //HELPER FUNCTIONS TO REMOVE SPACES
void ReadWords(struct assem *item);                                                     //stores the first word of each line in an array for evaluation
void printFile(struct assem item);                                                      //prints some stuff
void printWords(struct assem item);                                                     //checks the first character if its instruction, label, or comment

I am having trouble trying to figure out where my segmentation faults are coming from. I have narrowed it down to the function and snippet of code but I'm stumped. Mind taking a look at it?

P.S. I ask that you guys dont be too harsh on the negative votes. I am a student and im teaching myself plain c as I go. Not to mention im manipulating strings which is a pain in c so I understand my code looks like crap. But give me a break. That's why I'm here to learn.

Anyway let me know what else you need and thanks in advance.

Here is the console output.

   rpf0024@cse02:~/cse2610$ gcc -std=gnu99 main.c
   rpf0024@cse02:~/cse2610$ ./a.out

   # A demonstration of some simple MIPS instructions
   Loop: sll $t1, $s3,
   add $t1, $t1, $s6
   lw $t0, 0($t1)
   bne $t0, $s5, Exit
   addi $s3, $s3, 1
   j Loop
   Exit:

   Line: 0 #
   Line: 1
   Line: 2 Loop
   Line: 3 add
   Line: 4 lw
   Line: 5 bne
   Line: 6 addi
   Line: 7 j
   Line: 8 Exit
   WompWompWompSplitting string:

   add
   $t1
   $t1
   $s6
   Segmentation fault (core dumped)
   rpf0024@cse02:~/cse2610$

Assem.c - I have verified the segmentation faults are somewhere within my batch of "if" statements. I deleted that whole block of code and my program ran just fine.

int checkRformat(char *LineArray, struct assem item)
{
    int i = 0;
    char *str;
    char *temp;
    item.opcode = NULL;
    item.shamt = NULL;
    item.funct = NULL;
    item.rs = NULL;
    item.rt = NULL;
    item.rd = NULL;

    printf("Splitting string:\n");
    temp = strdup(LineArray);
    str = strtok(temp, " ,.-#\n\t");
    //this helper function gets called by another.It primarily serves to convert the MIPS code
    //instructions or labels into their binary values. These global variables will then concatenated
    // and be printed onto a .txt file. 
    while(str != NULL)
    {
        printf("%s\n", str);
        str = strtok(NULL, " ,.-#\n\t");

        if(strcmp(str, "add"))
        {
            item.opcode = "000000";
            item.shamt = "00000";
            item.funct = "10100";
        }
        else if(strcmp(str, "$t0"))
        {
            if(strcmp(item.rs, NULL)){item.rs = "01000";}
            else if(item.rt, NULL){item.rt = "01000";}
            else if(item.rd,NULL){item.rd = "01000";}
        }
        else if(strcmp(str, "$t1"))
        {
            if(strcmp(item.rs,NULL)){item.rs = "01001";}
            else if(item.rt,NULL){item.rt = "01001";}
            else if(item.rd,NULL){item.rd = "01001";}
        }
        else if(strcmp(str, "$s6"))
        {
            if(strcmp(item.rs,NULL)){item.rs = "11110";}
            else if(item.rt,NULL){item.rt = "11110";}
            else if(item.rd,NULL){item.rd = "11110";}
        }
    }
}

assem.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct assem
{
        char myString[101];                     //buffer
        char *myLines[20];                      //this will store the lines from a text file..up to 20 lines
        char *myLines2[20];
        char *myWord[20];                       //this stores the 1st words individually.
        char *opcode;
        char *rs;
        char *rt;
        char *rd;
        char *shamt;
        char *funct;
        char counter;                           //counter for number of lines
                                                //printing stuff...prints file directly from whats STORED IN THE ARRAY
};

int readFile(FILE *FileToBeRead, struct assem *item);                                   //takes in the file that needs to be read and splits it into lines
int firstCheck(struct assem item);
int secondCheck(struct assem item);
int checkRformat(char *LineArray, struct assem item);
int checkFormat1(char *LineArray,struct assem item, int offset);
int checkFormat2(char *LineArray, struct assem item, int offset);
int checkFormat3(char *LineArray, struct assem item, int offset);
int checkFormat4(char *LineArray, struct assem item, int offset);
int checkFormat5(char *LineArray, struct assem item, int offset);
int checkFormat6(char *LineArray, struct assem item);
int checkFormat7(char *LineArray, struct assem item);
int checkLabel1(char *Array, struct assem item);
int checkLabel2(char *Array, struct assem item);
int checkLabel3(char *Array, struct assem item);
//int checkSyntax1(char *LineArray, struct assem item);
void removeSpaces(char* str_trimmed, const char* str_untrimmed, struct assem *item);    //HELPER FUNCTIONS TO REMOVE SPACES
void ReadWords(struct assem *item);                                                     //stores the first word of each line in an array for evaluation
void printFile(struct assem item);                                                      //prints some stuff
void printWords(struct assem item);                                                     //checks the first character if its instruction, label, or comment

原文:https://stackoverflow.com/questions/31797139
更新时间:2023-03-26 13:03

最满意答案

我同意关于重构的评论,但这不是相关的问题。

你绝对应该在每个函数中使用连接,并且听起来好像你没有正确地处理它们。 假设数据库操作包含在你调用的函数中,你的代码应该是这样的:

using (SqlConnection connection = <connection code>)
{
  using (SqlCommand command = <command code>)
  {
    // Execute.
  }
}

在服务器端,连接将保持打开状态。 默认情况下,SqlConnection类启用连接池,因此您将在服务器端看到连接打开。

这种行为是正常的,应该是预料之中的。


I agree on the comment about refactoring, but that's not the pertinent issue here.

You definitely should use a connection in each function, and it sounds like you aren't disposing of them correctly. Assuming the database operations are contained to the function you are calling, your code should look something like this:

using (SqlConnection connection = <connection code>)
{
  using (SqlCommand command = <command code>)
  {
    // Execute.
  }
}

On the server side, the connections are going to stay open. By default, the SqlConnection class enables connection pooling, so you are going to see the connection open on the server end.

This behavior is normal and should be expected.

相关问答

更多
  • webservice的问题[2023-11-16]

    使用Spring HTTP invoker从普通Java类中实现远程服务可以把HTTP通讯的简单性和Java内置的对象序列化机制结合起来。 远程是企业分布式计算中的一项基本组成部分。你从同一台服务器里(Java虚拟机)调用的服务或者类称之为远程服务,但是如果你需要和一个外部程序(在不同的服务器上或者不同的组织里)通讯的话,它就必须实现为远程服务。Spring框架提供了一种独特且灵活的方式来把商业类实现为远程服务。 Spring远程体系结构的核心是服务对象,它们是POJO,也被称为Spring bean。Sp ...
  • 1.web service本身不能做太多事,各种功能都是你自己编程实现的,但是你的程序只能自己用,自己看,别人根本不知道你的程序是什么样的。而web service就是规定了一种方式,告诉你将你的程序如何的部署到服务器上,然后大家通过一种协商好的地址能找到你的程序的说明,说明的格式也是大家都协商好的,所以可以读懂……然后你就可以根据那个地址来调用这个程序的方法了。 你看,web service 本身貌似没干什么,它就是让你可以发布自己的程序和调用别人发布的程序。 2.协议基本就是上面提到的那些大家协商好的东 ...
  • 我认为你应该维护一个数据库连接池。 例如,您可以参考C3P0 。 这样,在初始化类时,在构造函数中,您可以设置数据库连接池(比如说与数据库的10个连接)。 在insertIntoDB方法中,首先从连接池获取连接,执行数据库CRUD操作,最后返回与池的连接。 这样,您就不会通过创建多个连接来对数据库施加压力。 每次调用创建连接也会使它变慢,因此,threr是一个反模式,称为Connection Thrashing 。 I had to create a connection pool on glassfish ...
  • 试试这种方式:)希望它有帮助...... Public Function LookupItem(ByVal strUPC As String) As DataSet Try Dim connUPC As New Data.SqlClient.SqlConnection Dim cmdUPC As New Data.SqlClient.SqlCommand Dim daUPC As New Data.SqlClient ...
  • 在即将出现视图之前,iOS系统会自己调用viewDidAppear 。 如果你想在didReceiveResponse方法之后执行一些函数, NSURLConnectionDelegate中有一个方法可以帮助你。 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 连接成功加载后使用此方法,您可以在此方法中didRecieveRespose之后要执行的函数。 您可以在此处阅读有关NSURLConnectionDelegatePr ...
  • 如果你没有给"where TekstWeb like '%Oven%' and " + vSagsNr + " IS NULL ,查询的这一部分"where TekstWeb like '%Oven%' and " + vSagsNr + " IS NULL将会失败。它看起来像这样: "where TekstWeb like '%Oven%' and IS NULL 如您所见,该查询不正确。 因此,更好的选择是将查询分隔为多个字符串,并且每次要对vSagsNr检查时,都会检查其值,如果它为null或空,则从 ...
  • 我同意关于重构的评论,但这不是相关的问题。 你绝对应该在每个函数中使用连接,并且听起来好像你没有正确地处理它们。 假设数据库操作包含在你调用的函数中,你的代码应该是这样的: using (SqlConnection connection = ) { using (SqlCommand command = ) { // Execute. } } 在服务器端,连接将保持打开状态。 默认情况下,SqlConnection类启用连接 ...
  • 我们创建了使用web服务的dll,并将array [] complex类型转换为flat数据表以返回。 现在即使返回数据为null也没有错误,因为平面表,之前我们有arra []复杂类型,返回null值中断。 We created the dll to consume the webservice and converted the array[] complex type into flat datatable to return. Now even if the return data is null ...
  • 问题是由我们的Web项目中的自定义HTTP模块引起的。 所以它不是WCF问题,而是我们解决方案特有的问题。 谢谢! The problem was being caused by a custom HTTP module in our web project. So it was not exactly a WCF problem, but a problem specific to our solution. Thanks!
  • 如果您使用的是SQL 2005/2008,则可以通过CLR存储过程执行此操作,如果您能够安装和运行这些程序。 欲了解更多信息: http://msdn.microsoft.com/en-us/library/ms190790.aspx If you're using SQL 2005/2008, you could do this from a CLR stored procedure if you have the ability to install and run these. For more in ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。