I' trying to develop a simulation that unluckily requires dynamic memory allocation. I allocate three 1-D vectors and two 2-D arrays. At run time the programs runs fine as long as I do not need to reallocate (to increase the space of) the 2-D array called "Ibox". Whenever I try to reallocate "Ibox" I get a Segmentation Fault from a system routine "memcpy /lib/libc.so.6". If I make the initial arrays very big so that no reallocation is necessary then the programs runs to the end with no problem at all. However, I relunctantly consider this option as the amount of memory required depends upon the size of the reals system being simulated. Hence any hard-coded allocated memory size would as a consequence limit the usefulness of the program itselt. I'll be sending three separate messages with one attached snapshot each: Realloc0.png shows how memory for "Ibox" is allocated the first time. Realloc1.png shows a failing attempt to realloc "Ibox" Realloc2.png shows another failing attempt to realloc "Ibox" Please, notice that all these arrays are STATIC GLOBAL variables as declared outside any routine. Changing their type from STATIC GLOBAL to AUTOMATIC is not possible as the contents of those arrays must be available to the program all its life long ! I would appreciate any suggestion and or comment about this problem. Thank you in advance for your attention. best regards, Maura E.M.
* Maura Edelweiss
I' trying to develop a simulation that unluckily requires dynamic memory allocation. I allocate three 1-D vectors and two 2-D arrays. At run time the programs runs fine as long as I do not need to reallocate (to increase the space of) the 2-D array called "Ibox".
Of course. There is no way you can get out of that. Just small memory controlling routines in with your stuff and you should be fine. ... but then again. What programming language is this? -- Mads Martin Jørgensen, http://mmj.dk "Why make things difficult, when it is possible to make them cryptic and totally illogic, with just a little bit more effort?" -- A. P. J.
On Thursday 18 April 2002 22:17, Maura Edelweiss wrote:
Realloc0.png shows how memory for "Ibox" is allocated the first time. Realloc1.png shows a failing attempt to realloc "Ibox" Realloc2.png shows another failing attempt to realloc "Ibox"
Instead of sending pictures, do a minimal program that exhibits the problem and post that. Looking at error messages is more or less meaningless unless you see the code that causes them. Anders
Automatic variables have a "function" scope, and disappear when you return from the function (they are mostly allocated on the stack but many times simply allocated as a register). Other than that "static global" variables have file scope and are visible across functions. 1D arrays are easy. char *x = malloc(size); Your array is a simple linear array. Multi-dimensional arrays are more difficult. They are easy if you declare them explicitly: eg. char x[a][b]; But, when you are allocating them, you must specity a pointer and do the arithmetic yourself. All realloc will do is (when expanding) allocate a new area of memory, copying your old block of data. Let's say you want a 2x2 array: To make it simple: char *x = malloc(4); /* 4 characters */ You can make it two dimentional by accessing it as a one dimensional array. Now if that is an array of 32 bit ints the array size would be 16. So, still maintaining chars and making this a 3 x 3. so: x = realloc(x, 9); The problem here is that you have changed the physical shape. So, things get messy because your 4x4 data is in elements 0 through 3. In general, we may allocate 2 dimentional arrays as a set of pointers. char **x = malloc(2); Then (forgive me for the horrible code that no sane C programmer would write). x[0] = malloc(2); x[1] = malloc(2); So, for(i=0;i<2;i++) for(j=0;j<2;j++) x[i][j] = a value; Now we want to expand the rows array: x = realloc(x, 10); for(i=2;i<10;i++) x[i] = malloc(2); Now we have a 10x2 array we can work with. The realloc, copies the first dimension which is the pointers. There are some tricks you can do, but the real problem is that C really does not support arrays in the true sense. If we don't need to change the size of the second dimension, we simplify it. On 18 Apr 2002 at 15:17, Maura Edelweiss wrote:
I' trying to develop a simulation that unluckily requires dynamic memory allocation. I allocate three 1-D vectors and two 2-D arrays. At run time the programs runs fine as long as I do not need to reallocate (to increase the space of) the 2-D array called "Ibox". Whenever I try to reallocate "Ibox" I get a Segmentation Fault from a system routine "memcpy /lib/libc.so.6". If I make the initial arrays very big so that no reallocation is necessary then the programs runs to the end with no problem at all. However, I relunctantly consider this option as the amount of memory required depends upon the size of the reals system being simulated. Hence any hard-coded allocated memory size would as a consequence limit the usefulness of the program itselt. I'll be sending three separate messages with one attached snapshot each: Realloc0.png shows how memory for "Ibox" is allocated the first time. Realloc1.png shows a failing attempt to realloc "Ibox" Realloc2.png shows another failing attempt to realloc "Ibox" Please, notice that all these arrays are STATIC GLOBAL variables as declared outside any routine. Changing their type from STATIC GLOBAL to AUTOMATIC is not possible as the contents of those arrays must be available to the program all its life long !
I would appreciate any suggestion and or comment about this problem. Thank you in advance for your attention.
best regards, Maura E.M.
-- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com
-- Jerry Feldman Portfolio Partner Engineering 508-467-4315 http://www.testdrive.compaq.com/linux/ Compaq Computer Corp. 200 Forest Street MRO1-3/F1 Marlboro, Ma. 01752
participants (4)
-
Anders Johansson
-
Jerry Feldman
-
Mads Martin Jørgensen
-
Maura Edelweiss