2010-12-02 98 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; 
相关问题