f



Is my memory is not enough?

Dear members:

I am programming in C. I declare as follows but when running program,
there is a fatal error appear:


double a[255][255];
double b[255][255];


When I change it to

double a[255][255];
double b[255][100];


then it works.

I think the problem caused by the running out of memory.

But I am wondering why I use malloc function, there is no error:
------------------------
double **a;
double **b;

a=malloc(255*255*sizeof(double));
if(a==NULL)
printf("there is an error");
b= malloc(255*255*sizeof(double));
if(a==NULL)
printf("there is an error");
-------------------------


I think I am using the same volume of memory for both version. Why in
the first case, the memory is insufficient of memory and in the other,
it is big enough.

One more thing, I need to use 2D array. I try the following but there
is always error when I try to access to an element of the 2D array:


double **a;
double **b;

a=malloc(255*255*sizeof(double));
if(a==NULL)
printf("there is an error");
b= malloc(255*255*sizeof(double));
if(a==NULL)
printf("there is an error");

a[1][1]=1;<----- the error occurs


Pleas take a look and point out the error for me. or can  you please
suggest me other more effective methods to use 2D array?

Thank you very much and look forward to your answer.
SIncerly yours

0
1/30/2005 12:00:43 AM
comp.lang.c 30657 articles. 3 followers. spinoza1111 (3246) is leader. Post Follow

10 Replies
392 Views

Similar Articles

[PageSpeed] 3

chinabooter2003@yahoo.com wrote:

> I am programming in C.
> I declare as follows but when running program,
> there is a fatal error appear:
> 
>       double a[255][255];
>       double b[255][255];
> 
> When I change it to
> 
>       double a[255][255];
>       double b[255][100];
> 
> then it works.
> 
> I think the problem caused by the running out of memory.

         > limit stacksize
         stacksize       10240 kbytes
         > limit stacksize unlimited
         > limit stacksize
         stacksize       unlimited
         > uname
         Linux
0
1/30/2005 12:37:50 AM
Hello Robert, what do you mean? can you please repost your answer.
Thanks/

0
1/30/2005 1:07:38 AM
chinabooter2003@yahoo.com wrote:
> Dear members:
>
> I am programming in C. I declare as follows but when running program,
> there is a fatal error appear:
>
>
> double a[255][255];
> double b[255][255];
>
>
> When I change it to
>
> double a[255][255];
> double b[255][100];
>
>
> then it works.
>
> I think the problem caused by the running out of memory.
>
> But I am wondering why I use malloc function, there is no error:
> ------------------------
> double **a;
> double **b;
>
> a=malloc(255*255*sizeof(double));
> if(a==NULL)
> printf("there is an error");
> b= malloc(255*255*sizeof(double));
> if(a==NULL)
> printf("there is an error");
> -------------------------
>
>
> I think I am using the same volume of memory for both version. Why in
> the first case, the memory is insufficient of memory and in the
other,
> it is big enough.
>
> One more thing, I need to use 2D array. I try the following but there
> is always error when I try to access to an element of the 2D array:
>
>
> double **a;
> double **b;
>
> a=malloc(255*255*sizeof(double));
> if(a==NULL)
> printf("there is an error");
> b= malloc(255*255*sizeof(double));
> if(a==NULL)
> printf("there is an error");
>
> a[1][1]=1;<----- the error occurs
>

declare a and b like this

double *a[255];
double *b[255];


You have to declare a & b as pointers to an array of 255 doubles,
otherwise, how would the compiler know how much to "step over" when you
double dereference.  I'm giving you a very simplistic answer (I think
you're a newbie).  Reading the c.l.c FAQ will help you better
understand the concepts.  For now, you can start making use of your
program by just changing the declarations to match what I have above.

See: http://www.eskimo.com/~scs/C-faq/s6.html for more details


As for your conventionally allocated arrays, I am not sure what
happened.  Remember, malloc gives you access to a different type/area
of memory than those conventionally allocated arrays would be place in.

0
1/30/2005 1:07:41 AM
Hi, kiru, I got it. Thank you.

0
1/30/2005 1:12:08 AM
In article <1107043243.709033.125850@z14g2000cwz.googlegroups.com>,
 <chinabooter2003@yahoo.com> wrote:

>I think I am using the same volume of memory for both version. Why in
>the first case, the memory is insufficient of memory and in the other,
>it is big enough.

Most likely the memory is allocated on the stack in one case, and on
the heap in the other.  Your operating system probably has a limit on
the stack size.  How you change that limit will depend on the
operating system; on unix systems the "limit" or "ulimit" command is
what you want.

-- Richard
0
richard91 (3692)
1/30/2005 2:01:08 AM
richard@cogsci.ed.ac.uk (Richard Tobin) writes:
> In article <1107043243.709033.125850@z14g2000cwz.googlegroups.com>,
>  <chinabooter2003@yahoo.com> wrote:
>>I think I am using the same volume of memory for both version. Why in
>>the first case, the memory is insufficient of memory and in the other,
>>it is big enough.
>
> Most likely the memory is allocated on the stack in one case, and on
> the heap in the other.  Your operating system probably has a limit on
> the stack size.  How you change that limit will depend on the
> operating system; on unix systems the "limit" or "ulimit" command is
> what you want.

Or you can just allocate the memory on the heap and not have to
increase your stack limit every time you run the program.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
kst-u (21963)
1/30/2005 6:30:04 AM
kiru.sengal@gmail.com wrote:
> chinabooter2003@yahoo.com wrote:

> >
> > One more thing, I need to use 2D array. I try the following but
there
> > is always error when I try to access to an element of the 2D array:
> >
> >
> > double **a;
> > double **b;
> >
> > a=malloc(255*255*sizeof(double));
> > if(a==NULL)
> > printf("there is an error");
> > b= malloc(255*255*sizeof(double));
> > if(a==NULL)
> > printf("there is an error");
> >
> > a[1][1]=1;<----- the error occurs
> >
>
> declare a and b like this
>
> double *a[255];
> double *b[255];
>

While I was doing something unrelated, my brain told me I made a
mistake showing you the above declaraion. It should be:

double (*a)[255];
double {*b}[255];

This declares pointers to arrays of 255 doubles (which is what you need
when accepting the malloc calls you have).

[The way I showed it before declares an array of 255 pointers to
doubles]

>
> You have to declare a & b as pointers to an array of 255 doubles,
> otherwise, how would the compiler know how much to "step over" when
you
> double dereference.  I'm giving you a very simplistic answer (I think
> you're a newbie).  Reading the c.l.c FAQ will help you better
> understand the concepts.  For now, you can start making use of your
> program by just changing the declarations to match what I have above.
> 
> See: http://www.eskimo.com/~scs/C-faq/s6.html for more details

0
1/30/2005 7:10:10 AM
On 29 Jan 2005 17:07:41 -0800, "kiru.sengal@gmail.com"
<kiru.sengal@gmail.com> wrote:

>declare a and b like this
>
>double *a[255];
>double *b[255];
>
>
>You have to declare a & b as pointers to an array of 255 doubles,

You have declared a and b as arrays of pointers, not as pointers to
arrays.  If you want a pointer to an array, you need
	double (*b)[255];

>otherwise, how would the compiler know how much to "step over" when you
>double dereference.  I'm giving you a very simplistic answer (I think
>you're a newbie).  Reading the c.l.c FAQ will help you better
>understand the concepts.  For now, you can start making use of your
>program by just changing the declarations to match what I have above.
>
>See: http://www.eskimo.com/~scs/C-faq/s6.html for more details

Always a good idea.




<<Remove the del for email>>
0
schwarzb (661)
1/30/2005 4:39:33 PM
On 29 Jan 2005 23:10:10 -0800, "Big K" <kiru.sengal@gmail.com> wrote:

>
>kiru.sengal@gmail.com wrote:
>> chinabooter2003@yahoo.com wrote:
>
>> >
>> > One more thing, I need to use 2D array. I try the following but
>there
>> > is always error when I try to access to an element of the 2D array:
>> >
>> >
>> > double **a;
>> > double **b;
>> >
>> > a=malloc(255*255*sizeof(double));
>> > if(a==NULL)
>> > printf("there is an error");
>> > b= malloc(255*255*sizeof(double));
>> > if(a==NULL)
>> > printf("there is an error");
>> >
>> > a[1][1]=1;<----- the error occurs
>> >
>>
>> declare a and b like this
>>
>> double *a[255];
>> double *b[255];
>>
>
>While I was doing something unrelated, my brain told me I made a
>mistake showing you the above declaraion. It should be:
>
>double (*a)[255];
>double {*b}[255];

Of course, you meant (*b).

>
>This declares pointers to arrays of 255 doubles (which is what you need
>when accepting the malloc calls you have).
>
>[The way I showed it before declares an array of 255 pointers to
>doubles]
>
>>
>> You have to declare a & b as pointers to an array of 255 doubles,
>> otherwise, how would the compiler know how much to "step over" when
>you
>> double dereference.  I'm giving you a very simplistic answer (I think
>> you're a newbie).  Reading the c.l.c FAQ will help you better
>> understand the concepts.  For now, you can start making use of your
>> program by just changing the declarations to match what I have above.
>> 
>> See: http://www.eskimo.com/~scs/C-faq/s6.html for more details



<<Remove the del for email>>
0
schwarzb (661)
1/30/2005 10:18:25 PM
Groovy hepcat chinabooter2003@yahoo.com was jivin' on 29 Jan 2005
17:07:38 -0800 in comp.lang.c.
Re: Is my memory is not enough?'s a cool scene! Dig it!

>Hello Robert, what do you mean? can you please repost your answer.

  No, don't ask Tisdale to do that. As usual, his answer was off topic
and not very helpful.

-- 

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
0
phaywood (258)
1/31/2005 2:18:49 AM
Reply: