shaynerossum 0 Newbie Poster

I'm trying to allocate memory delete it and then allocate it again with more, less, or the same. I've tried this several different ways, but I don't know how to do it or if it can be done with any similarity to my attempts. In my test program it should try to reallocate the same amount of memory, but in my actual program this will prove untrue.

Thank You for your Time.

// Test Program built to troubleshoot Reallocation of Memory
#include<iostream>
using namespace std;

void makelong(char* pagepass[],char pass2[], char pass1[],char *subpass[], int size);
int make1(char *pass1[], char **pagepass, char **dir,int run);
int make2(char *pass2[], char **pagepass, char **subpass, char **dir);

int main()
{
//Main is built from scratch and is passed varibles that will look like those obtained in the actuall program
char **pagepass, pass2[50] = "C:\\InputData\\", pass1[50] = "C:\\OutputData\\", **subpass; int size = 4, x;
pagepass = new char*[3];
for(x=0;x<3;x++)
	pagepass[x] = new char[20];
pagepass[0] = "something0.html";pagepass[1] = "something1.html";pagepass[2] = "something2.html";
subpass = new char *[size];
for(x=0;x<size;x++)
	subpass[x] = new char[30];
subpass[0] = "dirOne"; subpass[1]= "dirTwo"; subpass[2]= "dirThree"; subpass[3]= "dirFour";
makelong(pagepass, pass2, pass1, subpass, size);

}



void makelong(char* pagepass[],char pass2[], char pass1[],char *subpass[], int size)
{
//This function would normally do File IO but to encourage help this has been removed
int x = 0,y,z,looprun=0,dirleno, dirlenin; char *passtwo, *passone;
for(y=0;x<size;y++)
	{
	for(x=0;true;x++)	
		{
		cout << "Make 1";
		dirleno = make1(&pass1, &pagepass[x],&passone, looprun);
		/*
		// Make 2 is currently not in use because it's functionallity is similar to make1 and can not be fixed until make1 is fixed
		cout << "Make 2";
		dirlenin = make2(&pass2, &subpass[y], &pagepass[x],&passtwo);
		*/
		looprun = 1;
		}
	}
return;
}

				int make1(char *pass1[], char **pagepass, char **dir,int run)
				{
				int lenm, lens, tlen, x;
				char *ptr, *pt1, *pt2;
				for(x=0;pass1[x]!=0;x++);
				lenm = x;
				for(x=0;pagepass[x]!=0;x++);
				lens = x;
				tlen = lens + lenm;
				//Need a way to delete memory successfully 
				if(run == 1)
                                            delete dir;
                                *dir = new char[tlen];
				pt1 = *pass1;
				ptr = *dir;
				pt2 = *pagepass;
				x = 0;  
				while(*pt1)
					*ptr++ = *pt1++;
				while(*pt2)
					*ptr++ = *pt2++;
				*ptr = 0;
				cout << "\nOUTPUT DIRECTORY";
				cout << endl << *dir << endl;
				cout << "Return\n";
				return tlen;
				}

	/*			
		int make2(char *pass2[], char **pagepass, char **subpass, char **dir)
		{
		//Maybe more messy atm because I have yet to reach this function
		int lenm, lens, tlen, x, d=0, lens2; char *ptr, *ptr1;
		for(x=0;pass2[x]!=0;x++);
		lenm = x;
		for(x=0;subpass[x]!=0;x++);
		lens = x;
		for(x=0;pagepass[x]!=0;x++);
		lens2 = x;
		tlen = lens + lenm + lens2;
		*dir = new char(tlen);
		ptr = *dir;
		while(*pass2)
			*ptr++ = *mdir++;
		while(*subpass)
			*ptr++ = *subpass++;
		*ptr++ = '\\';
		while(*pagepass)
			*ptr++ = *pagepass++;
		return tlen;
		}
	*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.