473,803 Members | 3,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Learning C, need some help

Trying to implement a simple rrd. Code below. When calling rrd_write,
8 successful times in a row, the 9th one causes this to happen:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION _FAILURE at address: 0x00000001
0x00001f4f in rrd_write (rrd=0x3000f0) at rrd.c:81
81 rrd->value[rrd->position][i] = va_arg(ap, int);

I washed it through GDB a few times, and notice that starting from
rrd->value[8] I'm seeing strange memory addresses like 0xa and 0x1;
however, the calls to malloc() are clearly not failing.

I'm not sure what I'm doing wrong here.

The code:

--- cut here ---
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>

typedef struct rrd
{
short position; /* Last-used position */
short length; /* Length of RRD */
short width; /* Width of RRD */
uint32_t **value; /* Value */
} rrd_t;

/* The following functions work with in-memory structures */
rrd_t *rrd_create(sho rt length, short width);
int rrd_write(rrd_t *rrd, ...);

int main(int argc, char **argv)
{
rrd_t *rrd;

rrd = rrd_create(30, 2);

rrd_write(rrd, 1, 10);
rrd_write(rrd, 2, 10);
rrd_write(rrd, 3, 10);
rrd_write(rrd, 4, 10);
rrd_write(rrd, 5, 10);
rrd_write(rrd, 6, 10);
rrd_write(rrd, 7, 10);
rrd_write(rrd, 8, 10);
rrd_write(rrd, 9, 10);
rrd_write(rrd, 10, 10);
rrd_write(rrd, 11, 10);
rrd_write(rrd, 12, 10);
rrd_write(rrd, 13, 10);
rrd_write(rrd, 14, 10);
rrd_write(rrd, 15, 10);
rrd_write(rrd, 16, 10);
rrd_write(rrd, 17, 10);
rrd_write(rrd, 18, 10);
rrd_write(rrd, 19, 10);
rrd_write(rrd, 20, 10);
rrd_write(rrd, 21, 10);
rrd_write(rrd, 22, 10);
rrd_write(rrd, 23, 10);
rrd_write(rrd, 24, 10);
rrd_write(rrd, 25, 10);
rrd_write(rrd, 26, 10);
rrd_write(rrd, 27, 10);
rrd_write(rrd, 28, 10);
rrd_write(rrd, 29, 10);
rrd_write(rrd, 30, 10);
rrd_write(rrd, 1, 7);

return(1);
}

rrd_t *rrd_create(sho rt length, short width)
{
rrd_t *rrd;
int i;

rrd = (struct rrd *)malloc(sizeof (struct rrd));

rrd->position = 0; /* Set the initial position to 0 */
rrd->length = length;
rrd->width = width;

/* Allocate enough memory for the entire rrd database */
rrd->value = malloc(length);

for (i = 0; i < length; i++)
{
rrd->value[i] = malloc(width);
if (!rrd->value[i])
{
fprintf(stderr, "%s\n", strerror(errno) );
exit(1);
}
}

return(rrd);
}

/* TODO: This needs some error checking */
int rrd_write(rrd_t *rrd, ...)
{
va_list ap;
va_start(ap, rrd);
int i;
static int j;

j++;

printf("J: %i\n", j);

// Write values
for (i = 0; i < rrd->width; i++)
{
rrd->value[rrd->position][i] = va_arg(ap, int);
}

// Check position
rrd->position++;
if (rrd->position (rrd->length - 1))
{
rrd->position = 0;
}

va_end(ap);

return(1);
}

Oct 2 '07 #1
7 1689
Richard Cranium <ci********@gma il.comwrites:
Trying to implement a simple rrd.
What is an rrd?

Here are a couple of obvious problems:
typedef struct rrd
{
short position; /* Last-used position */
short length; /* Length of RRD */
short width; /* Width of RRD */
uint32_t **value; /* Value */
} rrd_t;
....
rrd->value = malloc(length);
rrd->value = malloc(length * sizeof *rrd->value);
for (i = 0; i < length; i++)
{
rrd->value[i] = malloc(width);
rrd->value[i] = malloc(width * sizeof *rrd->value[i]);
if (!rrd->value[i])
{
fprintf(stderr, "%s\n", strerror(errno) );
exit(1);
}
}
--
"Give me a couple of years and a large research grant,
and I'll give you a receipt." --Richard Heathfield
Oct 2 '07 #2
"Richard Cranium" <ci********@gma il.comschrieb im Newsbeitrag
news:2007100213 514775249-cisrichard@gmai lcom...
Trying to implement a simple rrd. Code below. When calling rrd_write,
Whatever rrd might be...
8 successful times in a row, the 9th one causes this to happen:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION _FAILURE at address: 0x00000001
0x00001f4f in rrd_write (rrd=0x3000f0) at rrd.c:81
81 rrd->value[rrd->position][i] = va_arg(ap, int);

I washed it through GDB a few times, and notice that starting from
rrd->value[8] I'm seeing strange memory addresses like 0xa and 0x1;
however, the calls to malloc() are clearly not failing.

I'm not sure what I'm doing wrong here.

The code:

--- cut here ---
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>

typedef struct rrd
{
short position; /* Last-used position */
short length; /* Length of RRD */
short width; /* Width of RRD */
uint32_t **value; /* Value */
Where's that from? I find one in sdtint.h, which you did not #include
} rrd_t;

/* The following functions work with in-memory structures */
rrd_t *rrd_create(sho rt length, short width);
int rrd_write(rrd_t *rrd, ...);

int main(int argc, char **argv)
{
rrd_t *rrd;

rrd = rrd_create(30, 2);

rrd_write(rrd, 1, 10);
rrd_write(rrd, 2, 10);
rrd_write(rrd, 3, 10);
rrd_write(rrd, 4, 10);
rrd_write(rrd, 5, 10);
rrd_write(rrd, 6, 10);
rrd_write(rrd, 7, 10);
rrd_write(rrd, 8, 10);
rrd_write(rrd, 9, 10);
rrd_write(rrd, 10, 10);
rrd_write(rrd, 11, 10);
rrd_write(rrd, 12, 10);
rrd_write(rrd, 13, 10);
rrd_write(rrd, 14, 10);
rrd_write(rrd, 15, 10);
rrd_write(rrd, 16, 10);
rrd_write(rrd, 17, 10);
rrd_write(rrd, 18, 10);
rrd_write(rrd, 19, 10);
rrd_write(rrd, 20, 10);
rrd_write(rrd, 21, 10);
rrd_write(rrd, 22, 10);
rrd_write(rrd, 23, 10);
rrd_write(rrd, 24, 10);
rrd_write(rrd, 25, 10);
rrd_write(rrd, 26, 10);
rrd_write(rrd, 27, 10);
rrd_write(rrd, 28, 10);
rrd_write(rrd, 29, 10);
rrd_write(rrd, 30, 10);
rrd_write(rrd, 1, 7);

return(1);
}

rrd_t *rrd_create(sho rt length, short width)
{
rrd_t *rrd;
int i;

rrd = (struct rrd *)malloc(sizeof (struct rrd));
Drop the cast. Why using sizeof(struct rrd) rather than sizeof(rrd_t)?
Check for malloc's return value before writing to it.
rrd->position = 0; /* Set the initial position to 0 */
rrd->length = length;
rrd->width = width;

/* Allocate enough memory for the entire rrd database */
rrd->value = malloc(length);
Ah, no cast here, good. But check for return value. And allocate enough
rrd->value = malloc(length * sizeof *rrd->value);
for (i = 0; i < length; i++)
{
rrd->value[i] = malloc(width);
Just like here! Again alloocate enough
rrd->value[i] = malloc(width * sizeof *rrd->value[i]);
if (!rrd->value[i])
{
fprintf(stderr, "%s\n", strerror(errno) );
strerror prototype missing. #include <string.h>
perror() might be better/easier to use here?
exit(1);
}
}

return(rrd);
}

/* TODO: This needs some error checking */
int rrd_write(rrd_t *rrd, ...)
{
va_list ap;
va_start(ap, rrd);
int i;
static int j;

j++;
This doesn't work in C89/C90, move va_start(ap, rrd);
down a couple of lines
printf("J: %i\n", j);

// Write values
Neither do these comments. Also they may break in the net
for (i = 0; i < rrd->width; i++)
{
rrd->value[rrd->position][i] = va_arg(ap, int);
}

// Check position
rrd->position++;
if (rrd->position (rrd->length - 1))
{
rrd->position = 0;
}

va_end(ap);

return(1);
}
you malloc() but don't free(), relying that the end of main will sort it for
you, fair enough in this small thing, but otherwise bad practise.
Bye, Jojo
Oct 2 '07 #3
See below, and thanks, that seems to have fixed it up.

On 2007-10-02 14:17:44 -0400, Ben Pfaff <bl*@cs.stanfor d.edusaid:
Richard Cranium <ci********@gma il.comwrites:
>Trying to implement a simple rrd.

What is an rrd?
Round Robin Database. It's basically a method of storage for
time-series data which guarantees a constant storage size.

When using time-series data to compute aveages, the oldest value is
worthless to the average and can be discarded. So the array of values
is used written to on a round-robin basis. We use a position indicator
to tell us where in the array to go to next. The one-off from our
current position is always the oldest value. Once we hit the bottom of
the array, we start writing to the top of it again.

Thus storage size is always constant.

Real-time representation of this data (usually in graph form) is
typically of an average of set number of probes.

Historical data can be stored and computed in the same manner,
basically "Averages of Averages"

For example:

30 Second average: 1 probe, 1 second apart, average the data. Requires
a 30 * sizeof(value) storage space.

5 Minute average is 10 30 second averages, requires a 10* sizeof(value)
storage array

2 hour average is 24 5 minute averages, requires a 24*sizeof(value )
storage array

etc,
etc,
etc.
>
Here are a couple of obvious problems:
>typedef struct rrd
{
short position; /* Last-used position */
short length; /* Length of RRD */
short width; /* Width of RRD */
uint32_t **value; /* Value */
} rrd_t;

...
> rrd->value = malloc(length);

rrd->value = malloc(length * sizeof *rrd->value);
> for (i = 0; i < length; i++)
{
rrd->value[i] = malloc(width);

rrd->value[i] = malloc(width * sizeof *rrd->value[i]);
> if (!rrd->value[i])
{
fprintf(stderr, "%s\n", strerror(errno) );
exit(1);
}
}

Oct 2 '07 #4
Richard Cranium wrote, On 02/10/07 18:51:
Trying to implement a simple rrd. Code below. When calling rrd_write,
8 successful times in a row, the 9th one causes this to happen:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION _FAILURE at address: 0x00000001
0x00001f4f in rrd_write (rrd=0x3000f0) at rrd.c:81
81 rrd->value[rrd->position][i] = va_arg(ap, int);

I washed it through GDB a few times, and notice that starting from
rrd->value[8] I'm seeing strange memory addresses like 0xa and 0x1;
however, the calls to malloc() are clearly not failing.
If you are using Linux I suggest running your program through valgrind.
You should also look at what other tools are available for debugging
memory access problems.
I'm not sure what I'm doing wrong here.

The code:

--- cut here ---
You've missed the inclusion of stdint.h without which this will not
compile. If you retyped, then don't use copy and paste. If you used copy
and paste, then please check that you did not introduce any other errors.

You also need string.h for strerror.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>

typedef struct rrd
{
short position; /* Last-used position */
short length; /* Length of RRD */
short width; /* Width of RRD */
uint32_t **value; /* Value */
} rrd_t;
<snip>
rrd_t *rrd_create(sho rt length, short width)
{
rrd_t *rrd;
int i;

rrd = (struct rrd *)malloc(sizeof (struct rrd));
Casting the result of malloc is rarely a good idea. Also there is a
generally far better way of determining what to pass to malloc. A lot of
people here recommend the following form for very good reasons:
rrd = malloc(sizeof *rrd);

In your program you should check that malloc succeeds. Otherwise if it
does fail the next line invokes undefined behaviour and is likely to
cause your program to crash. This applies to all the other malloc calls
as well.
rrd->position = 0; /* Set the initial position to 0 */
rrd->length = length;
rrd->width = width;

/* Allocate enough memory for the entire rrd database */
rrd->value = malloc(length);
Again, use
rrd->value = malloc(length * sizeof *rrd->value);
If you check you will probably find that sizeof *rrd->value is greater
than 1. You have allocated length bytes when you needed space for length
pointers.

As you can see, the patter often suggested protects you from this error.
It continues to protect you if you later change the type of rrd->value.
for (i = 0; i < length; i++)
{
rrd->value[i] = malloc(width);
Same problem again.
rrd->value[i] = malloc(length * sizeof *rrd->value[i]);
if (!rrd->value[i])
{
fprintf(stderr, "%s\n", strerror(errno) );
exit(1);
1 is not a portable value for exit, and on VMS will actually indicate
successful termination!
exit(EXIT_FAILU RE);
}
}

return(rrd);
}
<snip>
--
Flash Gordon
Oct 2 '07 #5
On Tue, 02 Oct 2007 11:17:44 -0700, in comp.lang.c , Ben Pfaff
<bl*@cs.stanfor d.eduwrote:
>What is an rrd?
Golly.
See for example

http://oss.oetiker.ch/rrdtool/

any anywebsite displaying ntop stats
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 2 '07 #6
In article <ua************ *************** *****@4ax.com>,
Mark McIntyre <ma**********@s pamcop.netwrote :
>>What is an rrd?

Golly.
See for example

http://oss.oetiker.ch/rrdtool/

any anywebsite displaying ntop stats
What is ntop?

-- Richard

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 3 '07 #7
Richard Tobin wrote, On 03/10/07 12:18:
In article <ua************ *************** *****@4ax.com>,
Mark McIntyre <ma**********@s pamcop.netwrote :
>>What is an rrd?
Golly.
See for example

http://oss.oetiker.ch/rrdtool/

any anywebsite displaying ntop stats

What is ntop?
Off topic ;-) Unless I miss my guess it is the first hit Google gives
for ntop.

The meaning of rrd is also off topic but had no bearing on the OPs problem.
--
Flash Gordon
Oct 3 '07 #8

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

Similar topics

0
2482
by: Sean | last post by:
While checking out opensourcecms.com - I found only 3 CMS - e-Learning tools. Are there more and has anybody used the 3.(Claroline - Moodle - Segue) I have has much success with PHPNUKE, Guppy, PHProjekt and others...But these 3 CMS were "unable to connect to MYSQL". Sure I looked and read all the docs & install files...I'm lost here - if anyone has used these or other CMS-e-Learning type tools - please let me know. I need to create an...
2
3477
by: ggg | last post by:
I'm looking for a complete project/application done with heavy use of of object-oriented programming & design. Preferably something well documented and/or commented so that I can pick it apart and learn how/why they designed it they way they did. Any suggestions?
29
3509
by: Jhon smith | last post by:
Hi,all,I was just wondering if I am likly to have any problems trying to learn C from older books,I have some from the late 80`s,mid/late 90`s. I am using Dev-C++ on the pc windows platform,But I have noticed small differnces in the books such as,int main(),main(void),fprintf,and others,just wondering if these older books are still worth trying to learn from as Im on a very tight budget and can`t really afford any thing else,or are they...
1
9661
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
26
2983
by: mfasoccer | last post by:
I am sorry if this is an inappropriate place to put this post, if so please delete it. I am wondering about a few things. Do you guys recommend learning C as a second language, as someone who already knows java very well. And what is the best way to learn C, books, tutorials, or what? Thanks, any response would be great.
7
2397
by: Hal Vaughan | last post by:
I have a problem with port forwarding and I have been working on it for over 2 weeks with no luck. I have found C programs that almost work and Java programs that almost work, but nothing that does what I need. I've even tried writing a port forwarder in Java and found problems that nobody seems to have the answer to in forums. I need to make it work essentially the same on both Windows and Linux. There is one program, in C, that...
1
1496
by: whosesocks | last post by:
I discovered that many of my very busy colleagues are having an extremely difficult time pulling themselves away to take vital training when it requires being away for consecutive days. I just completed training for .NET in Chicago through a mentored learning program that personally helped me to tackle that same issue. Though the mentored learning training allows complete interaction with classroom version labs and interaction with an on...
5
1775
by: romiro | last post by:
Hi all, I'm a PHP5 developer looking to "broaden my horizons" so to speak by learning a new language. I emphasize the 5 in PHP since I have fully engrossed myself in the full OOP of version 5 with my own ground-up projects as well as some work with PRADO (http://pradosoft.com) I've dabbled with a number of languages in the past, Python being no exception, but always ended up coming back to PHP due to being comfortable with it. Python...
16
1768
by: John Salerno | last post by:
Just something that crosses my mind every time I delve into "Learning Python" each night. Does anyone see any value in learning Python when you don't need to for school, work, or any other reason? I mean, sure, there's value in learning anything at any time, but for something like a programming language, I can't help but feel that I will be mostly unable to use what I learn simply because I have no reason to use it. The *process* of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10068
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7600
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.