Is my memory is not enough?

C

chinabooter2003

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
 
E

E. Robert Tisdale

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
Linux
 
K

kiru.sengal

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.
 
R

Richard Tobin

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
 
K

Keith Thompson

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.
 
B

Big K

(e-mail address removed) 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]
 
B

Barry Schwarz

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>>
 
B

Barry Schwarz

(e-mail address removed) 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>>
 
P

Peter Shaggy Haywood

Groovy hepcat (e-mail address removed) 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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top