Intializing Large Character Array [Solved]

Posts 1627 of 27 · Page 2 of 2
Quote Originally Posted by ISmokeWeedAmICoolNow View Post
you cant NOT have 30000 memory allocations unless you use static char arrays. your solution would have 30000 char*s all pointing to "30000".

Code:
char* OP[30000];
for (int Doesnt = 0; Doesnt < 30000; ++Doesnt)
{
	char* KnowCPP = new char[15];
	_itoa(Doesnt, KnowCPP, 10);
	OP[Doesnt] = KnowCPP;
}
Other way round bud, static remains at a constant memory allocation. Creating a new character array (as in my earlier post) every iteration does allocate new memory every iteration (while it does clean up the old memory due to it going out of scope, it's still overhead you don't need.)

Nice try with the insult code, shame you forgot to add 1 to "Doesnt" meaning his array with be { "0", "1"...."29999" }

QQ moar.
Quote Originally Posted by Jason View Post


Other way round bud, static remains at a constant memory allocation. Creating a new character array (as in my earlier post) every iteration does allocate new memory every iteration (while it does clean up the old memory due to it going out of scope, it's still overhead you don't need.)

Nice try with the insult code, shame you forgot to add 1 to "Doesnt" meaning his array with be { "0", "1"...."29999" }

QQ moar.


I have never seen a programming language insult until today. LOL.
Quote Originally Posted by 258456 View Post
I have never seen a programming language insult until today. LOL.
They're everywhere!
Code:
Human *Hassan = (Human*)0xBEEF;
Human *Me = (Human*)0xFFFFFFFF;
while ( Hassan->MusicTaste == MusicTaste.Shit )
    Me->RespectFor(Hassan)--;
Quote Originally Posted by Jason View Post


They're everywhere!
Code:
Human *Hassan = (Human*)0xBEEF;
Human *Me = (Human*)0xFFFFFFFF;
while ( Hassan->MusicTaste == MusicTaste.Shit )
    Me->RespectFor(Hassan)--;
ROFL. You mad because MJ sings and dance better than those who you listen to ?
Quote Originally Posted by Jason View Post


Other way round bud, static remains at a constant memory allocation. Creating a new character array (as in my earlier post) every iteration does allocate new memory every iteration (while it does clean up the old memory due to it going out of scope, it's still overhead you don't need.)

Nice try with the insult code, shame you forgot to add 1 to "Doesnt" meaning his array with be { "0", "1"...."29999" }

QQ moar.
sorry to burst your bubble but your code only has one static array that gets changed every iteration, and all of the array points to that one array, so they all point to "30000" :].

and your code had that same error in addition to its other errors by the way.
Quote Originally Posted by ISmokeWeedAmICoolNow View Post
sorry to burst your bubble but your code only has one static array that gets changed every iteration, and all of the array points to that one array, so they all point to "30000" :].

and your code had that same error in addition to its other errors by the way.
Yes, you are correct that my second solution would not work as I thought, at the time I just assumed itoa malloc'd some memory and returned that as it's returned char*, I use C not C++ mostly so I don't have itoa often, and have to write my own.

Code:
static char* __itoa(int v)
{
	char *retVal = (char*)malloc(sizeof(char) * 10);
	sprintf(retVal, "%d\0", v);
	return retVal;
}

static int main(int argc, char *argv[])
{
    char *values[30000];
    for ( int i = 0; i < 30000; i++ )
        values[i] = __itoa(i + 1);
    //assigning is complete, just to verify that all is correct we'll print them.
    for ( int i = 0; i < 1000; i++ )
        printf("%s\n", values[i]);

    //free the malloc'd memory
    for ( int i = 0; i < 30000; i++ ) 
        free(values[i]);
    
    return 0;
}
So, my original solution (the first one i posted) works anyway, get off your high horse.
Quote Originally Posted by Jason View Post


Yes, you are correct that my second solution would not work as I thought, at the time I just assumed itoa malloc'd some memory and returned that as it's returned char*, I use C not C++ mostly so I don't have itoa often, and have to write my own.

Code:
static char* __itoa(int v)
{
	char *retVal = (char*)malloc(sizeof(char) * 10);
	sprintf(retVal, "%d\0", v);
	return retVal;
}

static int main(int argc, char *argv[])
{
    char *values[30000];
    for ( int i = 0; i < 30000; i++ )
        values[i] = __itoa(i + 1);
    //assigning is complete, just to verify that all is correct we'll print them.
    for ( int i = 0; i < 1000; i++ )
        printf("%s\n", values[i]);

    //free the malloc'd memory
    for ( int i = 0; i < 30000; i++ ) 
        free(values[i]);
    
    return 0;
}
So, my original solution (the first one i posted) works anyway, get off your high horse.
you used *your* version of itoa, so it doesnt work for anyone except you. you act like ive been trying to attack you, but you posted incorrect code so i was pointing out the error, dont be so ignorant
Quote Originally Posted by ISmokeWeedAmICoolNow View Post
you used *your* version of itoa, so it doesnt work for anyone except you. you act like ive been trying to attack you, but you posted incorrect code so i was pointing out the error, dont be so ignorant
LOL itoa doesn't exist in C bud, so of course I wrote my own, I specifically stated that I made a mistake with my previous post, I was just used to my own itoa because I use C most of the time not C++.

Also, posting:
Code:
char* OP[30000];
for (int Doesnt = 0; Doesnt < 30000; ++Doesnt)
{
	char* KnowCPP = new char[15];
	_itoa(Doesnt, KnowCPP, 10);
	OP[Doesnt] = KnowCPP;
}
Is what? Constructive?
Quote Originally Posted by Jason View Post


LOL itoa doesn't exist in C bud, so of course I wrote my own, I specifically stated that I made a mistake with my previous post, I was just used to my own itoa because I use C most of the time not C++.

Also, posting:
Code:
char* OP[30000];
for (int Doesnt = 0; Doesnt < 30000; ++Doesnt)
{
	char* KnowCPP = new char[15];
	_itoa(Doesnt, KnowCPP, 10);
	OP[Doesnt] = KnowCPP;
}
Is what? Constructive?
you specifically stated that your first solution was correct, which it is clearly not.

is correct c++ and it works?
Quote Originally Posted by ISmokeWeedAmICoolNow View Post
you specifically stated that your first solution was correct, which it is clearly not.

is correct c++ and it works?
Yeah lol just looked at my first post and for some reason I thought I wrote one like this:

Code:
char *values[30000]
for (int i = 0; i < 30000; i++ )
    values[i] = itoa(i + 1, new char[15], 10);
Which I didn't, so just calm the fuck down.
Yes, that is exactly why I am mad. Because MJ is awesome. I am not being sarcastic, I really and truly believe it...

-.-
/Sigh. Jason's correction works. i.e;

[highlight=vb]char *largeArray[30000];
for(int i = 0; i < 30000; i++)
largeArray[i] = itoa(i + 1, new char[10], 10);[/highlight]

The OP doesn't gives a flying fuck if he wrote anything afterwards, so no big deal.

/Closed.
Posts 1627 of 27 · Page 2 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?