Solved

why  run time error on execution code

Posted on 2011-09-16
9
299 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
ID: 36548653
What is supposed to be located at address 10 ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 36548667
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
ID: 36548679
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36549898
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
 
LVL 53

Expert Comment

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

Expert Comment

by:Hugh McCurdy
ID: 36549915
*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
ID: 36550237
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
ID: 36551029
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
ID: 36565850
thanks
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

773 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