c - 段错误?你能找到吗?因为我不能

标签 c segmentation-fault

到目前为止,我已经查看了大约 15 次,但无济于事。我不明白为什么这是段错误?它甚至没有到达毫无意义的“打印”语句。错误代码确实有效(当我没有共享内存时)我有一个 load.c 程序但它工作得很好(我 100% 确定这一点)

#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/sem.h>
#include "header.h"

//BEGIN MAIN FUNCTION
main()
{
    int id;             //ID to data shmem
    struct StudentInfo *infoptr;    //ptr to data
    int found = 0;          //found 'boolean'
    char input[15];         //user input buffer
    struct StudentInfo *beginptr;   //ptr to beginning of data
    int rcid;           //Read count ID to shmem
    int *rcptr;         //RC ptr
    int sema_set;           //ID to shared semaphores

    //Find the shmem at our ID
        id = shmget(KEY,SEGSIZE,0);
        if(id < 0)
        {
                perror("Query: shmget failed");
                exit(1);
        }

    //set the ptr to our shared mem and attach to program
        infoptr = (struct StudentInfo *)shmat(id,0,0);
        if(infoptr <= (struct StudentInfo *)(0))
        {
                perror("Query: shmat failed");
                exit(1);
        }

    //Get our RC in shared memory 
    rcid = shmget(RCKEY,READCOUNT,0);
    if(rcid < 0)
    {
        perror("Query: shmget failed");
        exit(1);
    }
    //Set ptr to shmem and attach to process
    rcptr = (int*)shmat(rcid,0,0);
    if(rcptr <= (int*)(0))
    {
        perror("Print: Shmat failed");
        exit(1);
    }

    //Get semaphores
    sema_set = semget(SEMA_KEY,NUM_SEMAPHS,0);
    if(sema_set < 0)
    {
        perror("Query: Semget failed");
        exit(1);
    }   

    //Set program to queue up to wait
    Wait(sema_set,1);

    //Increment the read counter
    *rcptr += 1;

    //If we are the first reader, stop writers
    if(*rcptr == 1)
    Wait(sema_set,0);

    //Signal readers
    Signal(sema_set,1);

    //Set our begin ptr
    beginptr = infoptr;

    //Begin user input loop
    while(1)
    {
    //Ask user for input IT DOESN"T EVEN GET TO HERE <--
    printf("Please input a student ID :");
    scanf("%s",input);

    //While the record is not found search  
    while(strcmp(infoptr->Name,"")!=0 && found != 1)
    {
        //If record found, print the record
        if((strncmp(input,infoptr->ID,9)) == 0)
        {
            //Set found
            found = 1;

            printf("\n%s\n",infoptr->Name);
                    printf("%s\n",infoptr->telNumber);
                    printf("%s\n",infoptr->Address);
                    printf("%s\n\n",infoptr->ID);
        }
        else
            infoptr++;
    }

    //If not found, print error message
    if(found == 0)
        printf("Record not found.\n");

    //Wait on readers
    Wait(sema_set,1);
    //Decrement
    *rcptr--;
    //If no readers left
    if(*rcptr == 0)
        Signal(sema_set,0); //Signal writers
    //Signal readers
    Signal(sema_set,1);
    exit(0);        
    }
}

标题

#define KEY  ((key_t)(11111)) /*change it to last five digits of your SSN*/
#define SEGSIZE  sizeof(struct StudentInfo)

#define NUM_SEMAPHS 2
#define SEMA_KEY   ((key_t)(1111)) /* change this to last four digits of SSN */

#define READCOUNT sizeof(int)   //Set the size of shmem for read count
#define RCKEY ((key_t)(4003))   //Set the key of the shmem for RCount

//Struct student info
struct StudentInfo{
  char Name[20];
  char ID[15];
  char Address[50];
  char telNumber[15];
};

//Checks the semaphore whether or not to wait
void Wait(int semaph, int n);
//Signals that it's ok to run
void Signal(int semaph, int n);
//Gets the semaphore information
int GetSemaphs(key_t k, int n);

最佳答案

您的问题可能来自您对 shmat 的使用。在 C 中,永远不要强制转换此类函数的返回类型。您觉得需要它可能意味着您收到了一条虚假的错误消息,该错误消息来自于您缺少“sys/shm.h” header 这一事实。

在这种情况下,gcc 会采用 int 的返回类型,通常是 32 位数量,并将其重新解释为指针。所以 shmat 给你的地址的上半部分丢失了。

一般来说,不要抛弃问题。如果您的所有 header 均已正确编写,则在 C 中很少需要 Cast。转换系统函数的返回类型几乎总是错误的。

关于c - 段错误?你能找到吗?因为我不能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4062615/

相关文章:

集合的 C++ vector 在执行 push_back 后给出段错误

c++ - Typedeffing 函数(不是函数指针)

c - 获取 $$ 来保存 $1 和 $2 的串联

c - 使用 Atmega32 和 At42QT2100 的 SPI

c - 使用指针而不是索引更新数组

生成 "zsh segmentation fault"的C代码

c - 在 MacOS El Captain 上使用 gcc 编译时未找到库

c - 如何创建一个函数以返回 C 中的下一行输入

matlab - 并行运行 MEX 文件时出现段错误

c - 将指针分配给结构 C 时出现段错误