how do I dynamicly malloc memory ?

This is a discussion on how do I dynamicly malloc memory ? within the C Programming forums, part of the General Programming Boards category; Input file as follws: Code: CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCGCCGCCC GGGGGGGGGGGGGAGGG?GGGG?GGGG?G?GGGAGG?A??GGGGGA GGGGGGGGGGGGGGGGG?GGGG?GGGG?G?GGGGGG?G??GGAGGG AAAAAAAAAAAAAAAACAAAAACAAAACCCAAAAAAAAAAAA?AAA CCCCCCCCCCCCCCCCACCCCCACCCCAAACCCCCCCCCCCCCCCC CACAACACAAAAACCCCCCACC?ACCACCCCACCCCCCCCACCACC GAGAAGAGAAAAAGGGG?GAGG?AGGAGGGGAGGGGGGGGAGGAGG GGAGGGGAGGGGGGAGGAAGGGGGGGGGGGGGGGGGGGGGGGGGGG CCTCCCCTCCCCCCTCCTTCCCCCCCCCCCCCCCCCCCCCCCCCCC GGAGGGGAGGGGGGAGG?AGGGGGGGGGGGGGGGGGGGG?GG?GGG AACAAAACAAAAAACAACCAAAAAAAAA?ACAAA?AAAACAACACA ... I want to ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    how do I dynamicly malloc memory ?

    Input file as follws:
    Code:
    CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCGCCGCCC
    GGGGGGGGGGGGGAGGG?GGGG?GGGG?G?GGGAGG?A??GGGGGA
    GGGGGGGGGGGGGGGGG?GGGG?GGGG?G?GGGGGG?G??GGAGGG
    AAAAAAAAAAAAAAAACAAAAACAAAACCCAAAAAAAAAAAA?AAA
    CCCCCCCCCCCCCCCCACCCCCACCCCAAACCCCCCCCCCCCCCCC
    CACAACACAAAAACCCCCCACC?ACCACCCCACCCCCCCCACCACC
    GAGAAGAGAAAAAGGGG?GAGG?AGGAGGGGAGGGGGGGGAGGAGG
    GGAGGGGAGGGGGGAGGAAGGGGGGGGGGGGGGGGGGGGGGGGGGG
    CCTCCCCTCCCCCCTCCTTCCCCCCCCCCCCCCCCCCCCCCCCCCC
    GGAGGGGAGGGGGGAGG?AGGGGGGGGGGGGGGGGGGGG?GG?GGG
    AACAAAACAAAAAACAACCAAAAAAAAA?ACAAA?AAAACAACACA
    ...
    I want to load this file into char **inputfile;
    how to malloc memory dynamicly?

    My codes below can't malloc memory dynamicly
    Code:
    char **input = cmatrix(0,40,0,46);
    char    **cmatrix(int nrl,int nrh,int ncl,int nch)
    {
             int i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
             char **cm;
    
             /* allocate pointers to rows        */
             if((cm=(char **)calloc(nrow,sizeof(char*)))==NULL)
             {
                printf("error cmatrix: calloc error No. 1 \n");
                return(NULL);
             }
             cm+=0;
             cm-=nrl;
    
             /* allocate rows and set pointers to them        */
             if((cm[nrl]=(char *)calloc(nrow*ncol,sizeof(char)))==NULL)
             {
                printf("error cmatrix: calloc error No. 2 \n");
                return(NULL);
             }
             cm[nrl]+=0;
             cm[nrl]-=nrl;
    
             for(i=nrl+1;i<=nrh;i++)
                cm[i]=cm[i-1]+ncol;
             /* return pointer to array of pointers to rows   */
             return cm;
    }
    Last edited by zcrself; 04-21-2010 at 11:51 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So you want a 2d array. You need to do:
    Code:
    if((cm=(char **)calloc(nrow,sizeof(char*)))==NULL)
    {
        printf("error cmatrix: calloc error No. 1 \n");
        return(NULL);
     }
    for (i = 0; i < nrows; ++i) 
    {
             if((cm[i]=(char *)calloc(ncol,sizeof(char)))==NULL)
             {
                printf("error cmatrix: calloc error No. 1 \n");
                return(NULL);
             }
    }
    You allocate nrow pointers to char. For each such pointer you allocate ncol char. Thus you have nrow*ncol char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug in Best-Fit Memory Allocation program (Simulation)
    By RommelTJ in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 03:43 PM
  2. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  3. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  4. Reallocating Memory
    By Booie2k1 in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 06:09 AM
  5. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21