dynamic array

Discussion in 'Programmer's Corner' started by VVS, Feb 28, 2009.

  1. VVS

    Thread Starter Active Member

    Jul 22, 2007
    66
    0
    Hello,

    I am trying to read instructions from a text file into an array. but i dont know the number of instructions in the text file, so i need to use a dynamic array.
    But i dont know how i can increase the number of elements in the dynamics array while reading from the text file.
    You can find the code I have so far in the text file.
    The problem is that in effect i am only reading in the last instruction, because whenever the file pointer goes to a new line a new string array is created which erases the previous content. so i need to find a way of storing the previous content, but how? :confused:
    i tried many ways, but i just cant do it. please help me out.

    Note: I have to use dynamic arrays.
     
    • code.txt
      File size:
      994 bytes
      Views:
      13
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I took the liberty of copying your example into a code block.
    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. #include <fstream>
    4. #include <string>
    5. #include <stdio.h>
    6. #include <stdlib.h>
    7. using namespace std;
    8.  
    9. /////////////////////////////Global Variables/////////////////////////////
    10. int number_of_instructions;  //number of instructions in the command file
    11. string command_file_name;    //string for entering the name of the command file
    12. string current_line;         //string of the current line that is being read
    13. string *commands;        //string array of commands
    14. string *temp;
    15.  
    16.    
    17. int main (int argc, char* argv[])
    18. {
    19.   ifstream command_file ("command_test1.txt");    //open "command_test1.txt"
    20.   ptr=commands;
    21.   int i=0;
    22.    
    23.   while (! command_file.eof() )
    24.   {
    25.      command_file>>current_line;
    26.      commands=new string[i+1];
    27.      commands[i]=current_line;
    28.      i++;
    29.   }
    30.    
    31.   number_of_instructions = i;
    32.   for (i=0; i<number_of_instructions; i++)
    33.   {
    34.     cout<<commands[i]<<endl;
    35.   }
    36.  
    37.   getchar();
    38.   return 0;
    39. }
    40. [/i][/i]


    There are a lot of problems with this code. You mentioned that you are supposed to use dynamic arrays, but I don't see any evidence that you are using them. I assume that you mean that you are supposed to use arrays whose storage is allocated from the heap, which most likely means that your program will call alloc() to do this. There is also the realloc() function that can be used to reallocate a larger sized block of memory from the heap.

    Aside from not allocating memory from the heap, the next biggest problem is that you are using pointers to memory (the variables current_line, commands, and temp). When you declare pointer variables that will be used to hold the addresses of arrays, you have to allocate the storage that will be used. If you don't, your program will probably write over important information that is there, which might cause your program to crash.

    Also, there is an undeclared variable, ptr. That variable would cause the compiler to generate an error, and would not produce an executable, so I don't see how you are getting this program to do anything.
     
  4. VVS

    Thread Starter Active Member

    Jul 22, 2007
    66
    0
    @ mik3:
    thanx for the website, i already checked it.

    @ mark44
    thx for your reply. um i searched for dynamic array in cplusplus.com.
    But is the kind of thing i found for dynamic memory.
    Do you know another website which explains dynamic memory.
    tbh i never heard of realloc() or so. :confused:
    I am a bit worried now after hearing that what i am doing is wrong.
     
  5. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I'm pretty sure that what you're looking for is not dynamic memory but dynamic memory allocation. There is such a thing as dynamic memory, but that refers to the mechanism by which the actual RAM is refreshed (as opposed to static memory).

    Look at your compiler's documentation for realloc and the other heap allocation functions whose declarations are in stdlib.h. In the link that Mik3 provided, I found this page that briefly discusses the heap allocation functions calloc, free, malloc, and realloc. Look at the section titled Dynamic memory management--http://www.cplusplus.com/reference/clibrary/cstdlib/
     
Loading...