473,624 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic memory

I am having a little trouble with dynamic memory allocation. I am trying to
read a text file and put the contents into a dynamic array. I know I can
use vectors to make this easier, but it has to be done using dynamic arrays.
I don't know the size of the text file ahead of time, though, so I created a
class that includes a method to resize the array. Here is that class:

class Data
{
public:
Data(int initialsize);
~Data();
char **array;
void resize(int newsize);
void add(const int index, const string temp);
private:
int size;
int high;
int low;
};

Data::Data(int initialsize)
{
size = initialsize;
array = new char*[size];
}

Data::~Data()
{
delete array;
}

void Data::resize(in t newsize)
{
char **newarray;

newarray = new char*[newsize];
memcpy(newarray , array, size*sizeof (char*));
size = newsize;
delete array;
array = newarray;
}

Now, here is my driver program where I am reading the data and trying to put
into the array from the Data class above:

#include <iostream>
#include <fstream>
#include <string>
#include "Data.h"

using namespace std;

int main()
{
char filename[14];
ifstream infile;
string temp;

cout << "Please input filename: ";
cin >> filename;
cout << endl;

infile.open(fil ename);

if(infile.fail( ))
cout << "Invalid filename." << endl;
else
{
int size = 0; // initial size of data array (zero because we don't yet
know how many lines there are)

Data myData(size); // create a data array object

// keep reading lines from the data file until end of file is reached
while(!infile.e of())
{
myData.resize(+ +size); // add space to the array object for another data
line
getline(infile, temp, '\n');
strcpy(myData.a rray[size - 1], temp.c_str());
}

}

return 0;
}

The data file is simply a multiline text file with each line terminated with
a CRLF.

Everything compiles, but when I go to run it, I get a message that says
"Unhandled exception blah blah blah" when the strcpy(myData.a rray[0],
temp.c_str()); line is executed.

Can anyone help me figure out how to make this work?

Thanks
Oct 10 '05 #1
5 3403
meyousikmann wrote:
I am having a little trouble with dynamic memory allocation. I am trying
to
read a text file and put the contents into a dynamic array. I know I can
use vectors to make this easier, but it has to be done using dynamic
arrays.
Why?
I don't know the size of the text file ahead of time, though, so I
created a
class that includes a method to resize the array. Here is that class:

class Data
{
public:
Data(int initialsize);
~Data();
char **array;
This is an array of C-style strings. You should consider

std::string array [];

instead. That would allow you to focus on managing a dynamic array. The
approach you have taken forces you to simultaneously deal with the memory
management of the C-strings. That is where your problems come from.
void resize(int newsize);
void add(const int index, const string temp);
private:
int size;
int high;
int low;
};

Data::Data(int initialsize)
{
size = initialsize;
array = new char*[size];
This allocates the array. But it leaves the task of allocating memory for
each C-string to the client.
}

Data::~Data()
{
delete array;
This only deallocates the array. Memory for the individual C-strings will
leak.
}

void Data::resize(in t newsize)
{
char **newarray;

newarray = new char*[newsize];
memcpy(newarray , array, size*sizeof (char*));
size = newsize;
delete array;
array = newarray;
}

Now, here is my driver program where I am reading the data and trying to
put into the array from the Data class above:

#include <iostream>
#include <fstream>
#include <string>
#include "Data.h"

using namespace std;

int main()
{
char filename[14];
ifstream infile;
string temp;

cout << "Please input filename: ";
cin >> filename;
cout << endl;

infile.open(fil ename);

if(infile.fail( ))
cout << "Invalid filename." << endl;
else
{
int size = 0; // initial size of data array (zero because we don't yet
know how many lines there are)

Data myData(size); // create a data array object

// keep reading lines from the data file until end of file is reached
while(!infile.e of())
{
myData.resize(+ +size); // add space to the array object for another
data
line
getline(infile, temp, '\n');
strcpy(myData.a rray[size - 1], temp.c_str());
Hm, myData.array[size-1] is a char*. But you never actually allocated memory
for that one, did you? try:

char* dummy = new char [ temp.size() + 1 ];
myData.array[size-1] = dummy;
strcpy(dummy, temp.c_str());

However, using std::string[] instead of char** would be a way better option.
}

}

return 0;
}

The data file is simply a multiline text file with each line terminated
with a CRLF.

Everything compiles, but when I go to run it, I get a message that says
"Unhandled exception blah blah blah" when the strcpy(myData.a rray[0],
temp.c_str()); line is executed.

Can anyone help me figure out how to make this work?

Thanks


Oct 10 '05 #2
meyousikmann wrote:
I am having a little trouble with dynamic memory allocation. I am trying to
read a text file and put the contents into a dynamic array. I know I can
use vectors to make this easier, but it has to be done using dynamic arrays.
I don't know the size of the text file ahead of time, though, so I created a
class that includes a method to resize the array. Here is that class:


There are a number of problems... Here are the highlights:
- Don't resize as often. Resizing is usually a nontrivial performance
hit.
- If you're allowed to, prefer an array of std::string over an array
of char*.
- When reading from a file, don't check eof() until after you've
tried to read. This is the most frequent mistake newbies make. See the FAQ.
- getline() extracts the delimiter ('\n') from the stream and throws
it away. I assume you actually want to append it to the string you're
storing...
- When deleting an array you *must* use delete[] rather than plain
old delete.
- There's no point starting the size of your array at 0. It's OK to
over-estimate.
Here's some food for thought:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <cassert>

using namespace std;

class Data{
char** array; ///< Ideally this should be a string*, or
///< even vector<string>.
static const unsigned int initial_size = 16;
unsigned int size;
unsigned int next_line;

public:
Data(): array(new char*[initial_size]),
size(initial_si ze),
next_line(0)
{
char* dummy = NULL;
fill(array, array + size, dummy);
}

~Data(){
// Delete each line first, and then the array that held them.
for (unsigned i = 0; i < next_line; ++i){
delete[] array[i];
}
delete[] array;
}

char** begin(){ return array; }
char** end(){ return array + next_line; }

// There's no need to provide an index if we're always appending...
void append(const string& s){
if (next_line == size){
// Double the size each time rather than just adding 1.
resize(size<<1) ;
}
assert(array[next_line] == NULL);
array[next_line] = new char[s.size() + 1];
strcpy(array[next_line], s.c_str());
++next_line;
}

void resize(unsigned int new_size){
if (new_size <= size){
return;
}
char** new_array = new char*[new_size];
char* dummy = NULL;
fill(new_array, new_array + new_size, dummy);
// Copy the pointers to existing strings rather than
// copying their data.
copy(array, array + next_line, new_array);
delete[] array;
array = new_array;
size = new_size;
}
};

int main(int argc, char** argv){
if (argc != 2){
return EXIT_FAILURE;
}
ifstream in(argv[1]);
if (in.fail()){
return EXIT_FAILURE;
}
Data data;
string line;
while (getline(in, line, '\n')){
line+="\n";
data.append(lin e);
}
copy(data.begin (), data.end(), ostream_iterato r<char*>(cout)) ;
}

Oct 10 '05 #3

"Kai-Uwe Bux" <jk********@gmx .net> wrote in message
news:di******** **@murdoch.acc. Virginia.EDU...
meyousikmann wrote:
I am having a little trouble with dynamic memory allocation. I am trying
to
read a text file and put the contents into a dynamic array. I know I can
use vectors to make this easier, but it has to be done using dynamic
arrays.
Why?


There is a BUNCH of legacy code that is expecting the array of C-style
strings. I am not very comfortable with older C code so I am just trying to
make as small a change as possible. I just want to make a change that will
read the data from a data file and put into the array without screwing
everything up totally.
I don't know the size of the text file ahead of time, though, so I
created a
class that includes a method to resize the array. Here is that class:

class Data
{
public:
Data(int initialsize);
~Data();
char **array;
This is an array of C-style strings. You should consider

std::string array [];


Point well taken. I am going to try to do work in your suggestion.

instead. That would allow you to focus on managing a dynamic array. The
approach you have taken forces you to simultaneously deal with the memory
management of the C-strings. That is where your problems come from.
void resize(int newsize);
void add(const int index, const string temp);
private:
int size;
int high;
int low;
};

Data::Data(int initialsize)
{
size = initialsize;
array = new char*[size];
This allocates the array. But it leaves the task of allocating memory for
each C-string to the client.
}

Data::~Data()
{
delete array;


This only deallocates the array. Memory for the individual C-strings will
leak.


Yep, I see that. I will work on this as well.
}

void Data::resize(in t newsize)
{
char **newarray;

newarray = new char*[newsize];
memcpy(newarray , array, size*sizeof (char*));
size = newsize;
delete array;
array = newarray;
}

Now, here is my driver program where I am reading the data and trying to
put into the array from the Data class above:

#include <iostream>
#include <fstream>
#include <string>
#include "Data.h"

using namespace std;

int main()
{
char filename[14];
ifstream infile;
string temp;

cout << "Please input filename: ";
cin >> filename;
cout << endl;

infile.open(fil ename);

if(infile.fail( ))
cout << "Invalid filename." << endl;
else
{
int size = 0; // initial size of data array (zero because we don't yet
know how many lines there are)

Data myData(size); // create a data array object

// keep reading lines from the data file until end of file is reached
while(!infile.e of())
{
myData.resize(+ +size); // add space to the array object for another
data
line
getline(infile, temp, '\n');
strcpy(myData.a rray[size - 1], temp.c_str());
Hm, myData.array[size-1] is a char*. But you never actually allocated
memory
for that one, did you? try:

char* dummy = new char [ temp.size() + 1 ];
myData.array[size-1] = dummy;
strcpy(dummy, temp.c_str());


You got it. Funny how things seem to work when the memory is allocated
properly. Thanks for that. I really appreciate the help.

However, using std::string[] instead of char** would be a way better
option.
}

}

return 0;
}

The data file is simply a multiline text file with each line terminated
with a CRLF.

Everything compiles, but when I go to run it, I get a message that says
"Unhandled exception blah blah blah" when the strcpy(myData.a rray[0],
temp.c_str()); line is executed.

Can anyone help me figure out how to make this work?

Thanks

Oct 10 '05 #4

"Jacques Labuschagne" <ja*****@clawsh rimp.com> wrote in message
news:Yn******** *************@n ews.xtra.co.nz. ..
meyousikmann wrote:
I am having a little trouble with dynamic memory allocation. I am trying
to read a text file and put the contents into a dynamic array. I know I
can use vectors to make this easier, but it has to be done using dynamic
arrays. I don't know the size of the text file ahead of time, though, so
I created a class that includes a method to resize the array. Here is
that class:


There are a number of problems... Here are the highlights:
- Don't resize as often. Resizing is usually a nontrivial performance
hit.
- If you're allowed to, prefer an array of std::string over an array of
char*.
- When reading from a file, don't check eof() until after you've tried
to read. This is the most frequent mistake newbies make. See the FAQ.
- getline() extracts the delimiter ('\n') from the stream and throws it
away. I assume you actually want to append it to the string you're
storing...
- When deleting an array you *must* use delete[] rather than plain old
delete.
- There's no point starting the size of your array at 0. It's OK to
over-estimate.
Here's some food for thought:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <cassert>

using namespace std;

class Data{
char** array; ///< Ideally this should be a string*, or
///< even vector<string>.
static const unsigned int initial_size = 16;
unsigned int size;
unsigned int next_line;

public:
Data(): array(new char*[initial_size]),
size(initial_si ze),
next_line(0)
{
char* dummy = NULL;
fill(array, array + size, dummy);
}

~Data(){
// Delete each line first, and then the array that held them.
for (unsigned i = 0; i < next_line; ++i){
delete[] array[i];
}
delete[] array;
}

char** begin(){ return array; }
char** end(){ return array + next_line; }

// There's no need to provide an index if we're always appending...
void append(const string& s){
if (next_line == size){
// Double the size each time rather than just adding 1.
resize(size<<1) ;
}
assert(array[next_line] == NULL);
array[next_line] = new char[s.size() + 1];
strcpy(array[next_line], s.c_str());
++next_line;
}

void resize(unsigned int new_size){
if (new_size <= size){
return;
}
char** new_array = new char*[new_size];
char* dummy = NULL;
fill(new_array, new_array + new_size, dummy);
// Copy the pointers to existing strings rather than
// copying their data.
copy(array, array + next_line, new_array);
delete[] array;
array = new_array;
size = new_size;
}
};

int main(int argc, char** argv){
if (argc != 2){
return EXIT_FAILURE;
}
ifstream in(argv[1]);
if (in.fail()){
return EXIT_FAILURE;
}
Data data;
string line;
while (getline(in, line, '\n')){
line+="\n";
data.append(lin e);
}
copy(data.begin (), data.end(), ostream_iterato r<char*>(cout)) ;
}


Wow. Thanks for the great info. As you very accurately pointed out, I am a
newbie at this, but I am learning with help from great people like you.

Thanks!
Oct 10 '05 #5
meyousikmann wrote:
I am having a little trouble with dynamic memory allocation. I am trying
to
read a text file and put the contents into a dynamic array. I know I can
use vectors to make this easier, but it has to be done using dynamic
arrays. I don't know the size of the text file ahead of time, though, so I
created a
class that includes a method to resize the array. Here is that class:
[homegrown vector snipped] Now, here is my driver program where I am reading the data and trying to
put into the array from the Data class above:

#include <iostream>
#include <fstream>
#include <string>
#include "Data.h"

using namespace std;

int main()
{
char filename[14];
ifstream infile;
string temp;

cout << "Please input filename: ";
cin >> filename;
cout << endl;

infile.open(fil ename);

if(infile.fail( ))
cout << "Invalid filename." << endl;
else
{
int size = 0; // initial size of data array (zero because we don't yet
know how many lines there are)

Data myData(size); // create a data array object

// keep reading lines from the data file until end of file is reached
while(!infile.e of())
{
myData.resize(+ +size); // add space to the array object for another
data
line
getline(infile, temp, '\n');
strcpy(myData.a rray[size - 1], temp.c_str());
}

}

return 0;
}

Here is a version using the standard library:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
std::string filename;
std::ifstream infile;

std::cout << "Please input filename: ";
std::cin >> filename;
std::cout << '\n';

infile.open(fil ename.c_str());

if(infile.fail( )) {
std::cout << "Invalid filename.\n";
} else {
std::vector< std::string > data;

// keep reading lines from the data file
// until end of file is reached
while(!infile.e of()) {
std::string temp;
std::getline( infile, temp );
data.push_back( temp );
}

// print the data:
std::copy( data.begin(), data.end(),
std::ostream_it erator<std::str ing>( std::cout, "\n" ) );
}

return 0;
}

You should seriously consider using std::string and std::vector. Code
becomes way more easy to maintain. Note in particular that no memory
management is required on your part.
Best

Kai-Uwe Bux
Oct 10 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8200
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new DestinationAddress(); // code 1
6
2973
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of strings'. I already know that no individual string will be longer than 50 characters. I just don't know before run time how many elements of the array will be needed. I have heard it is possible to dynamically allocate memory for a 2
5
3748
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: I want to fill a list box with a list of Project Names from a database (in Palm this is more...
3
1312
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example below... --
24
19065
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
1
7960
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
14
3820
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is this stored in stack. If yes whether it will be deleted on exiting from the function. is dynamic memory allocation needed for this purpose
20
6530
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE() function in C.. But if I try to use a single linked list with a static char array fields I can free the memory allocated with out any problems using the FREE(). So, why freeing a single linked list with dynamic char* is hard and why the FREE() function is...
10
4410
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both the allocation is impossible. Sworna vidhya
0
8249
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.