I don't see where I'm clobbering the memory

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
 
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


Never mind. I forgot to add a '\0' to the string s.
 
K

Keith Thompson

Chad said:
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));
[snip]

You already mentioned the lack of space for the '\0', but why the
"sizeof(p)" term? Why do you need to allocate space for a pointer in
a character array?
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Keith said:
Chad said:
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));
[snip]

You already mentioned the lack of space for the '\0', but why the
"sizeof(p)" term? Why do you need to allocate space for a pointer in
a character array?

I suspect he meant sizeof(*p), for the terminating '\0'. Chad mentioned the
function argument was not terminated, but given correct input, build_string
does correctly add a '\0'.
 
C

Chad

Chad said:
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));

[snip]

You already mentioned the lack of space for the '\0', but why the
"sizeof(p)" term? Why do you need to allocate space for a pointer in
a character array?

--

At the time, I was trying to allocate space for the '\0'. However,
after I stopped to think about it, I realized that this might have
been a tad bit boneheaded. So I changed

char *p = malloc((max*len) + sizeof(p));

to

char *p = malloc((max*len) + 1);

Now, I want to make a passing comment to the people that are thinking
"Oh sweet lord, this is hobbyist code." The actual build_string()
function is a bit more complex. What I posted was a stripped down
version of the actual build_string() function.
 
B

Barry

Chad said:
Chad said:
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));

[snip]

You already mentioned the lack of space for the '\0', but why the
"sizeof(p)" term? Why do you need to allocate space for a pointer in
a character array?

--

At the time, I was trying to allocate space for the '\0'. However,
after I stopped to think about it, I realized that this might have
been a tad bit boneheaded. So I changed

char *p = malloc((max*len) + sizeof(p));

to

char *p = malloc((max*len) + 1);

Now, I want to make a passing comment to the people that are thinking
"Oh sweet lord, this is hobbyist code." The actual build_string()
function is a bit more complex. What I posted was a stripped down
version of the actual build_string() function.

Folks here reply to C questions. Providing the simple version
was the best way to get an accurate answer!

Of course responses have to assume there are not any nuances
your simple version has failed to show.

In a passing reply to you, I would suggest learning the debugger
for your set of tools.
 
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));
[snip]
You already mentioned the lack of space for the '\0', but why the
"sizeof(p)" term? Why do you need to allocate space for a pointer in
a character array?
--
At the time, I was trying to allocate space for the '\0'. However,
after I stopped to think about it, I realized that this might have
been a tad bit boneheaded. So I changed
char *p = malloc((max*len) + sizeof(p));

char *p = malloc((max*len) + 1);
Now, I want to make a passing comment to the people that are thinking
"Oh sweet lord, this is hobbyist code." The actual build_string()
function is a bit more complex. What I posted was a stripped down
version of the actual build_string() function.

Folks here reply to C questions. Providing the simple version
was the best way to get an accurate answer!

Of course responses have to assume there are not any nuances
your simple version has failed to show.
In a passing reply to you, I would suggest learning the debugger
for your set of tools.

I normally use a debugger on regular basis. I just had a brain lapse
when I posted the question, Shortly after the post, I realized that I
inserted the break point in the wrong part of the code. Hence why I
didn't see one string clobbering the other string.

Chad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,371
Latest member
TroyHursey

Latest Threads

Top