dynamic array

Thread Starter

VVS

Joined Jul 22, 2007
66
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.
 

Attachments

Mark44

Joined Nov 26, 2007
628
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.
I took the liberty of copying your example into a code block.
Rich (BB code):
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

/////////////////////////////Global Variables/////////////////////////////
int number_of_instructions;	 //number of instructions in the command file
string command_file_name;	 //string for entering the name of the command file
string current_line;		 //string of the current line that is being read
string *commands;		 //string array of commands
string *temp;

	
int main (int argc, char* argv[]) 
{
  ifstream command_file ("command_test1.txt");	  //open "command_test1.txt"
  ptr=commands;
  int i=0;
	
  while (! command_file.eof() )	
  {
     command_file>>current_line;
     commands=new string[i+1];
     commands=current_line;
     i++;
  }
	
  number_of_instructions = i;
  for (i=0; i<number_of_instructions; i++)
  {
    cout<<commands<<endl;
  }

  getchar();
  return 0;
}


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.
 

Thread Starter

VVS

Joined Jul 22, 2007
66
@ 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.
 

Mark44

Joined Nov 26, 2007
628
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/
 
Top