C
Chad
When the following functions takes the string "this is a string"
static void *build_string(char *s)
{
int k;
char *start;
size_t len;
size_t max;
len = strlen(s);
if(len == 0) {
fprintf(stderr,"Zero length");
exit(EXIT_FAILURE);
}
max = 1024/len;
char *p = malloc((max*len) + sizeof(p));
if(p == NULL){
fprintf(stderr, "Out of memory\n");
return NULL;
}
start = p;
for(k=0; k < max; k++){
memcpy(p, s, len);
p += len;
}
*p = '\0';
return start;
}
I get the following on my output
ou
this is a string
ou
this is a string
ou
this is a string
ou
this is a string
ou
this is a string
ou
this is a string
The ou is interlaced with my output. I figure I might be clobbering my
memory. Ideas?
Chad
static void *build_string(char *s)
{
int k;
char *start;
size_t len;
size_t max;
len = strlen(s);
if(len == 0) {
fprintf(stderr,"Zero length");
exit(EXIT_FAILURE);
}
max = 1024/len;
char *p = malloc((max*len) + sizeof(p));
if(p == NULL){
fprintf(stderr, "Out of memory\n");
return NULL;
}
start = p;
for(k=0; k < max; k++){
memcpy(p, s, len);
p += len;
}
*p = '\0';
return start;
}
I get the following on my output
ou
this is a string
ou
this is a string
ou
this is a string
ou
this is a string
ou
this is a string
ou
this is a string
The ou is interlaced with my output. I figure I might be clobbering my
memory. Ideas?
Chad