1

我编写了这个 c 程序来了解结构和指针。

问题

该程序至少在接收输入时工作正常(通过 gdb 测试确认),不正确的是,在打印输出数据时,它似乎被某些东西覆盖了......

问题

如果编译器为某个数组保留内存,是否可以假设在函数“死亡”后,保留结束,并将内存分配给其他进程,是否正确?因此这将解释为什么结构数据被覆盖?

编码

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

    typedef struct {int marks;int roll;} ADDRESS ;

    void printer(ADDRESS* pointer,int len)
         {          printf("-- Entering printing mode -- \n");
                ADDRESS* p1;
                p1=pointer;
                int counter=0;
                while ( counter < len)
                {
                   printf(" --> Entry %d  \n",counter);
                   printf("Marks : %d \n",p1->marks);
                   printf("Roll : %d \n ",p1->roll);
                   counter++;p1++;
                }
         }

     ADDRESS *stater(int number)
           {    
                   printf("\t STANDBY FOR INPUT PART \t \n\n\n");
               ADDRESS x[number];
               ADDRESS *c,*i;
               c=x;
               i=c;
               int counterf=number;
               int counter=0;
               while (counter!=counterf)
                   {
                        printf("\t Input the mark : ");
                        scanf("%d",&(c->marks));
                        printf("\t Input the roll : ");
                        scanf("%d",&(c->roll));
                        printf("\n");
                        counter++;
                        c=c+1;
                   }
               return i;
              }



            int main()
                 {
                         ADDRESS *o=stater(4);
                         printer(o,4);
                         return 0;

                  }
4

2 回答 2

3

在内存被释放后,它通常会被重新分配。当内存存储在堆栈上时尤其如此。所以,是的。

于 2013-04-10T22:33:34.920 回答
1

这对我来说很有效:

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

typedef struct {int marks;int roll;} ADDRESS ;

void printer(ADDRESS* pointer,int len)
     {          printf("-- Entering printing mode -- \n");
            ADDRESS* p1;
            p1=pointer;
            int counter=0;
            while ( counter < len)
            {
               printf(" --> Entry %d  \n",counter);
               printf("Marks : %d \n",p1->marks);
               printf("Roll : %d \n ",p1->roll);
               counter++;p1++;
            }
     }

ADDRESS *stater(int number)
       {    
           printf("\t STANDBY FOR INPUT PART \t \n\n\n");
           ADDRESS* x = (ADDRESS *)malloc(number*sizeof(ADDRESS));
           ADDRESS *c,*i;
           c=x;
           i=c;
           int counterf=number;
           int counter=0;
           while (counter!=counterf)
               {
                    printf("\t Input the mark : ");
                    scanf("%d",&(c->marks));
                    printf("\t Input the roll : ");
                    scanf("%d",&(c->roll));
                    printf("\n");
                    counter++;
                    c=c+1;
               }
           return i;
          }



        int main()
             {
                     ADDRESS *o=stater(4);
                     printer(o,4);
                     return 0;

              }
于 2013-04-10T23:04:49.737 回答