【问题标题】:Segmentation fault while executing c program执行c程序时出现分段错误
【发布时间】:2015-03-20 19:06:20
【问题描述】:

我在以下程序中遇到分段错误错误
我写的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
#include "devicescan.h"
#include <unistd.h>
#include<time.h>
void send_message_function(char*);
typedef struct node NODE;
static int pckt = 0;

NODE *first=0, *temp = 0;

int c = 0;
int main()

{

int i = 0, iret1, count = 0;
while (1) {
    first = (NODE *) malloc(sizeof(NODE));

    pckt = pckt + 1;
    if (c == 0 || c == 4) {
        sleep(2);
        printf("device scanning\n");
        first = deviceDiscovery();
        printf("here");
        c = 0;
        temp = first;
    }

    while (first != 0) {
        char *dest=malloc(21);
        sleep(2);
        sprintf(dest, first->val);
        printf("device::%s\n", dest);
        //send_message_function(dest);
        first = first -> ptr;
        bzero(dest, 18);

    }
    free(first);
    first = temp;
    c = c + 1;
   }
 }
 void send_message_function(char *dest)

 {
 printf("in fun");
 struct sockaddr_l2 addr = { 0 };
 int s, stat, status;
 char buf[17] = { 0 };
 char data[20];
 int bytes_read;
 printf("in fun");
  s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
  printf("in fun");
// set the connection parameters (who to connect to)
 addr.l2_family = AF_BLUETOOTH;
 addr.l2_psm = htobs(0x1001);
 //addr.l2_psm = htobs(channel);
 str2ba(dest, &addr.l2_bdaddr);
 status = connect(s, (struct sockaddr *) &addr, sizeof(addr));
if (status == 0) {

    printf("Sending message");
    sprintf(data, "%d : message from master", pckt);
    stat = write(s, data, sizeof(data));
    bytes_read = read(s, buf, sizeof(buf));
    if (bytes_read > 0) {
        printf("received %s:%d", buf, pckt);

    }

    bzero(data, 20);
    printf("\n");
  }

  if (status < 0) {

    perror("uh oh");
  }
  sleep(1);

  close(s);

 }

devicesacn.h 文件是:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
//  Pre processor  Directive  for  Memory  Leakage
struct node {
 char val[18];
 struct node *ptr;
  };
 typedef struct node *SNODE;

SNODE deviceDiscovery() {

 typedef struct node NODE;

 int count = 0;
 NODE *head, *first, *temp;

//head = (NODE *) malloc(sizeof(NODE));
inquiry_info *ii = NULL;
first = 0;
int max_rsp, num_rsp;
int dev_id, sock, len, flags;
int i;
printf(" DEVICE SCANNING....\n");
char addr[19] = { 0 };
char name[248] = { 0 };
dev_id = hci_get_route(NULL);
sock = hci_open_dev(dev_id);
if (dev_id < 0 || sock < 0) {
    perror("opening socket");
    exit(1);
}

len = 8;
max_rsp = 255;
flags = IREQ_CACHE_FLUSH;
ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
if (num_rsp < 0)
    perror("hci_inquiry");
//head = (NODE *) malloc(sizeof(NODE));
for (i = 0; i < num_rsp; i++) {
    ba2str(&(ii + i)->bdaddr, addr);
    memset(name, 0, sizeof(name));
    if (hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name), name, 0)
            < 0)
        strcpy(name, "[unknown]");
    head = (NODE *) malloc(sizeof(NODE));
    //first = (NODE *) malloc(sizeof(NODE));
    temp = (NODE *) malloc(sizeof(NODE));
    strcpy(head->val, addr);
    if (first != 0) {
        temp->ptr = head;
        temp = head;
    } else {
        first = temp = head;
    }
    fflush(stdin);

}

free(ii);

close(sock);
printf("returning value");
return first;
}

在调试此程序时,我在 while 内部的主函数中的 sprintf(dest,first->val) 处出现分段错误。 我想我已经为变量分配了内存,那么为什么会出现这个错误?谁能帮帮我。

【问题讨论】:

  • 如果调试是指使用调试器,为什么不在调用sprintf 之前查看first 和first-&gt;val?如果你不是说使用调试器,那你为什么不呢?
  • 调试的时候,你是用GDB吗?如果是这样,那么 while 循环中何时发生段错误(第一次迭代?)。我认为正在发生的事情是它到达最后一个节点,当它到达下一个节点时,该节点没有正确设置为 NULL 或 0 并且默认为垃圾数据。因此,它通过了 while 条件,但垃圾数据指向导致段错误的非法内存访问。如果不逐步执行该程序,我真的很难说出来。

标签: c


【解决方案1】:

您没有将内存分配给结构中的ptr。

first = first -> ptr; // In this if the ptr is null then you are accessing the next iteration first->val you are getting the segmentation fault.

之后,在使用sprintf 时,您必须使用常规字符串。

sprintf(dest,"%s",first->val);

你可以在访问前检查一下 first->val 可能喜欢

if ( first != NULL )

【讨论】:

  • ,现在程序执行没有分段错误。但是 devicediscovery() 函数中存在内存泄漏。我该如何解决这个问题,抱歉再次询问。提前谢谢。
  • 在那个函数中,我认为你没有释放 temp 和 head 变量。在其他情况下,first=temp=head。在此您可以释放存储头指针的临时文件。
猜你喜欢
  • 1970-01-01
  • 2013-11-30
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多