Solved

why  run time error on execution code

Posted on 2011-09-16
9
294 Views
Last Modified: 2012-06-27
#include<stdio.h>
main()
{
         int *p=10;
        printf("%d",*p);
       getch();
}
0
Comment
Question by:nagaharikola
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
Comment Utility
What is supposed to be located at address 10 ?
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Maybe you meant :
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = (int*) calloc(1, sizeof(int));
    *p = 10;
    printf("%d\n", *p);
    free(p);
    return 0;
}

Open in new window

0
 
LVL 4

Expert Comment

by:stachenov
Comment Utility
Because you assign a numeric value to a pointer. A pointer is a variable holding a memory address. You set it to point to a place with address "10". Since this address is probably invalid, it means that your variable points to some random place in memory or even doesn't point anywhere. Then you try to access this invalid address, so it fails. Any modern complier should issue a warning on code like this.

If you wanted the pointer to point to a varable holding the number 10, you should have created that variable first:
 
#include<stdio.h>
main()
{
       int n = 10;
       int *p=&n;
       printf("%d",*p);
       getch();
}

Open in new window

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
Comment Utility
I'm pretty confident you didn't compile this program in Linux.  In Linux, after some modifications, this program will produce a segmentation fault.  The Wikipedia article on segmentation faults might help you to understand.

http://en.wikipedia.org/wiki/Segmentation_fault
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
@hmccurdy : a segmentation fault is a runtime error ;)
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
Comment Utility
*after some modifications == means that I needed to modify the program just to get it to compile.  Changing getch() to getchar() and including <stdio.h> were the modifications I made.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
Comment Utility
Infinity, I know that a segmentation fault an element in the set of runtime errors..  I just didn't explicitly say that I knew.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
I probably misunderstood you ... I linked the first two phrases in your post together ... ie. that you said he didn't compile under Linux because he'd have had a segmentation fault if that were the case. But it's possible that those two phrases weren't related to each other.
0
 

Author Closing Comment

by:nagaharikola
Comment Utility
thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now