Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


limit on size of arrays inside a struct in C?

Posted on 2003-04-01
Medium Priority
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];
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?

Question by:chmod101
LVL 15

Accepted Solution

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.


Author Comment

ID: 8247610
GREAT, informative response.

Thank you for your prompt help,


Featured Post

Technology Partners: 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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

579 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