SAITO@sdr.slb.com ("Naoki Saito Ext. 5471", GEO-002) (05/31/89)
Hi, everyone! There's a bug in new and delete in g++-1.34.2 on Sun3 OS 4.0.1. I reported this before, but I confirmed this bug by simpler examples. Please try these examples on your machines and let me know what happens. Program (1) does produce out of memory error. (2) does not produce any error. (1) Error stuff. =====================cut here=============================================== // newtest.cc #include <stream.h> #include <math.h> main() { float array[100]; float window[100]; float *res, *product(float*, float*); int i; for (i = 0; i < 100; i++) { array[i] = (float)i; window[i] = cos(M_PI*(i-50)/100); } for (i = 0; i < 1000000; i++) { res = product(array, window); delete[100] res; } exit(0); } float *product(float* array, float* window) { int i; float* temp = new float[100]; for (i = 0; i < 100; i++) temp[i] = array[i]*window[i]; return temp; } ============================================================================== (2) The following program which basically replaces new and delete to malloc and free works fine without any error. =====================cut here================================================= // newtest2.cc #include <stdio.h> #include <math.h> main() { float array[100]; float window[100]; float *res, *product(float*, float*); int i; for (i = 0; i < 100; i++) { array[i] = (float)i; window[i] = cos(M_PI*(i-50)/100); } for (i = 0; i < 1000000; i++) { res = product(array, window); free(res); } exit(0); } float *product(float* array, float* window) { float *temp; int i; temp = (float*) malloc(sizeof(float)*100); for (i = 0; i < 100; i++) temp[i] = array[i]*window[i]; return temp; } ============================================================================== Both of them were compiled with $ g++ -g -O -m68881 -o newtest newtest.cc -lg++ -lm style. Does g++-1.35.* on Sun3 OS 4.0.1 produce the same bug? Thank you very much, Naoki Saito (saito@sdr.slb.com) Schlumberger-Doll Research
sacco@eileen.samsung.com (Joseph E. Sacco) (06/03/89)
I observed the same results on a SUN4 using g++-1.35.1-. The problem seems to be with the statement "delete[100] res; " If this statement is replaced by "delete res;" all is well under g++. This example is quite like the one on page 92 of Bjarne's book. Since you are not deleting a vector of class objects [see section 5.5.5] the book suggests "... the vector size provided by the user is ignored ..." This seems to be the case under Cfront1.2 where your program runs with or without [100] appended to delete [albeit slower by a factor of two ]. JOE