Author |
Message |
Ben Lachma #1 / 11
|
 malloc/free question
I have this piece of code that keeps crashing on me when I try to free the memory I've allocated using malloc, I read to the faq but it didn't seem to address this issue. trafficCurve1 = malloc( (2 * (3 * sigma)) * sizeof(int) ); trafficCurve2 = malloc( (2 * (3 * sigma)) * sizeof(int) ); for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ ) { trafficCurve1[i] = makeCurve(mean, sigma, x); /* I've also tried using (*(trafficCurve+i)) which works fine but doesn't stop the crashing */ } /* the makeCurve function looks like this: float makeCurve(float mean, float sigma, int x) { return ((1/(sigma * sqrt((2 * PI)))) * (pow(E, (-(pow((x - mean), 2)) / (2 * (pow(sigma, 2))))))); Quote: }
*/ for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ ) { trafficCurve2[i] = makeCurve(mean, sigma, x); } /* bunch of stuff here, none of which modifies the arrays ( i've moved the free calls around) */ free( trafficCurve1 ); free( trafficCurve2 ); Obviously it has something to do with my use of trafficCurve[i] = [...] but what exactally is it? I haven't been able to figure it out :( Thanks for any advice... Ben
|
Sat, 03 Aug 2002 03:00:00 GMT |
|
 |
Jarm #2 / 11
|
 malloc/free question
Whay are you allocating n * sizeof(int) and then storing floats in trafficCurves 1 & 2?
Quote: > I have this piece of code that keeps crashing on me when I try to free > the memory I've allocated using malloc, I read to the faq but it didn't > seem to address this issue. > trafficCurve1 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > trafficCurve2 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ > ) { > trafficCurve1[i] = makeCurve(mean, sigma, x); > /* I've also tried using (*(trafficCurve+i)) which works fine but > doesn't stop the crashing */ > } > /* the makeCurve function looks like this: > float makeCurve(float mean, float sigma, int x) > return ((1/(sigma * sqrt((2 * PI)))) * (pow(E, (-(pow((x - mean), 2)) / > (2 * (pow(sigma, 2))))))); > } > */ > for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ > ) { > trafficCurve2[i] = makeCurve(mean, sigma, x); > } > /* bunch of stuff here, none of which modifies the arrays ( i've moved > the free calls around) */ > free( trafficCurve1 ); > free( trafficCurve2 ); > Obviously it has something to do with my use of trafficCurve[i] = [...] > but what exactally is it? I haven't been able to figure it out :( > Thanks for any advice... > Ben
|
Sat, 03 Aug 2002 03:00:00 GMT |
|
 |
Ben Lachma #3 / 11
|
 malloc/free question
hmmm, thanks for pointing that out. It doesn't change anything tho, it still quits on the free() call :( Any other insight would be helpful. btw, i've also used calloc( (2 * (3 * sigma)), sizeof(float) ); which also didn't work. Quote:
> Whay are you allocating n * sizeof(int) and then storing floats in > trafficCurves 1 & 2?
> > I have this piece of code that keeps crashing on me when I try to free > > the memory I've allocated using malloc, I read to the faq but it didn't > > seem to address this issue. > > trafficCurve1 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > > trafficCurve2 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > > for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ > > ) { > > trafficCurve1[i] = makeCurve(mean, sigma, x); > > /* I've also tried using (*(trafficCurve+i)) which works fine but > > doesn't stop the crashing */ > > } > > /* the makeCurve function looks like this: > > float makeCurve(float mean, float sigma, int x) > > return ((1/(sigma * sqrt((2 * PI)))) * (pow(E, (-(pow((x - mean), 2)) / > > (2 * (pow(sigma, 2))))))); > > } > > */ > > for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ > > ) { > > trafficCurve2[i] = makeCurve(mean, sigma, x); > > } > > /* bunch of stuff here, none of which modifies the arrays ( i've moved > > the free calls around) */ > > free( trafficCurve1 ); > > free( trafficCurve2 ); > > Obviously it has something to do with my use of trafficCurve[i] = [...] > > but what exactally is it? I haven't been able to figure it out :( > > Thanks for any advice... > > Ben
|
Sun, 04 Aug 2002 03:00:00 GMT |
|
 |
Stephen How #4 / 11
|
 malloc/free question
Quote: > Obviously it has something to do with my use of trafficCurve[i] = [...] > but what exactally is it? I haven't been able to figure it out :(
Well, we are not going to know are we? Where is the declaration of trafficCurve? From your code I cannot tell if you have int *trafficCurve2; float *trafficCurve2; double *trafficCurve2; int trafficCurve2[20]; etc. So how am I going to know if your fragment of code is storing things in trafficCurve2 correctly? Answer: I don't. You should _ALWAYS_ post complete, compilable snippets. Thanks. Stephen Howe
|
Sun, 04 Aug 2002 03:00:00 GMT |
|
 |
anonym.. #5 / 11
|
 malloc/free question
Quote: >I have this piece of code that keeps crashing on me when I try to free >the memory I've allocated using malloc, I read to the faq but it didn't >seem to address this issue.
I assume sigma is a float and trafficCurve1 and 2 are pointers to float.. then I would reccommend you declare an integer variable called "N" and do this: N = (int)(6*sigma + 0.5); /* there is a real danger here if 6*sigma could be too large to be represented as an int or if it could be negative */ trafficCurve1=malloc(N * sizeof (*trafficCurve1)); if (trafficCurve1==NULL) abort(); trafficCurve2=malloc(N * sizeof (*trafficCurve2)); if (trafficCurve2==NULL){ free trafficCurve1; trafficCurve1=NULL; abort(); Quote: }
/* you should always test the return value from malloc */ for( x = mean - (3 * sigma), i = 0 ; i < N ; x++, i++) trafficCurve1[i] = makeCurve(mean, sigma, x); /* this way you are at least guaranteed to be looping the correct number of times and accessing properly allocated memory. */ /* I suspect you want to declare some kind of a float called, say, delta, and let it be equal to 6.0 * sigma/N. You should also probably make x a float. Then you could change the for loop above to something like this: for( x = mean - (3 * sigma), i = 0 ; i < N ; x+=delta) trafficCurve1[i++] = makeCurve(mean, sigma, x); */ ---- I am kind of surprised that your compiler didn't give you any warnings about this code. You assign a float value to an int, for example. This can loose information. Then you use that integer for loop control. Also, do you realize that with your code, if 6* sigma is less than one half you will actually pass zero to malloc? and then you don't check malloc to see if any space was actually allocated? Anyway, I hope my suggestions help. Good luck and keep at it. regards, GW Quote: > trafficCurve1 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > trafficCurve2 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ >) { > trafficCurve1[i] = makeCurve(mean, sigma, x); > /* I've also tried using (*(trafficCurve+i)) which works fine but >doesn't stop the crashing */ > } >/* the makeCurve function looks like this: >float makeCurve(float mean, float sigma, int x) >{ > return ((1/(sigma * sqrt((2 * PI)))) * (pow(E, (-(pow((x - mean), 2)) / >(2 * (pow(sigma, 2))))))); >} >*/ > for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ >) { > trafficCurve2[i] = makeCurve(mean, sigma, x); > } > /* bunch of stuff here, none of which modifies the arrays ( i've moved >the free calls around) */ > free( trafficCurve1 ); > free( trafficCurve2 ); >Obviously it has something to do with my use of trafficCurve[i] = [...] >but what exactally is it? I haven't been able to figure it out :( >Thanks for any advice... >Ben
|
Mon, 05 Aug 2002 03:00:00 GMT |
|
 |
Felix Radensk #6 / 11
|
 malloc/free question
Quote:
> I have this piece of code that keeps crashing on me when I try to free > the memory I've allocated using malloc, I read to the faq but it didn't > seem to address this issue. > trafficCurve1 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > trafficCurve2 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ > ) { > trafficCurve1[i] = makeCurve(mean, sigma, x); > /* I've also tried using (*(trafficCurve+i)) which works fine but > doesn't stop the crashing */ > } > /* the makeCurve function looks like this: > float makeCurve(float mean, float sigma, int x) > { > return ((1/(sigma * sqrt((2 * PI)))) * (pow(E, (-(pow((x - mean), 2)) / > (2 * (pow(sigma, 2))))))); > } > */ > for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ > ) { > trafficCurve2[i] = makeCurve(mean, sigma, x); > } > /* bunch of stuff here, none of which modifies the arrays ( i've moved > the free calls around) */ > free( trafficCurve1 ); > free( trafficCurve2 ); > Obviously it has something to do with my use of trafficCurve[i] = [...] > but what exactally is it? I haven't been able to figure it out :( > Thanks for any advice... > Ben
Did you try casting trafficCurve1 & trafficCurve2 to (float *), like this trafficCurve1 = (float *)malloc( (2 * (3 * sigma)) * sizeof(float) ); Felix.
|
Fri, 09 Aug 2002 03:00:00 GMT |
|
 |
XenoGea #7 / 11
|
 malloc/free question
Quote:
> I have this piece of code that keeps crashing on me when I try to free > the memory I've allocated using malloc, I read to the faq but it didn't > seem to address this issue. > trafficCurve1 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > trafficCurve2 = malloc( (2 * (3 * sigma)) * sizeof(int) );
Before you malloc trafficCurve1 and trafficCurve2, make sure you set: trafficCurve1 = NULL; trafficCurve2 = NULL; I've run into some horrible bugs later for not doing this.. Also, check the values of trafficCurve1 & 2 AFTER you malloc. If either are 0 right after you malloc, malloc didn't work, and you'll be trying to free *nothing* at the end, which could cause this problem to occur. Good luck, Jeremiah Spradlin
|
Fri, 09 Aug 2002 03:00:00 GMT |
|
 |
Gunnar Andersso #8 / 11
|
 malloc/free question
Quote:
> > I have this piece of code that keeps crashing on me when I try to free > > the memory I've allocated using malloc, I read to the faq but it didn't > > seem to address this issue. > > trafficCurve1 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > > trafficCurve2 = malloc( (2 * (3 * sigma)) * sizeof(int) ); > Before you malloc trafficCurve1 and trafficCurve2, make sure you set: > trafficCurve1 = NULL; > trafficCurve2 = NULL; > I've run into some horrible bugs later for not doing this..
Could you elaborate on this? I can't see how the contents of these variables before the calls to malloc() can matter. Unless, of course, they point to memory blocks to which no other pointers point, thus creating dangling pointers. But assigning NULL to trafficCurve* won't solve that problem anyway. / Gunnar
|
Fri, 09 Aug 2002 03:00:00 GMT |
|
 |
Barry Schwar #9 / 11
|
 malloc/free question
What is the approximate value of sigma? It appears to some type of float. Is it sufficiently small so that the argument to malloc when converted to integer will be less than the size of "i" ints? If yes, you are writing beyond the end of the allocated area.
Quote:
>> I have this piece of code that keeps crashing on me when I try to free >> the memory I've allocated using malloc, I read to the faq but it didn't >> seem to address this issue. >> trafficCurve1 = malloc( (2 * (3 * sigma)) * sizeof(int) ); >> trafficCurve2 = malloc( (2 * (3 * sigma)) * sizeof(int) ); >> for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ >> ) { >> trafficCurve1[i] = makeCurve(mean, sigma, x); >> /* I've also tried using (*(trafficCurve+i)) which works fine but >> doesn't stop the crashing */ >> } >> /* the makeCurve function looks like this: >> float makeCurve(float mean, float sigma, int x) >> { >> return ((1/(sigma * sqrt((2 * PI)))) * (pow(E, (-(pow((x - mean), 2)) / >> (2 * (pow(sigma, 2))))))); >> } >> */ >> for( x = mean - (3 * sigma), i = 0 ; x <= mean + (3 * sigma) ; x++, i++ >> ) { >> trafficCurve2[i] = makeCurve(mean, sigma, x); >> } >> /* bunch of stuff here, none of which modifies the arrays ( i've moved >> the free calls around) */ >> free( trafficCurve1 ); >> free( trafficCurve2 ); >> Obviously it has something to do with my use of trafficCurve[i] = [...] >> but what exactally is it? I haven't been able to figure it out :( >> Thanks for any advice... >> Ben >Did you try casting trafficCurve1 & trafficCurve2 to (float *), like this >trafficCurve1 = (float *)malloc( (2 * (3 * sigma)) * sizeof(float) ); >Felix.
<<Remove the del for email>>
|
Sun, 11 Aug 2002 03:00:00 GMT |
|
 |
-hs- #10 / 11
|
 malloc/free question
Quote: >Did you try casting trafficCurve1 & trafficCurve2 to (float *), like this >trafficCurve1 = (float *)malloc( (2 * (3 * sigma)) * sizeof(float) );
CS is not experimental. You must only follow the rules. There is absolutely no reason for casting the return of malloc() because it is a void*. AFAIK, the () are superfleous in a all-mult. expression. trafficCurve1 = malloc( 2 * 3 * sigma * sizeof(float) ); -- -hs- CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html ISO-C Library: http://www.dinkum.com/htm_cl "It's specified. But anyone who writes code like that should be transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
|
Sun, 11 Aug 2002 03:00:00 GMT |
|
 |
Lawrence Kir #11 / 11
|
 malloc/free question
Quote:
>> I have this piece of code that keeps crashing on me when I try to free >> the memory I've allocated using malloc, I read to the faq but it didn't >> seem to address this issue. >> trafficCurve1 = malloc( (2 * (3 * sigma)) * sizeof(int) ); >> trafficCurve2 = malloc( (2 * (3 * sigma)) * sizeof(int) ); >Before you malloc trafficCurve1 and trafficCurve2, make sure you set: > trafficCurve1 = NULL; > trafficCurve2 = NULL;
Any values you assign to trafficCurve1 and trafficCurve2 just prior to the aloocation statements will be immediately overwritten by them so those values will simply be lost. There is no point in doing this because it cannot make any difference to the program. Quote: >I've run into some horrible bugs later for not doing this..
If your program contains horrible bugs before you out the assignment staements in it will contain the same same horrible bugs after you put them in. The correct course of action is to fix the horrible bugs. Quote: >Also, check the values of trafficCurve1 & 2 AFTER you malloc. If >either are 0 right after you malloc, malloc didn't work, and you'll >be trying to free *nothing* at the end, which could cause this problem >to occur.
Well, free() is defined to do nothing when passed a null pointer so that isn't a problem. However if the malloc's fail then you must not try to access what trafficCurve1 and trafficCCurve2 point to because they don't point to anything. So you are correct that it is important to test the return values. -- -----------------------------------------
-----------------------------------------
|
Mon, 12 Aug 2002 03:00:00 GMT |
|
|
|