in Education by
So I've got some C code: #include #include /* putting one of the "char*"s here causes a segfault */ void main() { char* path = "/temp"; char* temp; strcpy(temp, path); } This compiles, runs, and behaves as it looks. However, if one or both of the character pointers is declared as global variable, strcpy results in a segmentation fault. Why does this happen? Evidently there's an error in my understanding of scope. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
As other posters mentioned, the root of the problem is that temp is uninitialized. When declared as an automatic variable on the stack it will contain whatever garbage happens to be in that memory location. Apparently for the compiler+CPU+OS you are running, the garbage at that location is a valid pointer. The strcpy "succeeds" in that it does not segfault, but really it copied a string to some arbitrary location elsewhere in memory. This kind of memory corruption problem strikes fear into the hearts of C programmers everywhere as it is extraordinarily difficult to debug. When you move the temp variable declaration to global scope, it is placed in the BSS section and automatically zeroed. Attempts to dereference *temp then result in a segfault. When you move *path to global scope, then *temp moves up one location on the stack. The garbage at that location is apparently not a valid pointer, and so dereferencing *temp results in a segfault.

Related questions

0 votes
    static storage is decided at compilation time. However, consider the scenario where we have lot of lazy ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    static storage is decided at compilation time. However, consider the scenario where we have lot of lazy ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    static storage is decided at compilation time. However, consider the scenario where we have lot of lazy ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    In my signal.h file I have added a signal handler like this: #define SIG_WPG ((__sighandler_t)3) and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a class that logs on to a server and retrieves some data. The data is put in a NSMutableArray, ... [heleTeksten componentsSeparatedByString:@"\n"]; NSString *searchFor = @"...
asked Jan 16, 2022 in Education by JackTerrance
0 votes
    What are the common causes of the segmentation fault in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the difference between Segmentation fault and Bus error?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is segmentation fault in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    This smells buggy, but probably, someone can explain it: The following script doesn't work, the output is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    In a function, How to create and use a global variable? How can I use it in other functions? Is it compulsory ... it in a function? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to set some global variables inside a function, how can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I am fairly new to Java, I wanted to know how can i define a global variable ? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    If there are more than one file(main.lua) which contains code and I declare some variables and functions ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    Which of the following global variables is used to get parameters? (a) HTTP_GET_VAR[] (b) HTTP_GET_VARS() ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Which global variables can be used to determine if a transaction is still open? (a) @@NESTLEVEL (b) ... Results topic in portion Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
...