gpt4 book ai didi

C: 当我尝试将我的结构传递到队列中时的核心转储

转载 作者:太空宇宙 更新时间:2023-11-04 01:59:10 25 4
gpt4 key购买 nike

编辑: 创建它并将其存储到临时节点中确实可行...但我没有为新节点动态分配空间...

struct node *tempNode = malloc(sizeof(struct node));

代替

struct node *tempNode;

所以我正在创建一个队列来存储一组分配的“连接”。

我很好地创建了连接(它是一个结构),将它传递给入队函数,它检查它是否为空(也就是队列中的第一个指针为 NULL)如果是,那么它假设将连接存储到第一个指针节点(这是一个结构)。

但是,当它尝试将 incomingConnection 放入节点的 connectionData 时,它会进行核心转储。 (如果您正在跟踪我的 printfs,它会进入队列中的第 1 部分并停止。)。所以这让我觉得我的 enqueueConnection 函数的第一部分有问题。

我尝试在节点中使连接成为一个空指针,将连接存储到一个临时节点中,然后将临时节点存储到队列的第一个节点指针中……但都没有用。

这是核心转储所在的函数......

int enqueueConnection(Queue *connectionQueue, Connection *incomingConnection)
{
/*
If the connection is empty
*/
printf("CHECKING IF EMPTY\n");
if(connectionQueue->first==NULL)
{
printf("PART 1 \n");
connectionQueue->first->connectionData = incomingConnection; //FAILS HERE
printf("PART 2\n");
connectionQueue->first->next = connectionQueue->last; //which is NULL but
printf("PART 3\n");
connectionQueue->first->previous = NULL;
printf("RETURNING\n");
return NULL;
}
....

这是我的其余代码

队列.c

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

typedef struct connections
{
/*
IP Address can be stored in two formats, IPv4 or IPv6.
IPv4 is 15 chars long and of the format ###.###.###.### in regular digits
IPv6 is 25 chars long and of the format ####:####:####::####:### in hex
*/
char* ipAddress[26];
int port; //port value from 0-65535
char* protocolType[2];
union
{
char* connectionName[6]; //UDP only, 5 letter name to the connection
int hops; //TCP only, 0-255 declaring the number of hops for the connection.
}protocol;
} Connection;

struct node
{
Connection *connectionData;
struct node *next;
struct node *previous;
};

typedef struct queue
{
struct node *first;
struct node *last;
}Queue;

/*
Sets up new node in a temporary node, and then links the new node
with the previous last node, inserts connection data, declares next
node as null as this node will be the last. Then declares the last
node in the queue as the new node, as by defintion of a queue.
Also checks to make sure the queue does not have only 1 item in it or
if the queue is empty.
*/
int enqueueConnection(Queue *connectionQueue, Connection *incomingConnection)
{
/*
If the connection is empty
*/
printf("CHECKING IF EMPTY\n");
if(connectionQueue->first==NULL)
{
printf("PART 1 \n");
connectionQueue->first->connectionData = incomingConnection;
printf("PART 2\n");
connectionQueue->first->next = connectionQueue->last; //which is NULL but
printf("PART 3\n");
connectionQueue->first->previous = NULL;
printf("RETURNING\n");
return NULL;
}

/*
If the connection only has 1 item in it
*/
if(connectionQueue->last==NULL)
{
connectionQueue->last->previous = connectionQueue->first;
connectionQueue->last->connectionData = incomingConnection;
connectionQueue->last->next = NULL;
return NULL;
}

/*
Sets up new node in a temporary node, and then links the new node
with the previous last node, inserts connection data, declares next
node as null as this node will be the last. Then declares the last
node in the queue as the new node, as by defintion of a queue
*/
struct node *tmp;

tmp->previous = connectionQueue->last;
tmp->connectionData=incomingConnection;
tmp->next=NULL;
connectionQueue->last=tmp;
return NULL;
}

char* dequeueConnection(Queue *connectionQueue, Connection *incomingConnection)
{
if(connectionQueue->first==NULL)
{
return "Error: Connection queue empty";
}

struct node *tmp, *tmp2, *tmp3;
if(connectionQueue->last == NULL)
{
tmp = connectionQueue->first;
connectionQueue->first = NULL;
free(tmp);
return NULL;
}

tmp = connectionQueue->first->next;
while(tmp->connectionData != incomingConnection)
{
if(tmp == NULL)
{
return "Error: Connection not found";
}

tmp2 = tmp->next;
tmp = tmp2;
}

tmp2 = tmp->next;
tmp3 = tmp->previous;
free(tmp);
tmp2->previous = tmp3->next;
return NULL;
}

int emptyQueue(Queue *connectionQueue)
{
if(connectionQueue->first == NULL)
{
if(connectionQueue->last == NULL)
{
free(connectionQueue);
return NULL;
}
}

struct node *tmp;
while(connectionQueue->first != NULL)
{
tmp = connectionQueue->first;
connectionQueue->first = tmp->next;
connectionQueue->first->previous = NULL;
tmp->previous=NULL;
tmp->next=NULL;
free(tmp);
}

connectionQueue->last=NULL;
return NULL;
}

Queue *createConnectionQueue()
{
Queue *connectionQueue = malloc(sizeof(Queue));
connectionQueue->first = NULL;
connectionQueue->last = NULL;

return connectionQueue;
}

networkManager2.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"

Connection *processConnection()
{
/*
Where the information for the connection is input.
Connection is stored and created on the heap
*/
Connection *currentConnection = malloc(sizeof(Connection));
printf("IP Address: ");
gets(currentConnection->ipAddress);
printf("Protocol Type: ");
gets(currentConnection->protocolType);
printf("Port: ");
scanf(" %d", &currentConnection->port);

/*
Depending on the protocol type, it stores hops or connection name.
*/
if(strcmp(currentConnection->protocolType, "TCP") == 0)
{
printf("Hops: ");
scanf(" %d", &currentConnection->protocol.hops);
}
else if(strcmp(currentConnection->protocolType, "UDP") == 0)
{
printf("Connection Name: ");
scanf(" %s",&currentConnection->protocol.connectionName);
}

return currentConnection;
}

void storeConnection(Queue *connectionQueue, Connection *incomingConnection)
{
printf("IN STORE CONNECTION\n");
enqueueConnection(connectionQueue,incomingConnection);
printf("%s and %s",connectionQueue->first->connectionData->ipAddress,connectionQueue->first->connectionData->ipAddress);
}

int main()
{
int running = 0; //variable to check if the user wants to input more connections
char* holder[1];

printf("STARTED\n");
Queue *connectionQueue = createConnectionQueue();
printf("QUEUE CREATED\n");

/*
Inputs connections and stores them in an array of connections. The array can
only hold up to 5 connections and replaces the oldest each time if its full.
*/
do
{
Connection *incomingConnection = processConnection();
printf("CONNECTION MADE\n");
storeConnection(connectionQueue,incomingConnection);
printf("Continue?: ");
scanf(" %d",&running);
gets(holder); //avoid the scanf problem.
}while(running == 0);
emptyQueue(connectionQueue);
printf("Deleted");
}

最佳答案

你所有的问题都来自你的结构属性类型:

char* ipAddress[26];

在这里,我假设您想创建一个 26 字节长的字符数组。但是你声明了一个 26 指针的 long char char 数组。数组应如下所示:

char* ipAddress;

如果您想动态分配它(例如使用 malloc)或:

char ipAddress[26];

如果你想在声明时在堆栈中有一个预先分配的大小。我想这就是您想要的情况。

为了获取它,传递数组的指针:

gets(&currentConnection->ipAddress);

关于C: 当我尝试将我的结构传递到队列中时的核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29584204/

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