我在以下程序中遇到分段错误 我写的代码是:
#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;
}
在调试这个程序时,我在主函数里面的sprintf(dest,first-&gt; val)stement中得到了一个分段错误。 我想我已经为变量分配了内存,那么为什么我会收到这个错误?任何人都可以帮助我。
答案 0 :(得分:0)
您没有将内存分配给结构中的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-&gt; val之前检查一下
if ( first != NULL )