Pointer array contents wiped when passed to a function in C -


so loading lines of floats text files , storing them in pointer array, before saving them text file , adding reference size. number of values in text file varies array must dynamic. define pointer array in main this.

size_t size = (int)100 * sizeof(float); float * val = malloc(size); 

i pass pointer array function loads text file , saves values it, this.

//read file array. int readfile(float *val, int size) {  char buf[20] = { 0 }; val[0] = 0; double temp = 0; int = 1; file *file; file = fopen("c:\\users\\moldoffice\\dropbox\\vs\\finalproj\\ecgproject\\datastream", "r"); if (!file) {     printf("coulding find file.\n");     exit(1); }  while (fgets(buf, 20, file) != null) {     temp = atof(buf);     if (temp != 0) {         // increment find size of useful data.         val[i] = temp;         //printf("%d",val[i]);         i++;         if (i == size / sizeof(float)) {             size += 100*sizeof(float);             double* val_temp = realloc(val, size);             if (val_temp == null) {                 printf("realloc failed.\n");             }             else {                 val = val_temp;             }         }     } } //test array readable. (int = 0; < 5; i++) printf("val[%d]=%f\n", i, val[i]);  return(i); fclose(file); 

this works fine , when print contents of pointer array in main, works. pass same pointer array function saves array in new text file, along size on first line, problem when pass pointer array second time, contents have changed (mostly 0 random numbers). have absolutely no idea why happening.. ideas? function writes file here:

// write file array. void writefile(float *val,int size) {  printf("%d",sizeof(val)); file *file; int samplenum; char buf[10]; file = fopen("samplenum.txt", "r"); if (file == null) { samplenum = 0; } else { fscanf(file, "%d", &samplenum); printf("%d",samplenum);} char filestring[10]; sprintf(filestring,"sample%d\0", samplenum); file = fopen(filestring, "w");  //test array readable. (int = 0; < 5; i++) printf("val[%d]=%f\n", i, val[i]);  //print array text file , save. fprintf(file, "%d\n", size); (int = 1; < size; i++) {     fprintf(file, "%f\n", val[i]);      printf("%f\n", val[i]); } fclose(file); } 

the rest of main can found here:

int main() {  size_t size = (int)100 * sizeof(float); float * val = malloc(size);  // read data array. int arraysize = readfile(val, size);  //test array readable. (int = 0; < 5; i++) printf("val[%d]=%f\n", i, val[i]);  // save array text file, size of array first element. writefile(val,arraysize);  } 

        double* val_temp = realloc(val, size);         if (val_temp == null) {             printf("realloc failed.\n");         }         else {             val = val_temp; 

the caller of function has no way know you've moved array different place. it's still got old, invalid, pointer.

you have similar problem size. how caller know changed it?

you choice of division of responsibilities poor. if caller responsible allocating buffer, function should ask caller enlarge it. if function responsible allocating buffer, should allocate it. it's bad idea split responsibility managing allocation of chunk of memory, , shows 1 of reasons why.

perhaps pass in pointer structure contains pointer buffer , size? work, still shows poor division of responsibilities.

perhaps have function allocate buffer , return structure includes pointer , number of elements in it?

if want to things way, consider passing function pointer structure includes pointer array, size of array, , pointer function resizes array. caller can, of course, set pointer point realloc function (though it's better function changes pointer , size members of structure).

you use code this:

struct float_buffer {     float* buffer;     int size; };  struct float_buffer allocate_float_buffer(int size) {     struct float_buffer buf;     buf.buffer = malloc (size * sizeof(float));     buf.size = size;     return buf; }  bool resize_float_buffer(struct float_buffer* buf, int size) {     float* tmp = realloc(buf->buffer, size * sizeof(float));     if (tmp == null)         return false;     buf->buffer = tmp;     buf->size = size;     return true; } 

and pass function struct float_buffer *.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -