gpt4 book ai didi

c - 执行c程序时出现段错误

转载 作者:行者123 更新时间:2023-11-30 17:22:09 26 4
gpt4 key购买 nike

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

#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) 部分出现段错误。我认为我已经为变量分配了内存,那么为什么我会收到此错误?谁能帮帮我吗?

最佳答案

您没有将内存分配给结构中的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 )

关于c - 执行c程序时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com