2010-12-02 97 views
1

該程序的任務是使用memcpy將結構中的所有數據推送到堆棧中。 執行後,它成功地將數據輸入到結構中,但在涉及到push()函數時卻達到了分段錯誤。將結構中的數據推送到堆棧中C

下面的代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <malloc.h> 
#include <mem.h> 

typedef struct STD { 
    char ime [50]; 
    int fn; 
    float usp; 
    } STD; 


typedef struct STACK { 
    STD *s; 
    STACK *next; 

    } STACK; 
    int push (void *a, int siz, STACK **sst) { 
STACK *snew; 
snew = (STACK *) malloc (siz + 1); 
memcpy (snew->s, a, siz); 
snew -> next = *sst; 
*sst = snew; 


} 

int main() { 
STACK *st; 
STD ss; 

printf ("Vyvedi ime"); 
gets (ss.ime); 
ss.ime[49] = 0; 
printf ("Vyvedi fn"); 
scanf ("%d", &ss.fn); 

printf ("Vyvedi usp"); 
scanf ("%f", &ss.usp); 



push (&ss, sizeof(ss) , &st); 



system ("pause");  } 

不知道它的問題,我用DEVC作爲一個編譯器。

+3

而且我希望您會問的問題隨時到來...... – abelenky 2010-12-02 18:59:03

+0

@abelenky - 這是很明顯,分割故障問題 – 2010-12-02 19:08:56

+1

@Steve湯森:這麼大多數普通用戶已經變得很清楚他們厭倦了,「這是我的代碼轉儲,請求解決它」類型的問題。我們都希望從海報中看到:你期望什麼?你究竟得到了什麼?你有什麼嘗試,你卡在哪裏?當人們提出真正的問題時,提供真正的答案會更容易。 – abelenky 2010-12-02 19:20:30

回答

1

這段代碼是錯誤的:

STACK *snew; 
snew = (STACK *) malloc (siz + 1); 
memcpy (snew->s, a, siz); 

snew->s未初始化的時候,你memcpy a進去。我期望看到兩個malloc s - 一個用於STACK*,另一個用於STD*,然後您可以在將東西複製到其中之前使用它來播種snew->s

STACK *snew; 
snew = (STACK *) malloc (sizeof(STACK)); 
snew->s = (STD*) malloc(sizeof(STD)); 
memcpy (snew->s, a, siz); 

或者,您也可以使用單個malloc,並指向snew->s到合適的範圍內它的偏移量(你已經離開了空間,爲STACK struct後)。

STACK *snew; 
snew = (STACK *) malloc (sizeof(STACK) + siz + 1); 
snew->s = (char*)snew + sizeof(STACK); 
memcpy (snew->s, a, siz); 

push功能siz參數似乎是多餘的,因爲你總是傳遞一個struct STD

1
  1. 注意您沒有爲s
  2. 分配空間,你需要初始化stNULL
  3. 請檢查snewNULL

int push (void *a, int siz, STACK **sst) { 
    STACK *snew (STACK *) malloc (siz + 1); 
    snew->s = (STD *) mallos (sizeof(STD)); // <----------- 
    memcpy (snew->s, a, siz); 
    snew -> next = *sst; 
    *sst = snew; 
} 

看起來還有其他問題UE的存在,開始使用有意義的名稱,而不是ssst ..

1

這是你要做的,Dalamar:
1.如果你有一個調試器,並且你知道如何使用它,那麼通過push()函數來查看出現分段錯誤的位置。
2.否則,推()把一個printf語句,每行之間:

printf ("1\n") ; 
... 
printf ("2\n") ; 
... 

這也將告訴你在哪裏分割故障發生。
如果你仍然陷入困境,那麼請回到我們這裏。

0
#include <iostream> 
#include <stdlib.h> 
#include <conio.h> 

#include "linkedlist.h" 
int main(int argc, char *argv[]) 
{ 
LinkedList myList; 
Node *node ; 
int choice = 0 , index=0 ; 
string agentName, caseDesc ; 
int caseNo; 
do 
{ 

    cout<< "\n Enter 1 Add a new node to the end \n"; 
    cout<< " Enter 2 Add a new node to the beginning \n"; 
    cout<< " Enter 3 Print out the entire list \n"; 
    cout<< " Enter 4 Remove a node from the list \n"; 
    cout<< " Enter 5 Quit the program \n"; 
    cout<< " Enter your choice : "; 
    cin>> choice; 
    switch(choice){ 
     case 1: 
        // Insert appropriate code here .... 
     break; 

     case 2: 
        // Insert appropriate code here .... 
     break; 

     case 3: 
        // Insert appropriate code here .... 
     break; 

     case 4: 
        // Insert appropriate code here ....   
     break; 

     case 5: 
     exit(1); 
     break;  

     default : 
     cout<<"\n Invalid Option, Please try again .....\n"; 
     break; 

    } 

}while (true); 




system("PAUSE"); 
return 0; 
相關問題