new replacing malloc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RS24
    New Member
    • Oct 2006
    • 9

    new replacing malloc

    Hi!
    Can someone tell me the correct answer for these ...

    Replace the following code using new operator-
    (Assume int is 2 bytes)
    #define MAXROW 3
    #define MAXCOL 4

    1.)
    void main()
    {
    int (*p)[MAXCOL];
    p=(int(*) [MAXCOL]) malloc(MAXROW*s izeof(*p));
    }


    2.)
    void main()
    {
    int (*p)[MAXROW][MAXCOL];
    p=(int(*) [MAXROW][MAXCOL]) malloc(sizeof(* p));
    }


    3.)
    void main()
    {
    int **p,i;
    p=new int *[MAXROW];
    for(i=0;i<MAXRO W;i++)
    p[i]=new int[MAXCOL];
    }

    4.)
    void main()
    {
    int **p,i;
    p=(int**)malloc (MAXROW*sizeof( int *));
    p[0]=(int *)malloc(MAXROW *MAXCOL*sizeof( int));
    for(i=0;i<MAXRO W;i++)
    p[i]=p[0]+i*MAXCOL;
    }
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    It is unlikely until you tell us what the question is. Is it "how to rewrite the following code using new rather than malloc"?

    Comment

    • RS24
      New Member
      • Oct 2006
      • 9

      #3
      Originally posted by D_C
      It is unlikely until you tell us what the question is. Is it "how to rewrite the following code using new rather than malloc"?
      Yes! That's exactly what the question is !

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I think you should probably attempt this yourself first (it looks like course/home work to me).

        Bear in mind that these are equivilent

        Code:
        int *pi = malloc(5 * sizeof(int));
        
        int *pi = malloc(5 * sizeof *pi);
        
        int *pi = new int[5];

        Comment

        • RS24
          New Member
          • Oct 2006
          • 9

          #5
          Originally posted by Banfa
          I think you should probably attempt this yourself first (it looks like course/home work to me).

          Bear in mind that these are equivilent

          Code:
          int *pi = malloc(5 * sizeof(int));
          
          int *pi = malloc(5 * sizeof *pi);
          
          int *pi = new int[5];
          ==============

          Hi Ben,

          No, its not a course/home work !
          I n fact the question is from a book and they have given the answers too . but I am not sure that the answers are right. i have an interview 2morrow n it will be really helpful if u check these-

          (these r the answers from the book itself)

          1. p=new int [MAXROW][sizeof(*p)/sizeof(int)];

          2. p=new int[1]MAXROW][MAXCOL];

          3. this one i think is correct
          p=new int*[MAXROW];
          for(----)
          p[i]=new int [MAXCOL];


          4. this too i think is correct
          p=new int*[MAXROW];
          p[0]=new int [MAXROW * MAXCOL];

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            This is probably late (sorry I shall blame the cold that has wiped out the past couple of days) but I think that all there solutions are syntatically correct. Not that I think they are necessarily a good idea.

            Comment

            Working...