core dump

This is a discussion on core dump within the C Programming forums, part of the General Programming Boards category; could anyone explain me why this program will generate a core dump ? something to do with lack of memory ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    21

    core dump

    could anyone explain me why this program will generate a core dump ? something to do with lack of memory allocation ? strcpy maybe ?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char * smd_ipv4_vers_decim(char *ipv4)
    {
      char chaine[4][4];
      long num[4];
      int  i;
      char *ch1, *ch2;
      char *tmp;
    
      ch2 = strchr(ipv4, ':')+1; 
      ch1 = strtok (ipv4,":"); 
    
         
      strcpy(ch[1], ch1 + (strlen(ch1) - 2));
      ch1[2] = '\0'; 
      strcpy(chaine[0],  ch1);
      strcpy(chaine[3], ch2 + (strlen(ch2) - 2));
      ch2[2] = '\0'; 
      strcpy(chaine[2], ch2);
    
      for (i=0 ; i<4 ; i++)
      {
        num[i] = strtol(chaine[i],NULL,16);
      }
    
      sprintf(tmp, "%i.%i.%i.%i", num[0], num[1], num[2], num[3]);
      return (tmp);
    
    }
    
    main()
    {
    
    char * src= "AC12:D374"; /* A C 1 2 : D 3 7 4 (no smiley !)*/
    char * result;
    
    result = smd_ipv4_vers_decim(src);
    printf("result: %s\n", result);
    
    return 0;
    }
    Last edited by lonbgeach; 04-07-2003 at 04:27 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Um, yes. You never allocate memory for Tmp.
    Regarding that smiley, there is an option to disable smileys when posting code. Just letting you know until next time (Or if you feel like modifying it).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Core Dump when closing the ifstream object- why?
    By Krsh in forum C++ Programming
    Replies: 11
    Last Post: 11-20-2008, 06:03 AM
  2. having a core dump
    By justins in forum C Programming
    Replies: 6
    Last Post: 05-21-2008, 01:00 PM
  3. STL vector core dump
    By creativeinspira in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2007, 05:49 PM
  4. Core Dump in While()
    By KrepNatas in forum C Programming
    Replies: 5
    Last Post: 05-17-2005, 12:15 PM
  5. core dump
    By kermit in forum Linux Programming
    Replies: 0
    Last Post: 08-03-2004, 07:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21