Same code different result

Discussion in 'Mac Programming' started by farmerdoug, Jan 24, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    Two codes from different programs. Each should be exactly the same. No problems in one; the other fails at strcpy(arr[fld],p) in parse which runs when it is commented out. Do see anything I have missed? Any suggestions on how to debug? Could it be entirely unrelated to this even though that's where it fails.


    from one program:
    Code:
    
    arr = (char **)calloc(100, sizeof(char*));
    		for(j = 0; j < 100; j++)
    			arr[j] = (char *)calloc(10,sizeof(char));
    
    fscanf(fp, "%s\n", listname); //get first file
    		action = fopen(listname, "r"); //open first file
    		fscanf(action, "%s\n", line); // discard first lineprintf("%s\n",  arr[i]);
    		
    		printf("%s\n", listname);
    		while (fgets(tmp,1024,action) != 0)
    		{
    		parse(tmp,",",(char**)arr,&fldcnt); // whack record into fields 
    
    
    
    
    void parse( char *record, char *delim, char **arr,int *fldcnt)
    
    {
    
    	char* p=strtok(record,delim);
    	int fld=0;
    	while(p)
    	{
    		strcpy(arr[fld],p);
    		fld++;
    		p=strtok('\0',delim);	
    	}
    
    
    }
    
    
    from another code

    Code:
    
    	arr = (char **)calloc(100, sizeof(char*));
    	for (i = 0; i < 100; i ++)
    		arr[i] = (char*)calloc(10, sizeof(char));
    	
    		fscanf(fl, "%s\n", listname); //get first file
    		printf("%s\n", listname);
    		if ((action = fopen(listname, "r")) == NULL)
    			printf("can't open action\n");
    		fscanf(action, "%s\n", line); // discard first line
    	
    			while (fgets(tmp,1024,action) != 0)
    			{	
    				parse(tmp,",",(char**)arr,&fldcnt);
    
    void parse( char *record, char *delim, char **arr,int *fldcnt)
    
    
    {
    	
    	char* p=strtok(record,delim);
    	int fld=0;
    	while(p)
    	{printf("%d %s\n", fld, arr[fld]);
    		//strcpy(arr[fld],p);
    		fld++;
    		
    		p=strtok('\0',delim);	
    	}
    	
    	
    }
    
    
     
  2. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #2
    If it's running when it's commented out, then the code you're changing isn't the code that's being run--even if it's where the debugger points you.


    Clean and rebuild.
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    same code

    Thanks.
    It turned out to be memory allocation errors.
     

Share This Page