Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
I declare char ** arr and the routine parse in the include file
char **arr;
void parse( char *record, char *delim, char **tarr);

memory is allocated for arr in main

for (i = 0; i < 100; i ++){
if( (arr = (char*)calloc(12, sizeof(char))) == NULL)
printf("no memory allocated for arr\n");
}

still in main I get a line of char
fgets(tmp,1024,action);
and sent it to parse
parse(tmp,",",(char**) arr);

parse is defined in another c file and looks like this

Code:
void parse( char *record, char *delim, char **tarr)


{
	char *p;
	int fld=0;
	if ( (p = (char *) calloc(12, sizeof(char))) == NULL)
		printf(""no memory for p");
	p = strtok(record,delim);
	
		while(p)
	{	
		strcpy(tarr[fld],p);
		fld++;
		p=strtok('\0',delim);	
	}
	
	
}
I get a "could not access memory" error at strcpy(tarr[fld],p).

Somebody please tell me what's wrong?
thanks
 
memory error

This is from gdb but I don't know what to do with it.

#0 0x00007fffffe007c5 in __memcpy ()
#1 0x0000000100001708 in __inline_strcpy_chk (__dest=0x100007673 <Address 0x100007673 out of bounds>, __src=0x100800000 "20090813 ") at _string.h:91
#2 0x000000010000169b in parse (record=0x100800000 "20090813 ", delim=0x100001cbf ",", tarr=0x1001000c0) at /Users/doug/ANDY/C/trading/tradingroutines.c:181
#3 0x0000000100000fa5 in main (argc=2, argv=0x7fff5fbfe580) at /Users/doug/ANDY/C/trading/main.c:54
 
char* strcpy(char* pDst, const char* pSrc);

The strcpy() function copies characters in the string 'pSrc' to the string 'pDst', including the null termination. The return value is 'pDst'. Note that strcpy() does not perform bounds checking, and thus risks overrunning from or to. For a similar (and safer) function that includes bounds checking, see strncpy().

You allocated space for 11 characters plus the zero terminator - is that correct?
 
This is from gdb but I don't know what to do with it.

#0 0x00007fffffe007c5 in __memcpy ()
#1 0x0000000100001708 in __inline_strcpy_chk (__dest=0x100007673 <Address 0x100007673 out of bounds>, __src=0x100800000 "20090813 ") at _string.h:91
#2 0x000000010000169b in parse (record=0x100800000 "20090813 ", delim=0x100001cbf ",", tarr=0x1001000c0) at /Users/doug/ANDY/C/trading/tradingroutines.c:181
#3 0x0000000100000fa5 in main (argc=2, argv=0x7fff5fbfe580) at /Users/doug/ANDY/C/trading/main.c:54

What you do with it is Break It Down.
http://www.cocoadev.com/index.pl?BreakItDown

Notice anything about the line labelled #1? What might "out of bounds" mean?

Notice anything about the out-of-bounds address itself? What ASCII characters do hex 73 and 76 represent?

Perhaps the __dest address isn't what you think it is, or the value from the array has been corrupted by something else going wrong earlier in the code.
 
Yes, I think that's right but I upped it to 14 + 1 with no change. Furthermore, the routine is used in another program without difficulty and with the same input. The only difference is in the other program arr is defined and memory for it allocated in the same routine that calls parse. Although I don't see it, I think I am passing pointers incorrectly. I shouldn't have to declare, allocate and call all in the same routine.

I've commented just about everything out of the code that I can think of. I open the file read the first line, read the second line and try to parse it. I can't parse the first line either for that matter.
 
Yes, I think that's right but I upped it to 14 + 1 with no change. Furthermore, the routine is used in another program without difficulty and with the same input. The only difference is in the other program arr is defined and memory for it allocated in the same routine that calls parse. Although I don't see it, I think I am passing pointers incorrectly. I shouldn't have to declare, allocate and call all in the same routine.

Create a minimal fail-case.

Since you also have code that works, create a minimal known-working case.

Post the code for both apps, along with sample data that makes the fail-case fail. The sample data can be fabricated, as long as it makes the fail-case fail.

If you're going to rely on other people to debug your code, you have to give them something debuggable. I see nothing inherently wrong in your posted code fragments that would directly cause the posted error. I see other bugs, but nothing inherently related to the immediate failure. If the problem isn't in the posted code, then the only place it could be is in the unposted code, or as a consequence of the unposted input data.
 
memory error

I was hoping it was something obvious that some one would catch. I'll try and put something together tomorrow.
Thanks.
 
char* strcpy(char* pDst, const char* pSrc);

The strcpy() function copies characters in the string 'pSrc' to the string 'pDst', including the null termination. The return value is 'pDst'. Note that strcpy() does not perform bounds checking, and thus risks overrunning from or to. For a similar (and safer) function that includes bounds checking, see strncpy().

You allocated space for 11 characters plus the zero terminator - is that correct?

Warning: Do NOT use strncpy. Instead of a buffer overflow it creates a trap for the unwary programmer - the result is a string without proper termination, which will lead to subtle and hard to find errors in your programs.

There is always the problem that it fills all the rest of the destination buffer with zeroes, which is very bad if you used a one megabyte buffer.
 
thanks guys.

I change parse from void to char ** and made the other necessary changes. That worked although I still don't know why the original code didn't.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.