?
Solved

limit on size of arrays inside a struct in C?

Posted on 2003-04-01
2
Medium Priority
?
225 Views
Last Modified: 2010-04-15
Hopefully someone knows the answer to this:

This program will work:

#include <stdio.h>

#define WIDTH 600
#define HEIGHT 600
 
double pix [HEIGHT][WIDTH] [3];
 
int main () {

  printf ("hello world\n");
   
  pix [100] [100] [0] = 100.0;

  return 0;

}
 

However, this one seg-faults:

#include <stdio.h>

#define WIDTH 600
#define HEIGHT 600
 
typedef struct {
  double pix [HEIGHT][WIDTH] [3];
}IMG_T;
 
int main () {

  IMG_T img;

  printf ("hello world\n");
   
  img.pix [100] [100] [0] = 100.0;

  return 0;

}
 

So, my thought was that having the information in a struct could
somehow be limiting the allowable size of the array.

This is being compiled using gcc 3.2.1 with OS Solaris8,
on both sparc and x86 platforms.

Any thoughts?

chmod101
0
Comment
Question by:chmod101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
efn earned 200 total points
ID: 8247210
The problem in the second case is not that the array is in a structure, but that it's allocated from automatic storage instead of static storage, and it's too big for the automatic storage.

The array contains 1,080,000 doubles.  In the first case, a fixed area of memory is reserved for the array when the program is loaded.  As long as the machine has enough memory to run the program, this will work.  In the second case, the memory is allocated at run-time, typically from a stack.  There is a limit on the size to which the stack can grow, and the array does not fit within that limit.

There are usually platform-specific run-time options you can use to specify the size of the stack you want to give a process, so you might be able to run the second program if you fiddled with those.

Or if you simply move the declaration of img outside main, that should make it work.

--efn
0
 

Author Comment

by:chmod101
ID: 8247610
GREAT, informative response.

Thank you for your prompt help,

chmod101
<><
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question