I want to bound *float vec_value
(member of struct vec
) to be large a defined size. I do with malloc():
pattern_ptr->vec_value=malloc(dataset.mFeatures * sizeof(float))
To be sure the allocation is done correcty I expect to see a SegFault if VALUE>=dataset.mFeatures when:
printf("%f\n",pattern_ptr[N].vec_value[VALUE]);
But it compiles and prints the value in the adjacent area in memory.
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
struct vec{
int id_vec;
float *vec_value;
};
//prototipi
Dataset loadDataset(void); // struct { int nPattern; int mFeatures; float *dataset_ptr; }
int main(){
int i,j;
Dataset dataset=loadDataset();
struct vec *pattern_ptr=malloc(dataset.nPattern * sizeof(struct vec));
pattern_ptr->vec_value=malloc(dataset.mFeatures * sizeof(float));
for(i=0;i<dataset.nPattern;i++){
pattern_ptr[i].id_vec=i;
pattern_ptr[i].vec_value=&dataset.dataset_ptr[i*dataset.mFeatures];
}
printf("%f\n",pattern_ptr[1].vec_value[10]);
What's wrong?