Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-09-2007, 09:08 AM   #1 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
Pointers

Hi guys, I'm trying to load the contents of a file I am writing into dynamic memory. What I was thinking is have an array, of which each element holds a pointer to a struct which contains information on different types of people.

the struct I am holding it in is something like this:
Code:
struct StudentRecord
{
	char* firstName;			// student key
	char* lastName;				// student name
	int id;					// student id
	float mark;				// course mark
};
in main, I have
Code:
struct StudentRecord ** Records;
int numRecords = 0;
my function will read from file (already opened), and dynamically allocate the lengths of names, as well as the array will grow as needed. my question is regarding resizing the array...

Sinc each element is a pointer to a structure, I will use another array which will grow each time the array is filled (by a factor of 2) to copy the existing array, resize it, then copy it back, after which deleting it. I think I've got it figured out, but am wondering about some tweaks:

Here's the code, and a couple questions to follow:
Code:
bool LoadFileToMemory(ifstream& fin, StudentRecord**& Records, int& numRecords)
{
	char * firstName;
	char * lastName;
	int size = 10; //an initial size for the StudentRecord array
	struct StudentRecord ** tempRecords;
	
	Records = new StudentRecord*[size];
	while(!fin.eof())
	{
		if((size -1) == numRecords)
		{
			tempRecords = new StudentRecord*[size];
			for(int i = 0; i < numRecords; i++)
				tempRecords[i] = Records[i];
			size += size;
			Records = new StudentRecord*[size];
			for (i = 0; i < numRecords; i++)
				Records[i] = tempRecords[i];
		}

		if(!fin >> firstName >> lastName)
		{
			cerr << "Failed to read from file...\n";
			return false;
		}
		Records[numRecords] = new struct StudentRecord;
		Records[numRecords]->firstName = new char[strlen(firstName) + 1];
		strcpy(Records[numRecords]->firstName,firstName);
		Records[numRecords]->lastName = new char[strlen(lastName) + 1];
		strcpy(Records[numRecords]->lastName,lastName);
		if(!fin >> Records[numRecords]->id >> Records[numRecords]->mark)
		{
			cerr << "Failed to read from file...\n";
			reteurn false;
		}
		numRecords += 1;
	}
	//delete the temporary array of pointers
	delete [] tempRecords;
	return true;
}
1) Would just resizing the array by one be more efficient than this approach, as I wouldn't have to keep track of the size of the array versus doubling it each time, to not have to enter the two for loops as often?

2) Is there a better way of loading from a file to memory?
Deliverance is offline   Reply With Quote
Old 01-09-2007, 09:43 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
Its too time consuming, it will take a fair amount of time to keep calling new on each read, and keep copying everything from one array to the other.
Since you're using C++, why not use something like std::vector or
std::lists or std::queue ??

If this was strict C, then I would chose to use your aproach, only increase every N.th time, since calling malloc()/realloc() otherwise would be too timeconsuming..
Besides your way of copying everything back and forth, is too timeconsuming anyway, to be of any effeciency if you were to increment with one for every read.

In this day and age, where every household computer comes with near to unlimited amount of memory, it wouldn't be smart to waste importain CPU cykles on switching back and forth.. Remember on each addition, your array only expand with one item, but you need to copy 2*N+1 items on each read. Imagine how that looks as a runtime scenario with wasted cykles....
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 01-09-2007, 01:26 PM   #3 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
I see what you're saying, that's what I thought too. I asked my professor on this and for the scope of this assignment he said it would be acceptable to increment the array by 1 each time, however there are more ways to do this. growing it by a factor of 2 I think will be the best option because when dealing with small or large cases, the array will not need to be resized and moved N times, rather something approaching logN, evening out the curve as 100's or 1000's of elements need to be added.

I also agree with your suggestion that a vector or list would have worked great, but the requirement was that I don't use any STL, just to get me back in the groove of things with pointers. In light of this, sad to say, this is my first real self-introduction into dynamic memory allocation. I've used malloc/realloc/free and new/delete, but never in respects to resizing an array as required, and am not familiar with the tried and tested ways of resizing an array. With my implementation, I tried to reduce the amount of times the array will have to be resized.

Thanks for the tips redhead
Deliverance is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
empty pointers are not empty using 'new' operator DJMaze Platform/API C++ 3 04-16-2005 12:11 AM
Problem with multi-dimensional array of pointers glennandrewcoop Standard C, C++ 4 03-30-2005 01:00 PM
A day late and pointers short saline Standard C, C++ 2 10-04-2003 10:33 AM
Pointers w00t Standard C, C++ 14 03-02-2003 12:15 PM
pointers .. sde Standard C, C++ 2 06-24-2002 12:54 PM


All times are GMT -8. The time now is 05:10 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting