Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > I don't see where I'm clobbering the memory

Reply
Thread Tools

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

 
 
Chad
Guest
Posts: n/a
 
      06-23-2007
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

 
Reply With Quote
 
 
 
 
Chad
Guest
Posts: n/a
 
      06-23-2007
On Jun 23, 11:02 am, Chad <cdal...@gmail.com> wrote:
> 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.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-23-2007
Chad <> writes:
> 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?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a
 
      06-23-2007
Keith Thompson wrote:
> Chad <> writes:
>> 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'.
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      06-23-2007
On Jun 23, 12:51 pm, Keith Thompson <k...@mib.org> wrote:
> Chad <cdal...@gmail.com> writes:
> > 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.



 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      06-24-2007

"Chad" <> wrote in message
news: oups.com...
> On Jun 23, 12:51 pm, Keith Thompson <k...@mib.org> wrote:
>> Chad <cdal...@gmail.com> writes:
>> > 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.


 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      06-24-2007
On Jun 23, 6:26 pm, "Barry" <bar...@nullhighstream.net> wrote:
> "Chad" <cdal...@gmail.com> wrote in message
>
> news: oups.com...
>
>
>
> > On Jun 23, 12:51 pm, Keith Thompson <k...@mib.org> wrote:
> >> Chad <cdal...@gmail.com> writes:
> >> > 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.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SimpleXMLRPCServer clobbering sys.stderr? (2.5.2) Joseph Turian Python 4 10-21-2009 12:48 PM
I don't see where I'm clobbering the memory Chad C Programming 5 11-20-2006 05:07 AM
How Do I Figure Out What's Clobbering My Sound? jim evans Computer Support 1 07-20-2006 06:08 PM
Desktop can see Laptop, but Laptop can't see Desktop! Plomaris Wireless Networking 3 02-17-2006 01:41 PM
qsort clobbering memory location William Buch C Programming 11 01-30-2004 11:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57