new char[i] immer 10 chars lang
-
Wieder eine ganz einfache Frage... hoffentlich hat sie auch eine einfach Lösung --> warum ist der neue char[] immer 10 chars groß?
Kompiliere mit dem MSCompiler.Code folgt:
char* Log::cint(int x, Uint32 radix) {
if (m_cint)
delete[] m_cint;int i = -1;
Uint32 j = 0;char t;
char ts[10];bool sign;
if (sign = (bool)(x & 0x80000000)) { //check if value is signed
x ^= 0x80000000; //delete sign bit
}if (x) { //check if x is zero
while (x) {
i++;
t = x % radix; //example: 23 mod 10 = 3
x /= radix; //example: 23 / 10 = 2t |= 0x30; //numberchars begin with 0x30
ts[i] = t; //copy new char to array
}
} else {
ts[0] = '0'; //temp string is just 0
}if(sign)
ts[++i] = '-'; //if there was a sign, add itm_cint = new char[i+1]; //create a new string
while (i>=0) { //copy reverse to string... example: {2,3} -> {3,2}
m_cint[j] = ts[i];
j++;
i--;
}return m_cint;
}Ich weiß, sieht extrem grausam aus... hoffe ihr versteht was es tut --> wandelt integer in einen char um

-
Entschuldigt... bin müde und außerdem langsam wirklich ... nicht gut gelaunt.
char* Log::cint(int x, Uint32 radix) { if (m_cint) delete[] m_cint; int i = -1; Uint32 j = 0; char t; char ts[10]; bool sign; if (sign = (bool)(x & 0x80000000)) { //check if value is signed x ^= 0x80000000; //delete sign bit } if (x) { //check if x is zero while (x) { i++; t = x % radix; //example: 23 mod 10 = 3 x /= radix; //example: 23 / 10 = 2 t |= 0x30; //numberchars begin with 0x30 ts[i] = t; //copy new char to array } } else { ts[0] = '0'; //temp string is just 0 } if(sign) ts[++i] = '-'; //if there was a sign, add it m_cint = new char[i+1]; //create a new string while (i>=0) { //copy reverse to string... example: {2,3} -> {3,2} m_cint[j] = ts[i]; j++; i--; } return m_cint; }
-
Super, ein Triplepost

Ich scheine in letzter Zeit wirklich garnichts zu packen und nur Mist zu machen.
Hier noch eine Lösung... (ist eben auf Geschwindigkeit ausgelegt):
char* Log::cint(int x, Uint32 radix) {
int i = -1;
Uint32 j = 0;char t;
bool sign;if (sign = (bool)(x & 0x80000000)) { //check if value is signed
x ^= 0x80000000;//delete sign bit
x = 0x80000000 - x;
}if (x) { //check if x is zero
while (x) {
i++;
t = x % radix; //example: 23 mod 10 = 3
x /= radix; //example: 23 / 10 = 2t |= 0x30; //numberchars begin with 0x30
cint_s.m_temp[i] = t; //copy new char to array
}
} else {
cint_s.m_temp[0] = '0'; //temp string is just 0
}if(sign)
cint_s.m_temp[++i] = '-'; //if there was a sign, add itwhile (i>=0) { //copy reverse to string... example: {2,3} -> {3,2}
cint_s.m_cint[j] = cint_s.m_temp[i];
j++;
i--;
}cint_s.m_cint[j] = NULL;
return cint_s.m_cint;
}
-
CrazyCrow schrieb:
if (sign = (bool)(x & 0x80000000)) { //check if value is signed x ^= 0x80000000; //delete sign bit }Das ist ja fast schon ein Fall für thedailywtf.
if( sign = (x < 0) ) { x = -x; }Und zu deiner Frage: Nullterminierung?
-
char ts[10];Viel mehr als 10 Einträge hat dein ts auch garnicht.
-
Zu MFK:
Ja, habe ich mir schon gedacht ^^. Aber ich dachte mir ein bitwise-And und ein Bitwise-xor sind immernoch schneller ... dabei ist das völliger Quatsch.
Zu David_pb:
Ne, kann nicht länger werden, aber ... ich weiß es auch nicht.. ich war zu müde hoffe ich.
-
CrazyCrow schrieb:
Ja, habe ich mir schon gedacht ^^. Aber ich dachte mir ein bitwise-And und ein Bitwise-xor sind immernoch schneller ... dabei ist das völliger Quatsch.
Ich vermute, das langsamste an deinem Code dürfte der dynamische Speicher sein. Aber das kann nur ein Profiler wirklich klären.