Loading text into memory

I need to make an array of pointers to a string where i allocate about 19 bytes. After the 19 is used i have to increment 10 each time it is filled; so ill have 29 when 19 is filled.

I think i need to use the new and delete functions but how would i implement this?

Btw im filling the array with ASCII characters from 0 to z.

Should i make a string array: string arr[19]; and fill it using a for loop. Then once the for loop is complete use operator new to allocate 10 more?
what you're saying basically is that you want a dynamic array, why not use a vector?

http://www.cplusplus.com/reference/stl/vector/
My prof hasn' taught us about vectors as yet (he did mention we will soon though)
This is what i did and i works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

#define AINDEX 19

int main() {
	string *ascii;
	int start = 48;
	int i;
	int incre=AINDEX;
	int doit = 1;

	ascii = new string[AINDEX];

	do
	{
		for (i=0;i<=incre-1;start++, i++)
		{
			if(start == 123)
			{
				doit = 0;
			}
			else
			{
				ascii[i] = start;
				cout << ascii[i] << endl;
			}
		}
		incre += 10;
		ascii = new string[incre];
	} while (doit);

	delete[] ascii;

	cin.get();
}


As you can see though i get garbage in the end anyway to fix this?

Also how can i make it so it displays a row of characters instead of on character on each line ie: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' instead of 'a'


edit: actually i dont think its working because when im out the loop and i display ascii[1] it doesnt display anything
Last edited on
You have a major memory leak there. On line 31, each iteration of the loop you are failing to delete[] whatever ascii is pointing at before re-assigning it.
Fixed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;

#define AINDEX 19

int main() {
	string *ascii;
	int start = 48;
	int i;
	int incre=AINDEX;
	int doit = 1;

	ascii = new string[AINDEX];

	do
	{
		for (i=0;i<=incre-1;start++, i++)
		{
			if(start == 123)
			{
				doit = 0;
			}
			else
			{
				ascii[i] = start;
				//cout << ascii[i] << endl;
			}
		}
		incre += 10;
		delete[] ascii;
		ascii = new string[incre];
	} while (doit);

	cout << ascii[0];

	delete[] ascii;

	cin.get();
}


Line 35 isnt displaying the value. Not sure what i did wrong here.
anyone got ideas? Ive been stuck this whole time
You delete[] ascii, allocate a new one exit the loop and try to print the first string. You aren't putting anything in it. See lines 31 - 35 (with doit = 0).
Last edited on
im trying to fix the problem but nothing is working. Do you have ideas what i can do to fix this?
Are you sure you need 19+k*10 strings? Or was it string with that many characters?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

template <typename T>
bool allocateSpace(T*& str, int* arraySize){
     
     str= (T*) malloc( *arraySize* sizeof(T));
   
     if(str!=NULL){
         *arraySize+=10;
         return true;
     }
     return false;
}

int main(){
    
char* str;
int arraySize= 19;

if(allocateSpace(str,&arraySize))
       std::cout << "Allocated!\n";
else std::cout << "Not allocated!\n";

    
getchar();
return 0;
}

If you need an array of strings just replace char* str; with std::string* str;
Last edited on
Free the allocated memory afterwards, of course.
im trying to fix the problem but nothing is working. Do you have ideas what i can do to fix this?


Aside from changing the order of things, you could try a break; after (or instead of) doit = 0;.
Topic archived. No new replies allowed.