some question about memory allocation not enough

M

miloody

Dear all:
My pc installed 2G DRAM, but when I compile a source code like below:

typedef struct _Y_Block{
unsigned char uData[16][16];
}Y_Block;

int main(void){
Y_Block ppYTmpblocks[1920/16][1920/16];
Y_Block ppUTmpblocks[1920/8][1920/8];
Y_Block ppVTmpblocks[1920/8][1920/8];
ppVTmpblocks[1920/8][1920/8].uData[0][0]=0x12; //xxxx
return 0;
}

The os will say "Program received signal SIGSEGV, Segmentation fault.
main () at test.c:9"
Does that mean my memory is really not enough?
if the problem doesn't come from memory, where I can fix it?
appreciate your help,
miloody
 
E

Eric Sosman

miloody said:
Dear all:
My pc installed 2G DRAM, but when I compile a source code like below:

typedef struct _Y_Block{
unsigned char uData[16][16];
}Y_Block;

int main(void){
Y_Block ppYTmpblocks[1920/16][1920/16];
Y_Block ppUTmpblocks[1920/8][1920/8];
Y_Block ppVTmpblocks[1920/8][1920/8];
ppVTmpblocks[1920/8][1920/8].uData[0][0]=0x12; //xxxx
return 0;
}

The os will say "Program received signal SIGSEGV, Segmentation fault.
main () at test.c:9"
Does that mean my memory is really not enough?
if the problem doesn't come from memory, where I can fix it?
appreciate your help,

This is Question 16.3 in the comp.lang.c Frequently
Asked Questions (FAQ) list, <http://www.c-faq.com/>.
 
R

Richard Tobin

miloody said:
Y_Block ppVTmpblocks[1920/8][1920/8];
ppVTmpblocks[1920/8][1920/8].uData[0][0]=0x12; //xxxx

The subscripts for ppVTmpblocks run from 0 to 1920/8 - 1, so you
are setting a non-existent element.

You also probably don't have enough stack space available; how to fix
that depends on your operating system. For unix with bash, look up
"ulimit".

-- Richard
 
K

Keith Thompson

miloody said:
Y_Block ppVTmpblocks[1920/8][1920/8];
ppVTmpblocks[1920/8][1920/8].uData[0][0]=0x12; //xxxx

The subscripts for ppVTmpblocks run from 0 to 1920/8 - 1, so you
are setting a non-existent element.

You also probably don't have enough stack space available; how to fix
that depends on your operating system. For unix with bash, look up
"ulimit".

Tweaking the OS settings so that one particular invocation of the
program has enough stack space is one solution, but a better one is
probably to arrange for the program not to use so much stack space.

(Lengthy quibbling about the meaning of "stack" omitted.)

And as Eric said, this is question 16.3 of the clc FAQ.
 
M

miloody

Hi:
[program with large objects on the stack and and off-by-one access error
snipped]

In a structured program with subroutines arranged in a roughly balanced
tree, subroutine depth grows logarithmically as a fucntion of program
complexity. However the total amount of data the program operates on will
tend to grow linearly with program complexity.

What this means is that stacks can be very small, which means we can
potentially put them into a fast area ofmemory, like the primary cache.
However we have to be disciplined in excluding large objects from the stack
and putting them in dynamicmemoryallocated with malloc. (Or creating them
as globals. Sometimes it is better to have a large object as a global).
My problem has been solved if I use malloc rather than declare those
variables in my functions.
I think the problems come from un-enough stack size.
But I type ulimit, and it says unlimited like below.
Does that really mean my stack is unlimited?

root@T43:/# ulimit
unlimited
root@T43:/#

appreciate your help,
miloody
 
J

James Kuyper

miloody wrote:
....
But I type ulimit, and it says unlimited like below.
Does that really mean my stack is unlimited?

root@T43:/# ulimit
unlimited
root@T43:/#

No, what ulimit tells you about is externally imposed upper limits.
You're still limited by the total amount of memory available.
 
N

Nate Eldredge

James Kuyper said:
miloody wrote:
...

No, what ulimit tells you about is externally imposed upper
limits. You're still limited by the total amount of memory available.

Also, on most shells `ulimit' without arguments tells you about the
limit on the size of files created by the shell. You probably want
`ulimit -s'.
 
M

miloody

Hi
Also, on most shells `ulimit' without arguments tells you about the
limit on the size of files created by the shell. You probably want
`ulimit -s'.
I can see the stack size by ulimit like below.
(I should use the right option.)
root@T43# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 16374
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 16374
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
root@T43#
appreciate your help,
miloody
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,113
Members
46,700
Latest member
VYJJanette

Latest Threads

Top