473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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

Jun 23 '07 #1
6 1776
On Jun 23, 11:02 am, Chad <cdal...@gmail.comwrote:
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.

Jun 23 '07 #2
Chad <cd*****@gmail.comwrites:
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) ks***@mib.org <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"
Jun 23 '07 #3
Keith Thompson wrote:
Chad <cd*****@gmail.comwrites:
>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'.
Jun 23 '07 #4
On Jun 23, 12:51 pm, Keith Thompson <k...@mib.orgwrote:
Chad <cdal...@gmail.comwrites:
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.

Jun 23 '07 #5

"Chad" <cd*****@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
On Jun 23, 12:51 pm, Keith Thompson <k...@mib.orgwrote:
>Chad <cdal...@gmail.comwrites:
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.
Jun 24 '07 #6
On Jun 23, 6:26 pm, "Barry" <bar...@nullhighstream.netwrote:
"Chad" <cdal...@gmail.comwrote in message

news:11**********************@i38g2000prf.googlegr oups.com...
On Jun 23, 12:51 pm, Keith Thompson <k...@mib.orgwrote:
Chad <cdal...@gmail.comwrites:
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

Jun 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Anon Email | last post by:
In the code below, what does this mean? mine = (short *)0; -------------- #include <iostream> int main() {
11
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698)...
61
by: John Baker | last post by:
When declaring an integer, you can specify the size by using int16, int32, or int64, with plain integer being int32. Is integer the accepted default in the programming community? If so, is...
3
by: LCAdeveloper | last post by:
Help! A trawl through the archives couldn't shed any light on this, so is there a way to handle DoubleClick events for RadioButtons in vb.NET? I'm recoding a VB4 application, which used the...
12
by: Tiny Tim | last post by:
Hope someone can help me to overcome this problem. Case 1 is working perfectly working with one number entry. Case 2 is the problem when I tried with 2 numbers entry. Case 1. #include...
7
by: Kevin Frey | last post by:
Using .NET 1.1. We have a mixed-mode assembly written in Managed C++ that we are using from an ASP.NET application that has been coded using C#. The mixed-mode assembly has its own...
5
by: Chad | last post by:
my input file is: test my program: #include <stdio.h> #include <stdlib.h> #define LOG "/home/miss_xtc/flood/words2.txt" #define BUFF 20
1
by: Jonathan Wilson | last post by:
I have a closed source app. I have a .dll plugin for this app (which I am writing). This plugin contains a bug somewhere which seems to clobber memory in a "random" fashion (as in, its not...
0
by: Steve Holden | last post by:
Hank @ITGroup wrote: It doesn't really make that much sense to watch memory usage as you have been doing. Your first test case appears to trigger a specific pathology, where the memory allocator...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.