Question

When I run this code use ./main How to fix it when program reminds segmentation fault...

When I run this code use ./main How to fix it when program reminds segmentation fault (core dumped)?

#include <iostream>

void set_num(int* i_prt, int num) {

*i_prt = num;

}

int main(int argc, char** argv) {

int x;

set_num(&x, 13);

std::cout << "x: " << x << std::endl;

int* y;

set_num(y, 4);

std::cout << "*y:" << *y << std::endl;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Segmentation fault (core dumped) is occuring because you are accessing memory that can't be accessed. This can be fixed by declaring y as x.try running this code this will give correct output.

#include<iostream>
void set_num(int *iptr, int num)
{
*iptr=num;
}
int main(int argc,char** argv)
{
int x;
set_num(&x, 13);
std::cout<<"x: "<<x<<std::endl;
int y;
set_num(&y,4);
std::cout<<"y: "<<y<<std::endl;
}

Add a comment
Know the answer?
Add Answer to:
When I run this code use ./main How to fix it when program reminds segmentation fault...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

  • C++ Type in the above program . Before running the program, change each of the question...

    C++ Type in the above program . Before running the program, change each of the question marks to the numbers 1 through 5 indicating the order in which that line will be executed. Run the program and check your numbers. Fix and rerun if necessary. Submit the program and the output. #include <iostream> using namespace std; //prototypes void DemonstrateProc1(); void DemonstrateProc2(int num); int main() { cout << "?. Execution starts with main. " << endl; DemonstrateProc1();       cout << "?....

  • 1) Fix the Function #include <iostream> void print Num() { std::cout << number; }; int main()...

    1) Fix the Function #include <iostream> void print Num() { std::cout << number; }; int main() { int number = 35; printNum (number); return 0; (Give two ways to fix this code. Indicate which is preferable and why.) #include <iostream> void double Number (int num) {num = num * 2;} int main() { int num = 35; double Number (num); std::cout << num; // Should print 70 return 0; (Changing the return type of doubleNumber is not a valid solution.)

  • The provided code is my solution, stripped of the details needed to make it work. It...

    The provided code is my solution, stripped of the details needed to make it work. It is not a “good” program. It lives in a single file, does not use classes, and it has those evil global variables. That is by design. I want to to craft code. Use my code as a guide to help you put together the needed parts. #include #include #include // defaults const int MAX_STEPS = 100; // how long do we run the simulation...

  • Question 19 4 pts Using the program below, please complete the program by adding 2 functions...

    Question 19 4 pts Using the program below, please complete the program by adding 2 functions as follow: 1. A function named getAverage that calculates and returns the Average. 2. A function named getMaximum that returns the maximum of the three numbers. Make sure to use the proper data types for all variables and functions. #include <iostream> using namespace std; //function getAverage //function getMaximum int main(int argc, char** argv) { int x, y, z; cout << "Enter three whole numbers:...

  • I need help to fix this program it is written in c++ and needs to run...

    I need help to fix this program it is written in c++ and needs to run in visual studio. Program uses character strings from a file containing on the first line and answer key and every other line after is the students ID number followed by a space then their test answers.Program currently reads only test answer key and student ID and their answers of second line.  Sample data can be found below. TFTTFTFFFTTTFFTTFFFT HUB00123 TFTFFFTFTFTFTTTFTTTT SMU12456 FFTFTTFFTTTTTTFFTTFT #include #include #include...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs ca...

    I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs can be found at compile time, while others are found at run time. Access the UserMenu.cpp code file (in the zip file in Start Here) and use debugging practices and the debugger in Visual Studio to find each bug. Fix the bug and write a comment in the code giving a description of how you found the...

  • Fix the following code to print whether the input is positive or negative #include <iostream> #include...

    Fix the following code to print whether the input is positive or negative #include <iostream> #include <string> using namespace std; void positiveOrNegative(int); // declare function int main(){    int y; int result; cin >> y; result = positiveOrNegative(y);    return 0; } void positiveOrNegative(int x){ if (x>=0) cout << "Positive"; else cout << "Negative"; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT