dynamic memory allocation problem

This is a discussion on dynamic memory allocation problem within the C Programming forums, part of the General Programming Boards category; I have been working on this program since the start of May. It was a converter to convert a form ...

  1. #1
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56

    dynamic memory allocation problem

    I have been working on this program since the start of May. It was a converter to convert a form of data to another form of data for simulation of some kind. Actually, this program is suppose to be finished, as I have run testing on it and it works great. However, my boss wants me to change the converter to read infinite (or alot) number of lines of data instead of 260 lines that I have set statically from the start. So what I need to do is to establish a dynamic memory location array that gives me the ability to allocate memory for data by determining the number of lines in the text file. Then after allocation, i want to initialize all elements of the strct to either NULL or 0 (hence the function that I am showing is caklled initializing structs).
    Here is the function and the struct:
    Code:
    struct database_input {
      char* A;
      int B;
      double C;
      int D;
    };
    
    
    
    
    void initializing_structs(struct database_input *data, FILE *InputConnectionFile)
    {
      FILE *ifp = InputConnectionFile;
      char cat_A[260];
      char cat_B[1];
      char cat_C[260];
      int cat_D;
      int cat_E;
      int i = 0;
      int j = 0;	
      while(!feof(stdin) && !ferror(stdin))
        {
          fscanf(ifp, "%s", cat_A);
          fscanf( ifp, "%s %s %d %d",  cat_B, cat_C, &cat_D, &cat_E);
          i++;
        }
      
      *data = (struct database_input *) malloc(i * sizeof(struct database_input));
      while(j< i)
        {
          data[j].B = 0;
          data[j].C = 0;
          data[j].D = 0;
          j++;
        }
    }
    however, when i compile my program, an error message pops up:
    finprojectmain.c: In function `initializing_structs':
    finprojectmain.c:168: error: incompatible types in assignment
    which line 168 refers to the line with malloc. Can somebody give me a hint in how to fix this problem?

    Thanks
    Firyace
    Undergraduate Research
    Electrical and Biomedical Engineering Department
    University of Calgary

    My Comp:
    |Core 2 Duo 6420 4mb cache| Corsair 2*1Gb memory pc5400|
    |500Gb and 80Gb Sata2| HIS 1950pro Turbo OC 256mb ViVo|
    |X-Cube2 red micro atx case| 3in1 Tiger Game port|
    |ASUS P5B-LD2 Rev2.0-VM| WindowsXP Pro SP2| Fedora 8|
    |Windows XP Pro 64|

    My Store
    Real estate 43

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Try this:

    Code:
    data = malloc(i * sizeof(*data));

  3. #3
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    thankyou, it works now
    Firyace
    Undergraduate Research
    Electrical and Biomedical Engineering Department
    University of Calgary

    My Comp:
    |Core 2 Duo 6420 4mb cache| Corsair 2*1Gb memory pc5400|
    |500Gb and 80Gb Sata2| HIS 1950pro Turbo OC 256mb ViVo|
    |X-Cube2 red micro atx case| 3in1 Tiger Game port|
    |ASUS P5B-LD2 Rev2.0-VM| WindowsXP Pro SP2| Fedora 8|
    |Windows XP Pro 64|

    My Store
    Real estate 43

  4. #4
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    Actually I got another problem, even though its not title related, its from the same code. Some how in the line:
    Code:
    FILE *ifp = InputConnectionFile;
    When I move the pointer ifp, the InputConnectiponFIle pointer moves too, so that when in the other functions, it reads that the filepointer is at the end of the file. How do I fix this?

    Thanks.

    Edit: disregard this message please, I just solved the problem.
    Last edited by firyace; 05-23-2007 at 02:45 PM.
    Firyace
    Undergraduate Research
    Electrical and Biomedical Engineering Department
    University of Calgary

    My Comp:
    |Core 2 Duo 6420 4mb cache| Corsair 2*1Gb memory pc5400|
    |500Gb and 80Gb Sata2| HIS 1950pro Turbo OC 256mb ViVo|
    |X-Cube2 red micro atx case| 3in1 Tiger Game port|
    |ASUS P5B-LD2 Rev2.0-VM| WindowsXP Pro SP2| Fedora 8|
    |Windows XP Pro 64|

    My Store
    Real estate 43

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    fseek, fsetpos/fgetpos
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is dynamic memory allocation
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 06-08-2009, 08:11 AM
  2. Dynamic memory allocation
    By Luciferek in forum C++ Programming
    Replies: 118
    Last Post: 10-02-2008, 11:34 AM
  3. Find Size Of Dynamic Memory Allocation?
    By appleGuy in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2007, 09:34 AM
  4. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 01:07 AM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 09:52 AM

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