c code is dumping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jagadeesha
    New Member
    • Aug 2008
    • 4

    c code is dumping

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main () {
        char *temp;
        int size = 1396899840;
        temp = (char *) malloc (size);
    
        if (!temp) {
            fprintf (stderr, "Error in allocating memory");
            exit (-1);
        }
        memcpy (temp, "Jagadeesha",size);
        printf ("temp : %s", temp);
    }
  • jagadeesha
    New Member
    • Aug 2008
    • 4

    #2
    what is solution for this

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      At line 14 don't try to copy 1396899840 from a data object that is only 11 bytes long. As soon as you access data outside the valid object boundaries your program invokes underfined behaviour one of the possibilities of which is a program crash.

      Comment

      Working...