[RISOLTO]Errore not memory available

di Anonimizzato20410 il
10 risposte
Salve a tutti avrei un problema con il caricamento di un immagine in un buffer il codice non presenta nessun errore ma nella finestra del terminale mi scrive not memory available not enough space cosa sto sbagliando???

10 Risposte

  • Probabilmente non hai allocato spazio a sufficienza
  • Quindi in questo caso??come potrei correggerlo?
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <string.h>
    
    typedef struct
        {
            unsigned char RGB[3];
        }RGB;
    
    typedef struct
        {
            unsigned int size;
            int width,height;
            unsigned short int planes;
            unsigned short int bpp;
            unsigned int compression;
            unsigned int imagesize;
            int xresolution,yresolution;
            unsigned int colours;
            unsigned int impcolours;
        }INFOHEADER;
    
    // ********** Create Matrix **********
    RGB** createMatrix(int height,int width){
        RGB** Matrix;
        int i;
        Matrix = (RGB **) malloc (sizeof (RGB*) * height);
        if (Matrix == NULL){
            perror("***** No memory available*****");
            exit(0);
        }
        for (i=0;i<height;i++){
            Matrix[i] = (RGB *) malloc (sizeof(RGB) * width);
            if (Matrix[i] == NULL){
            perror("***** No memory available *****");
                exit(0);
            }
        }
        return(Matrix);
    }
    
    // ********** Verify if the file is BMP *********
    void isBMP(FILE* arq){
        char type[3];
        unsigned short int bpp;
        fseek(arq,0,0);
        fread(type,1,2,arq);
        type[2] = '\0';
    
        fseek(arq,28,0);
        fread(&bpp,1,2,arq);
    
        if (strcmp(type,"BM") || (bpp != 24)){
            printf("\nThe file is not BMP format or is not 24 bits\n");
                exit(0);
        }
    }
    
    // ********** Read BMP info from file **********
    INFOHEADER readInfo(FILE* arq){
        INFOHEADER info;
    
        // Image Width in pixels
        fseek(arq,18,0);
        fread(&info.width,1,4,arq);
    
        // Image Height in pixels
        fseek(arq,22,0);
        fread(&info.height,1,4,arq);
    
        // Color depth, BPP (bits per pixel)
        fseek(arq,28,0);
        fread(&info.bpp,1,2,arq);
    
        // Compression type
        // 0 = Normmally
        // 1 = 8 bits per pixel
        // 2 = 4 bits per pixel
        fseek(arq,30,0);
        fread(&info.compression,1,4,arq);
    
        // Image size in bytes
        fseek(arq,34,0);
        fread(&info.imagesize,1,4,arq);
    
        // Number of color used (NCL)
        // value = 0 for full color set
        fseek(arq,46,0);
        fread(&info.colours,1,4,arq);
    
        // Number of important color (NIC)
        // value = 0 means all collors important
        fseek(arq,50,0);
        fread(&info.impcolours,1,4,arq);
    
        return(info);
    }
    
    RGB** loadImage(FILE* arq, RGB** Matrix,int height,int width){
        int i,j;
        RGB tmp;
        long pos = 51;
    
        fseek(arq,0,0);
    
        for (i=0; i<height; i++){
            for (j=0; j<width; j++){
                pos+= 3;
                fseek(arq,pos,0);
                fread(&tmp,(sizeof(RGB)),1,arq);
                Matrix[i][j] = tmp;
            }
        }
        return(Matrix);
    }
    
    int main(void)
    {
     int height, width;
    FILE* arq = fopen("lena512.bmp", "r");
     /* in your main program you just call */
     /* the bitmap file 24 bits */
    RGB** Matrix_aux;
    RGB** Matrix;
    INFOHEADER info;
    info = readInfo(arq);
    height = info.height;
    width = info.width;
    
    Matrix_aux = createMatrix(height,width);
    Matrix = loadImage(arq,Matrix_aux,height,width);
    
    int i , j;
    for(i=0;i<height;i++){
        for(j=0;j<width;j++){
            printf("\n %d,%d,%d",Matrix[i][j].RGB[0],Matrix[i][j].RGB[1],Matrix[i][j].RGB[2]);
        }
    }
    
    printf("\n tool=%d l3ard=%d",height,width);
    
    return 0;
    }
    
    
  • Ma in quale riga ti capita? Esegui il programma passo passo.

    Quanto valgono le variabili

    height width

    ?
  • Vorrei che non entrasse in questo ciclo o meglio che non stampi no memory available
    // ********** Create Matrix **********
    RGB** createMatrix(int height,int width){
        RGB** Matrix;
        int i;
        Matrix = (RGB **) malloc (sizeof (RGB*) * height);
        if (Matrix == NULL){
            perror("***** No memory available*****");
            exit(0);
        }
        for (i=0;i<height;i++){
            Matrix[i] = (RGB *) malloc (sizeof(RGB) * width);
            if (Matrix[i] == NULL){
            perror("***** No memory available *****");
                exit(0);
            }
        }
        return(Matrix);
    }
  • RAGIONA: tu stai allocando spazio per un'immagine!

    Ora, la quantita' di memoria necessaria DIPENDE, fondamentalmente, dal numero di RIGHE e COLONNE dell'immagine e DALLA MEMORIA che hai correntemente disponibile.

    Quindi, PROVA: se allochi spazio per un'immagine 10x10 funziona? E 100x100? E 1000x1000?

    Se ad un certo punto non ci riesci piu', vuol dire che vuoi gestire un'immagine TROPPO GRANDE!

    Nota: PER FORTUNA che ti stampa "*** No memory available ***" perche' altrimenti avresti un errore DECISAMENTE PIU' astruso!!!!!!
  • migliorabile ha scritto:


    RAGIONA: tu stai allocando spazio per un'immagine!

    Ora, la quantita' di memoria necessaria DIPENDE, fondamentalmente, dal numero di RIGHE e COLONNE dell'immagine e DALLA MEMORIA che hai correntemente disponibile.

    Quindi, PROVA: se allochi spazio per un'immagine 10x10 funziona? E 100x100? E 1000x1000?

    Se ad un certo punto non ci riesci piu', vuol dire che vuoi gestire un'immagine TROPPO GRANDE!

    Nota: PER FORTUNA che ti stampa "*** No memory available ***" perche' altrimenti avresti un errore DECISAMENTE PIU' astruso!!!!!!
    risolto grazie mille
  • Ti avevo fatto una domanda ... quanto valgono quelle variabili quando hai l'errore?

    Se provi a controllare e a rispondere, si può arrivare ad una soluzione, altrimenti ...
  • oregon ha scritto:


    Ti avevo fatto una domanda ... quanto valgono quelle variabili quando hai l'errore?

    Se provi a controllare e a rispondere, si può arrivare ad una soluzione, altrimenti ...
    sono riuscito riducendo leggermente..sei stato molto utile anche se non capisco come nel pc del mio collega con la stessa immagine compili..
    Un altra domanda se adesso volessi dividere l'immagine in blocchi 8x8 come posso fare?' doppio ciclo for??si accettano suggerimenti
  • Questo non è un errore di compilazione ma di esecuzione. Quindi nel pc del tuo collega il programma viene eseguito.

    E' probabile che il tuo abbia, in quel momento, meno memoria a disposizione perché stanno girando più programmi.

    Per la suddivisione devi operare con due cicli opportuni che puntino agli elementi che ti servono man mano.
Devi accedere o registrarti per scrivere nel forum
10 risposte