having a core dump

This is a discussion on having a core dump within the C Programming forums, part of the General Programming Boards category; the first code is iterate thu numbers and it works fine. when it come to strings it generate core dump ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    8

    having a core dump

    the first code is iterate thu numbers and it works fine. when it come to strings it generate core dump although both of them using same algorithm. the different is just the type.
    And i cant detect the core dump.
    thx

    this is the first code with integer which works fine
    Code:
    void smg(int outer[], int inner[]){
       int i,j;
       
       int match = 0;
       int outSize = 14;
       int inSize = 8;
       
       int ta = outer[0];
       int tb = inner[0];
       int gb = inner[0];
       
       i = j = 0;
       
       while( i < outSize && j < inSize){
     
          while(ta < gb){
             ta = outer[i++];
          }
      
          while(ta > gb){
             gb = inner[j++];
          }
    
          tb = gb;
          while(tb == gb){
             tb = gb;
    
             while(tb == ta){
                match++;
                tb = inner[j++];
               
             }
              ta = outer[i++];       
          }
          gb = tb;  
       }
       printf("> %d  %d %d",i,j, match);
    }
    
    
    void nj(int outer[], int inner[]){
       int i, j;
       int tmp;
       int match = 0;
       
       for(i = 0 ; i < 14 ; i++){
          tmp = outer[i];
          for(j = 0 ; j < 8 ; j++){
             if(tmp == inner[j])
                match++;
          }
       }  
    }
    
    
    int main(){
    
    
       int outer[] ={ 
          1,2,3,4,5,6,7,8,9,10,11,12,13,14};
       
       int inner[] = {
          2,3,5,5,7,8,12,14
       };
    
       smg(outer, inner);
    
    }
    this is the second code with strings which generate core dump
    Code:
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    
    void smg(char *outer[], char*inner[], int innerSize, int outerSize){
       int i,j;
       char ta[21];
       char tb[21];
       char gb[21];
       char tmp[21];
       int match;
       strcpy(ta, outer[0]);
       strcpy(tb, inner[0]);
       strcpy(gb, inner[0]);
       
       i = j = 0;
        match = 0;
       while(i < outerSize && j < innerSize){
       
          while(strcmp(ta,gb) < 0){
             
             strcpy(tmp, outer[i++]);
             strcpy(ta, tmp);
          }
          
          while(strcmp(ta, gb) > 0){
             
             strcpy(tmp, inner[j++]);
             strcpy(gb, tmp);
          }
          
          strcpy(tb,gb);
          
          while(strcmp(tb,gb) == 0){
             strcpy(tb,gb);         
             
             while(strcmp(tb,ta) == 0){
                match++;
                
                strcpy(tmp, inner[j++]);
                strcpy(tb,tmp);
             }
             
             strcpy(tmp, outer[i++]);
             strcpy(ta, tmp);
          }
          strcpy(gb,tb); 
       }
       
       printf("> %d  %d %d",i,j, match);
    }
    
    int main(){
       int match = 0;
       int innerSize = 8;
       int outerSize = 14;
    
       
       char* innerRel[innerSize];
       char* outer[outerSize];
       
       int i;
       
       for(i = 0 ; i < innerSize ; i++){
          innerRel[i] = malloc(sizeof(char) * 21); 
            
       }
      
       for(i = 0; i < outerSize ; i++){
          outer[i]  = malloc(sizeof(char) * 21);
      
       }
    
       strcpy(innerRel[0],"2...................\n");
       strcpy(innerRel[1],"3...................\n");
       strcpy(innerRel[2],"5...................\n");
       strcpy(innerRel[3],"5...................\n");
       strcpy(innerRel[4],"7...................\n");
       strcpy(innerRel[5],"8...................\n");
       strcpy(innerRel[6],"12..................\n");
       strcpy(innerRel[7],"14...................\n");
       
       strcpy(outer[0],"1...................\n" );
       strcpy(outer[1],  "2...................\n");
       strcpy(outer[2],  "3...................\n");
       strcpy(outer[3],  "4...................\n");
       strcpy(outer[4],  "5...................\n");
       strcpy(outer[5],  "6...................\n");
       strcpy(outer[6],  "7...................\n");
       strcpy(outer[7],  "8...................\n");
       strcpy(outer[8],  "9...................\n");
       strcpy(outer[9],  "10..................\n");
       strcpy(outer[10],  "11..................\n");
       strcpy(outer[11],  "12..................\n");
       strcpy(outer[12],  "13..................\n");
       strcpy(outer[13],  "14..................\n");
       
      
       smg(outer, innerRel, innerSize,outerSize);
    
       
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to have at least 22 characters in your malloc'd memory if you want to store a 21 length string. You need space for the '\0' at the end too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Aside from what mats pointed out, this is also wrong:
    Code:
    	strcpy(innerRel[7],"14...................\n");
    It's 23 chars, not 22, so buffer overrun yet again.

    Code:
    	char ta[21];
    	char tb[21];
    	char gb[21];
    	char tmp[21];
    These buffers are too small (should be 22).

    The loop
    Code:
    		while(strcmp(ta, gb) > 0){
    
    			strcpy(tmp, inner[j++]);
    			strcpy(gb, tmp);
    		}
    Also gives an access violation due to (probably) out of bounds access.
    It's best to get a debugger and do this right. Perhaps, if you use Windows, even use VS to use the safe strcpy_s function. It will assist greatly in eliminating bugs.
    Last edited by Elysia; 05-21-2008 at 12:00 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Perhaps even VS to use the safe strcpy_s function. It will assist greatly in eliminating bugs.
    Does this mean that you are offering to port Visual Studio to whatever Linux/Unix variant that the OP is using?

    You don't, generally, get core-dumps in Windows (unless you are using something like Cygwin - and somehow I doubt that is the case).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    I'm not aware of how Linux works and since no OS is specified, I guess it wouldn't hurt to try specifying that.
    Otherwise, inventing strcpy_s yourself isn't difficult.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I'm not aware of how Linux works and since no OS is specified, I guess it wouldn't hurt to try specifying that.
    Otherwise, inventing strcpy_s yourself isn't difficult.
    I didn't say that - I just suggested that you may look for hints of the OS before you lead someone down a path that doesn't work. Or at least qualify the statement with "if you are using Windows you could ..."

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Good idea. I'll definitely consider that!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird core dump
    By bean66 in forum C++ Programming
    Replies: 5
    Last Post: 04-01-2009, 05:29 PM
  2. hi to over come with core dump
    By vijay85 in forum C Programming
    Replies: 9
    Last Post: 01-14-2009, 10:59 AM
  3. STL vector core dump
    By creativeinspira in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2007, 04:49 PM
  4. Core Dump in While()
    By KrepNatas in forum C Programming
    Replies: 5
    Last Post: 05-17-2005, 11:15 AM
  5. core dump
    By kermit in forum Linux Programming
    Replies: 0
    Last Post: 08-03-2004, 06:25 PM

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