我正在用C创建一个优先级队列结构。
结构:
void Initialize(PriorityQueue *PQ)
{
PQ->Count=0;
}
初始化函数:
int main (void) {
PriorityQueue* PQ;
Initialize(PQ);
Insert(2, PQ);
Insert(3, PQ);
Insert(5, PQ);
Insert(1, PQ);
Insert(12, PQ);
Insert(6, PQ);
Insert(10, PQ);
printPQ(PQ);
return 0;
}
主要:
int
问题是,当我运行它时,我遇到了分段错误。使用gdb,我发现它被抛出了初始化函数。令我感到困惑的是,Public Function SendEmailViaGmail(SendTo As String, Optional Subject As String = "", Optional TextBody As String = "", Optional ReplyTo As String = "", Optional AttachedFiles As Variant = "") As String
On Error GoTo send_emailErr
Dim ErrNum As Long
Dim ErrDes As String
SendEmailViaGmail = ""
ErrNum = 0
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = sendusername '
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = sendpassword
.Update
End With
' build email parts
With cdomsg
.To = SendTo
.FROM = sendusername
.Subject = Subject
.TextBody = TextBody & vbCrLf & vbCrLf & vbCrLf & "--" & vbCrLf & "Sent using Marlan Data-Systems"
If IsArray(AttachedFiles) Then
For Each AttachedFile In AttachedFiles
If Len(AttachedFile) > 3 Then .AddAttachment AttachedFile
Next
Else
If Len(AttachedFiles) > 3 Then .AddAttachment AttachedFiles
End If
.send
End With
SendEmailViaGmail = "Done!"
send_emailExit:
Set cdomsg = Nothing
Exit Function
send_emailErr:
ErrNum = Err.Number
ErrDes = Err.Description
Select Case Err.Number
Case -2147220977 'Likely cause, Incorrectly Formatted Email Address, server rejected the Email Format
SendEmailViaGmail = "Please Format the Email Address Correctly."
Case -2147220980 'Likely cause, No Recipient Provided (No Email Address)
SendEmailViaGmail = "Please Provide an Email Address"
Case -2147220960 'Likely cause, SendUsing Configuration Error
SendEmailViaGmail = "SendUsing Configuration Error"
Case -2147220973 'Likely cause, No Internet Connection
SendEmailViaGmail = "Please Check Internet Connection"
Case -2147220975 'Likely cause, Incorrect Password
SendEmailViaGmail = "Please Check Password"
Case Else 'Report Other Errors
SendEmailViaGmail = ""
End Select
SendEmailViaGmail = SendEmailViaGmail & " Error number: " & Err.Number & " Description: " & Err.Description
'If ErrNum = -2147220975 Then
' cdomsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
' Resume
'End If
Resume send_emailExit
End Function
的简单赋值如何引发分段错误。
答案 0 :(得分:2)
令我感到困惑的是
int
的简单分配如何引发分段错误。
那么,看看之前!!!
在Initialize()
功能中,当您尝试访问PQ
时,未分配有效内存。在解除反射之前,您需要为PQ
分配适当的内存。否则,尝试访问无效内存会调用undefined behavior。
答案 1 :(得分:0)
声明:
PriorityQueue* PQ;
声明一个变量(它的类型和名称,以及它在内存中的间接位置),但不初始化它。所以当你用
传递值时Initialize(PQ);
被调用的函数使用随机值作为内存的地址 - 赋值
PQ->Count=0;
将零置于任意内存位置,很高兴您进入无效/禁止位置。
答案 2 :(得分:0)
改为编写PriorityQueue PQ;
(即删除指针类型),并在&PQ
中的每个其他实例中使用main
。在这种情况下,使用自动变量作为优先级队列就足够了。
否则你需要分配内存(使用malloc
& c。),将该内存的位置分配给PQ
,并记得呼叫{{1}当你完成时。
答案 3 :(得分:0)
PriorityQueue变量PQ没有为其分配任何内存空间,分段错误的原因是您试图将变量存储在未分配的空间(或您无权访问的内存位置) 。要获取变量PQ的内存空间,请使用malloc()函数。
PQ = (PriorityQueue*) malloc(sizeof(PriorityQueue));