Re: realloc() failing under DOS (why it does'nt ...)
- Posted by pseudonomus at yahoo.com Nov 08, 2001
- 479 views
--- Robert Craig <rds at RapidEuphoria.com> wrote: > > pseudonomus writes: > > s = (char*)Realloc(s, i); > > s[i-1] = 'X'; > > s[i] = 0; /* EOS */ > > In C, if s is a pointer to i bytes, then s[i] is > *not* a valid element. > The subscripts run from 0 to i-1. You are writing a > byte beyond > the allocated space. Sometimes this won't matter. > Other times > it will lead to disaster. Arrghh! That's a syntax error :p I posted the C code in question as a quick explenation of my point, yet I introduced a syntax error in the proccess. The code I've being having trouble with is the following; char *characterWeld(char *dest,char source) { int dl = strlen(dest); dest = (char*)realloc(dest,dl+2); dest[dl] = source; dest[dl+1] = 0; return dest; } main() { int i = 6000000; char *s = (char*)malloc(1); while(i--) s = characterWeld(s,'X'); /* Fails */ } Above code is correct for a change :p But ... > Buy the source in 2 weeks and learn > all > the tricks of the trade. I'll do just that. > Regards, > Rob Craig > Rapid Deployment Software > http://www.RapidEuphoria.com