Linux C + + inexplicable segment error (segmentation fault), unable to invoke other functions

Source: Internet
Author: User
Tags array definition

came in to develop C + + projects under Linux and encountered a very strange bug.

The project requires a multi-threaded implementation, and when the code is written, it will prompt for a segment error (segmentation fault) whenever other functions such as printf, fopen, etc. are called inside the thread function. Programming for a very long time, consciously do not appear very low-level grammatical errors, after careful examination did not find no matter what problem. After reading the code to a lot of powerful friends, it didn't help to find the bug, and then finally found out the problem by thinking about it.

The following is a simplified framework for the outgoing thread function:

void* Thread_func (void* rank) {    long My_rank = (long) rank;    printf ("Thread%ld is working...\n", My_rank);    //...    Char BUFFER[BUFF_SZ];    //...}

This code compilation must pass, and at execution time there will be no problem in assigning values to variables. However, when executing to the printf call function there will be a segment error, which indicates that the function address is not found, but why this problem?!

Originally. Take a look at the buffer array definition that row, inside the array size BUFF_SZ is its own definition of global constants, this constant due to business requirements are determined by the larger (50MB or so). This is where the crux of the problem lies! Such an array definition occupies the thread stack memory, but the Linux thread occupies a stack memory limit of 8MB. This buffer actually fills the entire thread stack memory, which causes the function entry not to be found inside the thread at execution time. So here's mark, let's say that someone will encounter a similar problem later. I hope we can consider this point.

In general, this is a very easy problem to solve, but it's really hard to find (it takes 3 days).

)。 There are two main reasons: 1. Once we rarely apply for a particularly large stack memory, so although we can understand the principle of stack memory limitations, there are very few actual mistakes experienced. 2. The size of the array is represented as a constant (there are many companies that need to avoid magic number). It is difficult to find this number too large. So. When it comes to such a problem, it is possible to waste a lot of time assuming that you have no experience. I hope this article will help meet similar bugs in the future less detours. Spend less time.

At last:

workaround : Use new to dynamically allocate memory to open up heap memory space, but finally remember that delete is released.

find the method of the bug : First of all staring at the function body, and then according to the program section of the implementation of the view, to see what is added to the program segment caused by the problem.


Linux C + + inexplicable segment error (segmentation fault), unable to invoke other functions

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.