创建列表时遇到问题

时间:2019-01-22 10:54:56

标签: c list

我创建了一个从文件中读取数据的列表,并为文件的每一行创建了列表的新元素。

实际上,它在Prod_ptr处阻塞corrente_ptr = * lptr;由于某些我无法理解的原因

程序的主体,直到创建列表的循环为止:

//this one works well
    void playVideo::on_zoomInBtn_clicked()
    {
       ui->zoomOutBtn->setEnabled(true);
       statusBar()->showMessage("zoomin*2",1000);
       Mat scaleFrame;
       cv::resize(this->frame,scaleFrame,Size(0,0),2, 2,CV_INTER_LINEAR);
       QImage img=QImage(scaleFrame.data,scaleFrame.cols,scaleFrame.rows,QImage::Format_RGB888).rgbSwapped();
       ui->videoPlayArea->setPixmap(QPixmap::fromImage(img));
    }


//this one works bad
    void playVideo::on_zoomOutBtn_clicked()
    {
        statusBar()->showMessage("zoomout",1000);
        Mat scaleFrame;
        ui->videoPlayArea->setFixedSize(QSize(701,371));
        cv::resize(this->frame,scaleFrame,Size(701,371),CV_INTER_AREA);
        imshow("name",scaleFrame);
        QImage img=QImage(scaleFrame.data,scaleFrame.cols,scaleFrame.rows,QImage::Format_RGB888).rgbSwapped();
        img.scaled(ui->videoPlayArea->width(),ui->videoPlayArea->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
           ui->videoPlayArea->setPixmap(QPixmap::fromImage(img));
    }

创建列表的函数:

int main (){
Prod_ptr *lptr = NULL ;
int count, i =  0 ;
char c;
short opzione;
    FILE *file_ptr;
void (*f[DIM])(Prod_ptr *lptr ) ={cambia_quantita , insert_prod, delete_prod ,  cambia_prezzo , cerca_prodotto , stampa_prod};
    file_ptr= fopen( "pischelletto.csv" , "r");
if(file_ptr==NULL) {
    printf("error program name");
    return 1;
}

for (c = getc(file_ptr); c != EOF; c = getc(file_ptr))
    if (c == '\n') // Increment count if this character is newline 
        count = count + 1; 

rewind(file_ptr);
for ( i =  0 ;  i < count ; i++ ){
    create_list_prod ( lptr , file_ptr);
}

}

1 个答案:

答案 0 :(得分:4)

主要

Prod_ptr *lptr = NULL ;

.. lptr not changed

for ( i =  0 ;  i < count ; i++ ){
  create_list_prod ( lptr , file_ptr);
} 

因此,您使用 lptr == NULL调用create_list_prod,而当您执行Prod_ptr corrente_ptr = *lptr; lptr 仍为NULL,因此您在地址0 =>崩溃

对我来说,该行必须为Prod_ptr corrente_ptr = lptr;


您在该行中也有问题

while( corrente_ptr != NULL || corrente_ptr->product.codice < nuovo_prod_ptr->product.codice)

因为如果corrente_ptr为NULL,则您进行corrente_ptr->product.codice等。

必须为

while( corrente_ptr != NULL && corrente_ptr->product.codice < nuovo_prod_ptr->product.codice)

请注意,行nuovo_prod_ptr->product.prezzo;不执行任何操作


在以前的版本中,我说过:

  

在线

 nuovo_prod_ptr->prossimo_prod_ptr = *lptr;
 *lptr =  nuovo_prod_ptr;
     

必须

 nuovo_prod_ptr->prossimo_prod_ptr = lptr;
 lptr =  nuovo_prod_ptr;

但是我错了


您没有给出Prod_Ptr等的定义,因此我从您的代码中推断出它们。

我在您的代码中添加了注释,以说明为使其正常运行而进行的其他更改。

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

/* 
   extrapolated definitions
*/

typedef struct Product {
  char nome_prod[20];
  int codice;
  int prezzo;
  int quantita_magazzino;
} Product;

typedef struct Lista_prodotti {
  struct Lista_prodotti * prossimo_prod_ptr;
  Product product;
}Lista_prodotti;

typedef Lista_prodotti * Prod_ptr;

/* */

void create_list_prod ( Prod_ptr *lptr, FILE *file_ptr) {
  int code, price;
  char nome[20];

  if (fscanf(file_ptr , "%d;%19[^;];%d;", &code , nome , &price) != 3) {
    puts("invalid values in the file, bypassed");
  }
  else {
    Prod_ptr nuovo_prod_ptr = malloc(sizeof(Lista_prodotti));

    if (nuovo_prod_ptr != NULL) {
      nuovo_prod_ptr->product.codice = code;
      strcpy(nuovo_prod_ptr->product.nome_prod , nome);
      nuovo_prod_ptr->product.prezzo = price;  /* I suppose */
      nuovo_prod_ptr->product.quantita_magazzino = rand() % (100001); 
      nuovo_prod_ptr->prossimo_prod_ptr= NULL;

      if (*lptr == NULL) {
        /* this is the first added cell of the list */
        *lptr = nuovo_prod_ptr;
      }
      else {
        Prod_ptr precedente_ptr = NULL;
        Prod_ptr corrente_ptr = *lptr;

        while( corrente_ptr != NULL && corrente_ptr->product.codice < nuovo_prod_ptr->product.codice){
          precedente_ptr = corrente_ptr;
          corrente_ptr = corrente_ptr->prossimo_prod_ptr;
        }
        if( precedente_ptr== NULL){
          nuovo_prod_ptr->prossimo_prod_ptr = *lptr;
          *lptr =  nuovo_prod_ptr;
        }
        else{
          precedente_ptr->prossimo_prod_ptr= nuovo_prod_ptr;
          nuovo_prod_ptr->prossimo_prod_ptr = corrente_ptr;
        }
      }
    }
    else{
      puts("Memoria esaurita");
    }
  }
}

int main (){
  Prod_ptr lptr = NULL; /* not "Prod_ptr *" */
  int count = 0, i; /* count must be initialized, not i */
  int c; /* must be an int rather than a char to be able to memorize EOF whatever it is */
  /* put in comment because not used : short opzione; */
  FILE *file_ptr;
  /* put in comment because not used : void (*f[DIM])(Prod_ptr *lptr ) ={cambia_quantita , insert_prod, delete_prod ,  cambia_prezzo , cerca_prodotto , stampa_prod}; */
  file_ptr= fopen( "pischelletto.csv" , "r");

  if(file_ptr==NULL) {
    printf("error program name");
    return 1;
  }

  for (c = getc(file_ptr); c != EOF; c = getc(file_ptr))
    if (c == '\n') // Increment count if this character is newline 
      count = count + 1; 

  rewind(file_ptr);

  for ( i =  0 ;  i < count ; i++ ){
    create_list_prod ( &lptr , file_ptr); /* give the address of lptr */
  }

  fclose(file_ptr); /* added */

  /* print the list to check, also free memory. 
     You may need to adapt if my extrapolated definitions are wrong  */
  while (lptr != NULL) {
    printf("%s : %d %d %d\n", 
           lptr->product.nome_prod,
           lptr->product.codice,
           lptr->product.prezzo,
           lptr->product.quantita_magazzino);
    Prod_ptr next = lptr->prossimo_prod_ptr;
    free(lptr);
    lptr = next;
  }

  return 0;
}

如果我将这些内容放在pischelletto.csv

12;aze;3;
34;qsd;2;
6;foo;123;

执行产生:

foo : 6 123 75961
aze : 12 3 71341
qsd : 34 2 22417

valgrind 下执行以检查内存访问/内存泄漏:

valgrind --leak-check=full ./a.out
==22125== Memcheck, a memory error detector
==22125== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==22125== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==22125== Command: ./a.out
==22125== 
foo : 6 123 75961
aze : 12 3 71341
qsd : 34 2 22417
==22125== 
==22125== HEAP SUMMARY:
==22125==     in use at exit: 0 bytes in 0 blocks
==22125==   total heap usage: 4 allocs, 4 frees, 688 bytes allocated
==22125== 
==22125== All heap blocks were freed -- no leaks are possible
==22125== 
==22125== For counts of detected and suppressed errors, rerun with: -v
==22125== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)