0

I am trying to write a program to use these functions from the header BV.h. I keep getting an error when trying to use setBit, clrBit, valBit, and printV.

I believe it has to do with the [pos] part of the code, but I am not really sure.

bv.c

//bv.c  
#include <stdio.h>
#include "bv.h"
#include <stdlib.h>

bitV* newVec(uint32_t length){
    bitV* v = malloc(sizeof(bitV));
    (*v).l = length;
    return v;
}

void delVec(bitV* v){
    free(v);
//  *v = NULL;
}

void oneVec(bitV* v){
    for(int i = 0;(unsigned)i < (v->l); i++){
        (*v).head = 1;
    }
}

void setBit(bitV* v, uint32_t pos){
    (*v).head[pos] = 1;
}

void clrBit(bitV* v, uint32_t pos){
    v->head[pos] = 0;
}

uint8_t valBit(bitV* v, uint32_t pos){
    return v->head[pos];
}

uint32_t lenVec(bitV* v){
    return v->l;
}

void printV(bitV* v){
    for(int i = 0;(unsigned) i < (v->l); i++){
        printf("%d\n",valBit(v,i));
    }
}

bv.h

// bv.h — Bit Vector interface


# ifndef _BVector
# define _BVector
# include <stdint.h>

typedef struct bitV {
    uint8_t *head;
    uint32_t l;
} bitV;

bitV *newVec(uint32_t);

void delVec(bitV *);

void oneVec(bitV *);

void setBit(bitV *, uint32_t);

void clrBit(bitV *, uint32_t);

uint8_t valBit(bitV *, uint32_t);

uint32_t lenVec(bitV *);

void printV(bitV *);
# endif
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70

1 Answers1

0

Your newVec(*) and delVec(*) must have some allocation and freeing of the actual data.

bitV* newVec(uint32_t length)
{
    bitV* v = malloc(sizeof(bitV));
    v->l = length;
    v->head = malloc(sizeof(uint8_t)*length);
    return v;
}

void delVec(bitV* v)
{
    if(v->head!=NULL) free(v->head);
    v->l = 0;
}
  • There is NO need to cast the return of `malloc`, it is unnecessary. See: [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714) for thorough explanation. – David C. Rankin Apr 26 '17 at 02:04