Thread: Re: [Dev-C++] Multidimensional arrays and memory leaks
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <tay...@dd...> - 2004-08-10 23:15:08
|
Thanks to your advice I was able to go a little further with my program.
However, when I tried to assign values to the array, the debugger gives me a
message telling about a segment fragmentation problem and then crashes.
The way I assign the values is the following:
for (i=0;i<FL[0];i++)
{
numbuque[i][0]=0; // tipo de buque - rango de esloras menor
numbuque[i][1]=i; // numero de llegada del buque
}
Any suggestion? Thanks,
Jorge
> First, allocate the "pointers" array, and then allocate the arrays
> inside. In C:
>
> number=(float **)malloc(2*sizeof(float *));
> ....
> int i;
> for(i=0;i<50000;i++){
> *(number+i)=(float *)malloc(sizeof(float));
> ...
> }
>
> and so on.
>
> tay...@dd... escribió:
>
> >Hi,
> >
> >Since I tried to use a 2D array "number[50000][2]" I started to have
ran> dom
> >crashes at run time and debugging.
> >
> >I guess this is a memory handling problem (what do you think?). Thus, I
> tried
> >to put the array in the heap (would that help?). However I tried many
wa> y to do
> >it without success.
> >
> >I declared the array in the header of a class as:
> >
> >float number[][];
> >
> >Then I tried to put it in the heap using GlobalAlloc in the
constructor.>
> >
> >
> >Could anybody give me a hand? Thanks a lot.
> >
> >Jorge
> >
> >
> >---------------------------------------------
> >This message was sent using Endymion MailMan.
> >http://www.endymion.com/products/mailman/
> >
> >
> >
> >
> >-------------------------------------------------------
> >SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
> >100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
> >Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
> >http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
> >_______________________________________________
> >Dev-cpp-users mailing list
> >Dev...@li...
> >TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
> >
> >
>
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
> 100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
> Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
> http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
---------------------------------------------
This message was sent using Endymion MailMan.
http://www.endymion.com/products/mailman/
|
|
From: Jorge A. <jor...@ep...> - 2004-08-11 02:00:02
|
I find this in Google=20 (http://www.ics.uci.edu/~dan/class/165/notes/bigarrays.html), maybe it=20 can help: Big arrays in C procedures There is a not-very-huge number N such that, if a procedure declares a=20 local integer array larger than N, you get a segmentation fault when the=20 procedure is called. The same holds for multiple arrays whose total=20 space is equivalent. Apparently whatever C uses for automatic variables=20 cannot handle this magnitude of array on the stack of procedure calls. =20 I would suggest that you use one of the following methods: 1. Declare the large arrays as a local variable in one file, and as a global variable in all procedures. This works, but I can see no advantage to it relative to the following two methods. 2. Declare the arrays in the procedures as static. This retains locality of variables, and is quite simple. However, the (excessive) storage remains in existence even when the procedures that use it are not executing. 3. Only declare (int **) variables, and use calloc to get the required space when needed, returning it when done. This retains locality of variables without wasting a large chunk of space when the procedures that use the big arrays are not in use. There is an added bonus in that you can save much space for each array by building triangular arrays, which is not much harder than building square ones when using C's pointer-vector system. tay...@dd... escribi=F3: >Thanks to your advice I was able to go a little further with my program.= =20 >However, when I tried to assign values to the array, the debugger gives = me a=20 >message telling about a segment fragmentation problem and then crashes.=20 > >The way I assign the values is the following: > > >for (i=3D0;i<FL[0];i++) > { > numbuque[i][0]=3D0; // tipo de buque - rango de esloras menor > numbuque[i][1]=3Di; // numero de llegada del buque > =20 > } > >Any suggestion? Thanks, > >Jorge=20 > > > > > =20 > >>First, allocate the "pointers" array, and then allocate the arrays=20 >>inside. In C: >> >>number=3D(float **)malloc(2*sizeof(float *)); >>.... >>int i; >>for(i=3D0;i<50000;i++){ >> *(number+i)=3D(float *)malloc(sizeof(float)); >> ... >>} >> >>and so on. >> >>tay...@dd... escribi=F3: >> >> =20 >> >>>Hi, >>> >>>Since I tried to use a 2D array "number[50000][2]" I started to have >>> =20 >>> >ran> dom=20 > =20 > >>>crashes at run time and debugging.=20 >>> >>>I guess this is a memory handling problem (what do you think?). Thus, = I >>> =20 >>> >>tried >> =20 >> >>>to put the array in the heap (would that help?). However I tried many >>> =20 >>> >wa> y to do=20 > =20 > >>>it without success.=20 >>> >>>I declared the array in the header of a class as: >>> >>>float number[][]; >>> >>>Then I tried to put it in the heap using GlobalAlloc in the >>> =20 >>> >constructor.> =20 > =20 > >>>Could anybody give me a hand? Thanks a lot. >>> >>>Jorge=20 >>> >>> >>>--------------------------------------------- >>>This message was sent using Endymion MailMan. >>>http://www.endymion.com/products/mailman/ >>> >>> >>> >>> >>>------------------------------------------------------- >>>SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media >>>100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33 >>>Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift. >>>http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285 >>>_______________________________________________ >>>Dev-cpp-users mailing list >>>Dev...@li... >>>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >>> >>>=20 >>> >>> =20 >>> >> >>------------------------------------------------------- >>SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media >>100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33 >>Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift. >>http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285 >>_______________________________________________ >>Dev-cpp-users mailing list >>Dev...@li... >>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >> >> =20 >> > > > >--------------------------------------------- >This message was sent using Endymion MailMan. >http://www.endymion.com/products/mailman/ > > > > >------------------------------------------------------- >SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media >100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33 >Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift. >http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285 >_______________________________________________ >Dev-cpp-users mailing list >Dev...@li... >TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > =20 > |