Problem of defining large array depletion stack in C language function

Source: Internet
Author: User
Tonight, a classmate asked me to help look at a C program, GCC compile time has been wrong, that is a paragraph error.
The program itself writes poorly, but compiles through, with just dozens of warnings.
Two hours later, in the ECLIPSE+GCC did not find the problem (this environment is not familiar), into the VC below, step-by-step debugging, only to find out the problem in the main call called Readmctal () the first few lines of the function. The function is as follows:
Void readmctal (void) {    int number,count,sign,num_tally,nflag;      int ncell,nstep,mstep,sum;     char * (str_temp[80]);     float temp_spectra[num_cell][egroup],temp_error[num_cell][egroup]; float temp_flux[num_cell],temp_flux_error[num_cell];     int total_cell_no[num_cell];     sum=0;       number=0;     /* open the mctal file */    if  ( Fpt_mc=fopen ("Mctal", "R")) ==null) {        printf ("/nERROR -  cannot open mctal file/n ");         exit (1);    &NBSP}     else     {         printf ("file mctal opened.../n");   &nBSP;&NBSP}      While
# define Num_cell 9999 # define Egroup 175
The two-bit array defined is too large. After looking at the disassembly, it seems to be a stack problem, try to
float Temp_spectra[num_cell][egroup],temp_error[num_cell][egroup];
float Temp_flux[num_cell],temp_flux_error[num_cell];
Move to the body of the function.
To Linux to compile with gcc, "segment error" prompted to disappear.

After analysis, I think a function allocation of memory is limited, in the function body defined two-dimensional array is too large, exhausted the stack, so the error.

PS: Forum for People's Views:
Develop good programming habits, the general company has coding style, which should have provisions:
Inside functions (local variables) prohibit the definition of large arrays, but should use dynamic memory, as an array to pass the function should use pointers as parameters;
In fact, even if you call this function is not wrong, but if the function nested a lot of words will occur segment error
In some resources limited system, more need to pay attention to this problem, such as 51 SCM

Within the function is to allocate memory on the stack, the stack size is generally limited to 1M to 2M
function is a global variable, allocating memory in the data segment



PSPs: Turn to a article on C memory allocation, the original link: http://www.lupaworld.com/bbs/viewthread.php?tid=29577
talking about C memory allocationIt was written a long time ago and is now in the C edition.

On the C language Memory topic to really speak up that I'm afraid there is no head, so this article is only a simple talk.
There is a certain difference between different platforms for memory issues. The platform referred to in this article is x86 Linux platform
C language to do the program (in fact, the same in other languages), not only to familiarize yourself with the grammar, in fact, a lot of relevant background knowledge is also very important. Before learning and studying the memory allocation problem in C, first understand what Linux is assigned to the process (the running program) 's address space.
In general, there are 3 segments, that is, code snippets, data segments, and stack segments (friends who have learned the compilation must be familiar with them). The code snippet is the stored program text, so it is sometimes called a text segment, and the instruction in the instruction pointer is obtained from here. This section is generally can be shared, such as you in Linux opened 2 vi to edit the text, then generally these two VI is to share a code snippet, but the data segment is different (this is somewhat similar to C + + classes in different objects share the same member function). Data segments are used for storing data and can be divided into three regions initialized to Non-zero data areas, BSS, and heaps (HEAP). Initialization of a non-0 data region typically holds static non-0 data and global non-0 data. BSS is the acronym for block started by symbol, which is originally a term in assembly language. This area primarily holds uninitialized global data and static data. There is a heap, this area is for dynamic allocation of memory is used, that is, the use of malloc and other functions allocated memory is in this area. Its address is growing upwards. The last stack segment (note that the stack is stacks, the heap is heap, not the same thing), and the stack is too important, where local variables and function parameters are stored. For example, recursive algorithms are implemented by stacks. The address of the stack is growing downward. Specifically as follows:
======== High Address =======
Program Stack stack Segment
Grow downward
"Empty" =======
Grow up
Heap
------Data Segment
Bss
------
Non 0 data
========= Low Address =======
=========          =======
Code snippet
=========          =======
It is to be noted that there is a clear separation between the code snippet and the data segment, but there is no between the data segment and the stack segment, and the stack is growing downward and the heap is growing upwards, so the heap and stack are theoretically "growing together", but the operating system prevents such errors, so don't worry too much.
With the above theory to pave, the following is said dynamic memory allocation. It says that the dynamic memory space is allocated in the heap. The following functions are implemented for dynamic allocation:
Stdlib.h:
void *malloc (size_t size);
void *calloc (size_t nmemb, size_t size);
void *realloc (void *ptr, size_t size);
void free (void *ptr);
One by one, say it. malloc is allocating a size memory space, pointing to the space with a void type pointer, and then returning the pointer. That is, malloc returns a pointer to the void type of a space of size, and if you want to use that space, you have to convert the void* type to a type you need, such as int*. Calloc and malloc Basically the same, the difference is that there are two points, one is the calloc allocation of space size is determined by the nmemb*size, that is, NMEMB is the number of entries, and size can be considered as the size of the item, the total space task calculated by calloc to do. The second is that the space returned by the calloc is filled with 0, while malloc is not sure what is in memory. ReAlloc is used to change the size of the space that has been allocated. Pointer ptr is of type void, it should point to a space that needs to be reassigned, and the size parameter is the entire space size after the reallocation, not the increased size. Again, a pointer to the new space is returned. Free is used to release the space allocated by the above 3 functions, whose argument is a pointer to a space.
Basically, these are the basic topics, advanced topics and details of a lot of issues, there is no explanation, there are opportunities I will continue to sum up

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.