R
Richard Cranium
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] = 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(short 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(short 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 = malloc(width);
if (!rrd->value)
{
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] = va_arg(ap, int);
}
// Check position
rrd->position++;
if (rrd->position > (rrd->length - 1))
{
rrd->position = 0;
}
va_end(ap);
return(1);
}
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] = 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(short 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(short 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 = malloc(width);
if (!rrd->value)
{
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] = va_arg(ap, int);
}
// Check position
rrd->position++;
if (rrd->position > (rrd->length - 1))
{
rrd->position = 0;
}
va_end(ap);
return(1);
}