473,406 Members | 2,843 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,406 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 1779
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.