How to use new and delete functions in Visual C ++ 6/ Please help me. Very Urgent!!!!

Discussion in 'C++' started by Liu Ju, Jul 4, 2003.

  1. Liu Ju

    Liu Ju Guest

    Dear people,


    I wrote a program in Visual C6 in which I used the functions
    "malloc" and "free". The errors sometimes appears due to some mystery
    causes.
    A friend of mine suggested that I use new and delete functions instead
    since malloc and free are not always good.

    My aim is to set aside a free range of memory and then initialize
    these values. My orginal source codes using malloc and free:
    -----------------
    int *accum;
    float *tabSin;
    float *tabCos;
    accum = (int *) malloc( sizeof( int ) * numangle * numrho );
    tabSin = (float *) malloc( sizeof( float ) * numangle );
    tabCos = (float *) malloc( sizeof( float ) * numangle );
    memset( accum, 0, sizeof( int ) * numangle * numrho );

    blah blah blah
     
    Liu Ju, Jul 4, 2003
    #1
    1. Advertising

  2. "Liu Ju" <> wrote in message
    news:...
    > Dear people,
    >
    >
    > I wrote a program in Visual C6 in which I used the functions
    > "malloc" and "free". The errors sometimes appears due to some mystery
    > causes.
    > A friend of mine suggested that I use new and delete functions instead
    > since malloc and free are not always good.


    new and delete are *different*, they are not better or worse. If you don't
    understand the difference how do you expect to improve your program by
    substituting on for the other?

    >
    > My aim is to set aside a free range of memory and then initialize
    > these values. My orginal source codes using malloc and free:
    > -----------------
    > int *accum;
    > float *tabSin;
    > float *tabCos;
    > accum = (int *) malloc( sizeof( int ) * numangle * numrho );
    > tabSin = (float *) malloc( sizeof( float ) * numangle );
    > tabCos = (float *) malloc( sizeof( float ) * numangle );
    > memset( accum, 0, sizeof( int ) * numangle * numrho );
    >
    > blah blah blah
    > .
    > .
    > free(accum);
    > free(tabSin);
    > free(tabCos);
    >
    > ---------------------
    >
    >
    > Errors often appear.
    >
    >
    > Now I want to use new and delete instead. But what I need are the
    > range of memory is not fixed while programming. It would be pass to
    > the program as a parameter.
    >
    > Can I write it as
    > ------------------------
    > int* accum = new int[numangle * numrho];
    > float* tabSin = new float[numangle];
    > float* tabCos = new float[numangle];
    >
    > for(int i=0;i<=numangle * numrho;i++)
    > accum=0;
    >
    > -----------------
    >
    >
    > would it work without risk???


    Don't forget

    delete[] accum;
    delete[] tabSin;
    delete[] tabCos;

    Your code is perfectly correct, but whatever your problem was before this is
    not going to fix it.

    You need to find out what the real problem is, instead of guessing.

    >
    > Thank you very much,
    >
    > Yours truly,
    >
    > LiuJu


    john
     
    John Harrison, Jul 4, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,621
    Victor Bazarov
    Jun 21, 2004
  2. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    486
    Raymond Hettinger
    Jul 27, 2003
  3. Replies:
    3
    Views:
    380
    Milsnips
    Nov 15, 2006
  4. vhdl124
    Replies:
    1
    Views:
    496
    vhdl124
    Apr 28, 2008
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,310
Loading...

Share This Page