Mailinglist Archive: opensuse (3378 mails)

< Previous Next >
Re: [SLE] gcc compiler ... BUG or MISUSE ????
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
x[0] = malloc(2);
x[1] = malloc(2);
x[i][j] = a value;
Now we want to expand the rows array:
x = realloc(x, 10);
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

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/".
> 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@xxxxxxxx
> For additional commands send e-mail to suse-linux-e-help@xxxxxxxx
> Also check the archives at

Jerry Feldman
Portfolio Partner Engineering
Compaq Computer Corp.
200 Forest Street MRO1-3/F1
Marlboro, Ma. 01752

< Previous Next >