Thread: realloc inside a routine

  1. #1
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37

    realloc inside a routine

    I wish to increase the size of an array inside a function, since I don't know the size of the array before I call the function.

    I would think I could do (for example):

    Code:
    char log_filename[100];
    double *buf = NULL;
    int buf_len;
    
    sprintf(log_filename, "log.txt");
    
    fill_array(log_filename, buf, &buf_len);
    fprintf(stderr, "buf[0]=%f\n", buf[0]);
    
    void fill_array(char *log_filename, double *buf, int *buf_len) {
      int i = 0;
      FILE *fd;
      double time;
    
      fd = fopen(log_filename, "r");
    
      while (fscanf(fd_cam, "%lf\n", &time) != EOF) {
        times = (double *) realloc(times, (i + 1) * sizeof(double));
        times[i] = time;
        i++;
      }
    
    }
    But I get a seg fault, cause the memory is only accessible inside the fill_array() routine. How can I write such a routine, so it is accessible outside the routine. Thx.
    Last edited by coletek; 06-04-2009 at 08:56 PM. Reason: set log_filename to a path
    "What comes around, goes around"

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are those statements in main? If so, realloc is going to fail hard when you pass it the garbage pointer buf; make sure it's NULL before you send it off.

  3. #3
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37
    Quote Originally Posted by tabstop View Post
    Are those statements in main? If so, realloc is going to fail hard when you pass it the garbage pointer buf; make sure it's NULL before you send it off.
    The routine is in another .c file and the calls to it are in a main() routine set. Except the buf variable is a global variable.

    And yes the buf is set to NULL first. Sorry forgot to add that in my example.
    "What comes around, goes around"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The function can only change its local copy of buf (presumably = "times" in your example). If you want to be able to change the value of buf, you must pass a pointer to it.

  5. #5
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37
    Quote Originally Posted by tabstop View Post
    The function can only change its local copy of buf (presumably = "times" in your example). If you want to be able to change the value of buf, you must pass a pointer to it.
    But I have passed it a pointer.

    Or do you mean doing?:

    Code:
    char log_filename[100];
    double *buf = NULL;
    int buf_len;
    
    sprintf(log_filename, "log.txt");
    
    fill_array(log_filename, &buf, &buf_len);
    fprintf(stderr, "buf[0]=%f\n", buf[0]);
    
    void fill_array(char *log_filename, double **buf, int *buf_len) {
      int i = 0;
      FILE *fd;
      double time;
    
      fd = fopen(log_filename, "r");
    
      while (fscanf(fd_cam, "%lf\n", &time) != EOF) {
        *times = (double *) realloc(*times, (i + 1) * sizeof(double));
        (*times)[i] = time;
        i++;
      }
    
    }
    That works. Thanks...
    Last edited by coletek; 06-04-2009 at 09:04 PM. Reason: Fixed bug, it doesn't seg fault now.
    "What comes around, goes around"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call to realloc() inside a function: memory problem
    By simone.marras in forum C Programming
    Replies: 15
    Last Post: 11-30-2008, 10:01 AM
  2. variables when declared inside or outside a function
    By jas_atwal in forum C Programming
    Replies: 6
    Last Post: 12-14-2007, 02:42 PM
  3. Still battling with Copy Control
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2006, 08:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 06:48 AM