pointers & arrays and realloc!

This is a discussion on pointers & arrays and realloc! within the C Programming forums, part of the General Programming Boards category; Hi..I need some help in using the realloc function in my program as the memory i need to use keeps ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    16

    pointers & arrays and realloc!

    Hi..I need some help in using the realloc function in my program as the memory i need to use keeps on changing based on some parameters.Do i first have to use the malloc function and then realloc?

    I need to know how to use malloc, but in my programme I am using arrays for example t and x both of which are arrays..When i try to declare them as t[],x[] as i dont know how big the arrays should be it doesnt work, so how do i declare arrays when i dont know the size they should be, so that i can use the malloc function.I know that an array is a pointer to its first value, but i dnt know how to implement this..basically i need help!
    this is a bit of my programme.Only the arrays time and x(t) are output to my external file, so would i have to use malloc on all arrays used in my program or only those that are output?

    Code:
    float m, k, g, x[], v[], time_increment,time[], w2, w, p, f[], A, omega;
    
    
     fprintf(output, "%f %f\n", g, w);
       
    time[0]=0;
     i=-1;
      
      while((time[i]>=0)&&(time[i] < p))
      {
     
       if(CASE_FLAG==1)
       {
        f[i]=0;
       }
       else if(CASE_FLAG==2)
       {
        f[i]=A*cos(omega*time[i]);
       }
       
        i++;
        
        time[i] = i*time_increment;
        v[i+1] = v[i] - (g*v[i] + w2*x[i])*time_increment + (1/m)*f[i]*time_increment;
    	x[i+1] = x[i] + v[i+1]*time_increment;
      
    	fprintf(output, "%f %f\n", time[i], x[i]);
      }
      	
    	fclose(output);
    many thanx
    -zesty-

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Code:
    /* .. */
    
    float *fArray = malloc(sizeof(float) * 128); /* Allocates memory for 128 floating point numbers. */
    
    /* .. */

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    but i dont understand what i would write in the *fArray bit for my time array?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by zesty View Post
    but i dont understand what i would write in the *fArray bit for my time array?
    You mean when you declare the variable? You should just declare it as
    Code:
    float *x; /* array of floats, array size to be determined later */
    Once you know how big your array is going to be, then you can malloc.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    thanx. And i was also wondering in my programme i use 4 arrays all of which i dont know the size..However, only two arrays are actually output to an external file.So would i need to use malloc on all 4 arrays or only the two arrays that are output?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to use malloc just to have an array! Declaring the pointer, as above, just says "I want to have an array, eventually." You don't have any storage to put the array in until you malloc.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    ok then...so i declare float *x..Then do i have to do anything with it like x=&x[0]; or *x=x[0] to get it to take it as the start of my array, or am i talking rubbish..And when would i have to use malloc.At the end once ive determined how many values i will get in my output file, or right at the start?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't malloc until you know how many you want. You can't access the array until you malloc (you have a pointer, but it doesn't point anywhere until you allocate storage for it with malloc).

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    but how would i go about doing that..it seems like a contradiction:S...my malloc looks like this

    Code:
    FILE *output;
      float m, k, g, *x, *v, time_increment,*time, w2, w, p, *f, A, omega;
      int i,CASE_FLAG;
      int ivalue(int i);
      
      
      
      time=(float *)malloc(sizeof(float)*(i+2));
      x=(float *)malloc(sizeof(float)*(i+2));
      v=(float *)malloc(sizeof(float)*(i+2));
      f=(float *)malloc(sizeof(float)*(i+2));
    but 'i' can only be determined once the second bit of my programme has been run which is after the malloc:
    Code:
    i++;
        
        time[i] = i*time_increment;
        v[i+1] = v[i] - (g*v[i] + w2*x[i])*time_increment + (1/m)*f[i]*time_increment;
    	x[i+1] = x[i] + v[i+1]*time_increment;
      
    	fprintf(output, "&#37;f %f\n", time[i], x[i]);
      }
      
    	fclose(output);
    	
    	free(time);
    	free(x);
    	free(v);
    	free(f);
        return(0);
    }
    also, is that the right place to put the free function..right at end of my programe?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That looks like you're in a for-loop. How do you know when to stop? Put another way: you said, way back when, that your memory needs changed based on some parameters. Do you know which parameters? Why not read those in, use those to compute how large i is going to be, and then do the malloc?

    Edit to add: And for freeing, you can free at any time after you have finished using that array. If you free at the very end, then at least you know that you've done it somewhere.
    Last edited by tabstop; 01-19-2008 at 11:47 AM.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    yeh, it was a while loop that looks like

    Code:
    time_increment=0.5;
      time[0]=0;
      A=1;
      w2= k/m;
      w= pow( w2, 0.5);
      p= (10*M_PI)/w;
      omega= (w/4);
      
      
      fprintf(output, "&#37;f %f\n", g, w);
       
    
      /*While loop used as time is always changing based on parameter p*/
      
      
      
      i=-1;
      
      while((time[i]>=0)&&(time[i] < p))
      {
     
       if(CASE_FLAG==1)
       {
        f[i]=0;
       }
       else if(CASE_FLAG==2)
       {
        f[i]=A*cos(omega*time[i]);
       }
       
        i++;
        
        time[i] = i*time_increment;
        v[i+1] = v[i] - (g*v[i] + w2*x[i])*time_increment + (1/m)*f[i]*time_increment;
    	x[i+1] = x[i] + v[i+1]*time_increment;
      
    	fprintf(output, "%f %f\n", time[i], x[i]);
      }
    so i have that my programe stops when time[i]<p.However p is a float, and 'i' is an integer, so do you mean I enter in a formula to compute 'i' before my malloc and then that should work out?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, time[i] = i*time_increment and p is, well, p. So i*time_increment < p means i < p/time_increment. So evaluate the right-hand side, cast to an int (which rounds down), and add 1.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    yeh i managed to do that, and it works so WOOHOO!:>..tho i used the ceiling function which worked out alrite..thankyou so much for all ur help..xx

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    Quote Originally Posted by zesty View Post
    Hi..I need some help in using the realloc function in my program as the memory i need to use keeps on changing based on some parameters.Do i first have to use the malloc function and then realloc?
    You don't have to.
    Passing NULL to realloc will allocate memory for the first time.
    Consider the following example:

    Code:
    int *AllocateMemory(int *pArr, int *iTotal)
    {
    	pArr=realloc(pArr,sizeof(*pArr)*(*iTotal+1));
    	if (pArr==NULL)
    	{
    		puts("Allocation failed!");
    		exit(1);
    	}
    
    	++(*iTotal);
    	return pArr;
    }
    
    int main(void)
    {
    	int *pArray=NULL;
    	int iTotal=0;
    
    	// First record
    	pArray=AllocateMemory(pArray, &iTotal);
    	// Second record
    	pArray=AllocateMemory(pArray, &iTotal);
    
    	return 0;
    }
    Regards
    Doron Moraz

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    zesty: Oooh, spaghetti code. I don't like spaghetti code!
    Have a read!
    http://cpwiki.sourceforge.net/index.php/Indentation
    Will do you wonders in making readable code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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, 09:01 AM
  2. using character arrays and realloc
    By MK27 in forum C Programming
    Replies: 9
    Last Post: 09-05-2008, 08:21 AM
  3. malloc, realloc..wat on earth!
    By zesty in forum C Programming
    Replies: 3
    Last Post: 12-21-2007, 12:42 PM
  4. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  5. Concerning Dynamic Arrays
    By CeeCee in forum C Programming
    Replies: 15
    Last Post: 11-25-2001, 01:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21