I'm having another error at runtime which I'm sure has something to do with a pointer (like 90% of my errors). I just can't figure out why. :'(
Function 1:
void hex(long number, char *s1) {
char *binbuf = new char[512]; // <--- die!
// ====================================
// Convert number to binary
// ====================================
bin(number, binbuf);
// It will never get passed this, due to
// an "illegal operation".
...
Function 2:
void bin(long number, char *s1) {
...
// return
strcpy(s1, buffer);
}
Can someone help me figure out what I'm doing wrong? TIA.
If you know how to use one, a debugger would tell you quickly.
Other than that, it looks like you've trashed your memory earlier, you're trashing it now (buffer > 512 / not zero terminated), buffer is invalid, or you're out of memory.
It would help if you pasted all of the 2nd function.
And are you sure it's happening at that exact statement?
void bin(long number, char *s1) {
int remainder;
char bufhold[256];
char buffer[256];
if (number == 0) {
strcpy(s1, "00000000\0");
return;
}
while (number > 0) {
remainder = number % 2;
number /= 2;
strcpy(bufhold, buffer);
sprintf(buffer, "%d%s", remainder, bufhold);
}
// ==============
// Pad with zeros
// ==============
while (strlen(buffer) % 8 > 0) {
strcpy(bufhold, buffer);
sprintf(buffer, "0%s", bufhold);
}
// return
strcpy(s1, buffer);
}
Quote from: Sargera on July 16, 2004, 05:40 PM
And are you sure it's happening at that exact statement?
No. All I know is the function worked when I called it like this:
char test[256];
bin(100, test);
And gave me this error when I called it like this:
char *test = new char[256];
bin(100, test);
delete test;
Use a debugger then. Your error is probably happening in the body of the function, not the function call. Set break points on all of the statements in the function and step through each one until your program comes to a crash. Look at the debugger information and you should be able to figure out what's wrong. If you're using VS.NET 2003 debugging is very easy.
Quote from: Sargera on July 16, 2004, 05:47 PM
Use a debugger then. Your error is probably happening in the body of the function, not the function call.
I don't think the error is occuring on the call. I think the error is occuring when the bin function attempts to write to s1. I don't have a debugger, nor VC++, can you recommend something?
Quote from: Eli_1 on July 16, 2004, 05:49 PM
Quote from: Sargera on July 16, 2004, 05:47 PM
Use a debugger then. Your error is probably happening in the body of the function, not the function call.
I don't think the error is occuring on the call. I think the error is occuring when the bin function attempts to write to s1. I don't have a debugger, nor VC++, can you recommend something?
I don't know of any stand-alone debuggers for C/C++. They usually come in IDEs. One I can recommend is Dev-CPP which is powered by GNU GCC I believe. I use it for small testing all of the time as you don't need to make a seperate project to compile/run a program. It has a debugger, though I prefer Microsoft's, this one will do fine however. Also, there's debugging techniques without a debugger. Use exception handling and try blocks in areas where you think code might fail (i.e. the part where you're copying to s1 which points to 512 bytes of allocated characters). Also, a more simple approach will be to put a printf() or cout statement before and after the statement you think will fail to determine if that's the statement which fails or not.
DON'T
USE
STRCPY!
If I ever catch you doing that again, I'm taking away your C++ license.
Ok, I think the problem was I wasn't initializing some of my counters, so it was trying to do something like this:
buffer[66908136] = 0
The only problem there is now, is the output for hex(255, s1) ends up being "!FF". I have no clue where that "!" is comming from. :/
Full hex() functions:
void hex(long number, char *s1) {
char buftmp[256];
char *binbuf = new char[256];
char hextmp[4];
char buffer[256];
int i=0;
int holdnum=0;
// ====================================
// Convert number binary
// ====================================
binbuf[0] = 0;
bin(number, binbuf);
// ==============
// Convery to hex
// ==============
while (strlen(binbuf) > 0) {
strncpy(hextmp, binbuf, 4);
for (i=0; i<4; i++) {
if (hextmp[i] == '1') {
holdnum += power(2, 4 - (i + 1));
}
}
// =============
// Add to buffer
// =============
strcpy(buftmp, buffer);
if (holdnum < 10) {
sprintf(buffer, "%s%d", buftmp, holdnum);
} else {
switch (holdnum) {
case 10: sprintf(buffer, "%sA", buftmp); break;
case 11: sprintf(buffer, "%sB", buftmp); break;
case 12: sprintf(buffer, "%sC", buftmp); break;
case 13: sprintf(buffer, "%sD", buftmp); break;
case 14: sprintf(buffer, "%sE", buftmp); break;
case 15: sprintf(buffer, "%sF", buftmp); break;
}
}
holdnum = 0;
binbuf += 4;
}
// return
strcpy(s1, buffer);
}
Quote from: $t0rm on July 16, 2004, 06:23 PM
DON'T
USE
STRCPY!
If I ever catch you doing that again, I'm taking away your C++ license.
Eeek! What's wrong with strcpy?
Quote from: Eli_1 on July 16, 2004, 06:28 PM
Quote from: $t0rm on July 16, 2004, 06:23 PM
DON'T
USE
STRCPY!
If I ever catch you doing that again, I'm taking away your C++ license.
Eeek! What's wrong with strcpy?
Nothing, if you use it properly. Though, an example of when not to use it is if you're initalizing your buffers with a variable that isn't always going to be the same (constant?) and then attempt to use strcpy() you take a risk of a buffer overflow. But if you're careful, there's nothing wrong with it. If you're not sure about risking a buffer overflow or not, use strncpy() for the security of it. ;)
NEVER use it; ALWAYS strncpy.
Quote from: Eli_1 on July 16, 2004, 06:27 PM
Ok, I think the problem was I wasn't initializing some of my counters, so it was trying to do something like this:
buffer[66908136] = 0
The only problem there is now, is the output for hex(255, s1) ends up being "!FF". I have no clue where that "!" is comming from. :/
Full hex() functions:
void hex(long number, char *s1) {
char buftmp[256];
char *binbuf = new char[256];
char hextmp[4];
char buffer[256];
int i=0;
int holdnum=0;
// ====================================
// Convert number binary
// ====================================
binbuf[0] = 0;
bin(number, binbuf);
// ==============
// Convery to hex
// ==============
while (strlen(binbuf) > 0) {
strncpy(hextmp, binbuf, 4);
for (i=0; i<4; i++) {
if (hextmp[i] == '1') {
holdnum += power(2, 4 - (i + 1));
}
}
// =============
// Add to buffer
// =============
strcpy(buftmp, buffer);
if (holdnum < 10) {
sprintf(buffer, "%s%d", buftmp, holdnum);
} else {
switch (holdnum) {
case 10: sprintf(buffer, "%sA", buftmp); break;
case 11: sprintf(buffer, "%sB", buftmp); break;
case 12: sprintf(buffer, "%sC", buftmp); break;
case 13: sprintf(buffer, "%sD", buftmp); break;
case 14: sprintf(buffer, "%sE", buftmp); break;
case 15: sprintf(buffer, "%sF", buftmp); break;
}
}
holdnum = 0;
binbuf += 4;
}
// return
strcpy(s1, buffer);
}
Is power() one of your functions or is it pow() from the library cmath?
power is my function. I wasn't aware there was a pow function untill after I wrote the function and saw it on google.
Quote from: Eli_1 on July 16, 2004, 07:46 PM
power is my function. I wasn't aware there was a pow function untill after I wrote the function and saw it on google.
Can you please paste your entire program including your driver program (main) so I can help you fix your problem. Using the pow function requires two floats or two doubles, and I would have to modify your program a bit to support pow(), and oddly enough when I did it crashed. ^^ But I am assuming it's because of your earlier functions. So please post the updated functions/driver program.
Hmm, alright...
convert.h
(is suppost to include different conversion methods,
like base2 (bin), base 4, base 16 (hex), and base 64,
but as you can see, I'm stuck on hex. :)
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */
/* |||||||||| Convert.h - base converting functions ||||||||||| */
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */
// ================
// Headers
// ================
#include <string.h>
#include <stdio.h>
// ==========
// Prototypes
// ==========
void hex(long, char*);
void bin(long, char*);
long power(int, int);
// ==============================================
// Functions:
//
// Note: Most functions are void, because I don't
// know how to return a character array
// properly. :(
// ==============================================
void hex(long number, char *s1) {
char buftmp[256];
char *binbuf = new char[256];
char hextmp[4];
char buffer[256];
int i=0;
int holdnum=0;
// ====================================
// Convert number binary
// ====================================
binbuf[0] = 0;
bin(number, binbuf);
// ==============
// Convery to hex
// ==============
while (strlen(binbuf) > 0) {
strncpy(hextmp, binbuf, 4);
for (i=0; i<4; i++) {
if (hextmp[i] == '1') {
holdnum += power(2, 4 - (i + 1));
}
}
// =============
// Add to buffer
// =============
strcpy(buftmp, buffer);
if (holdnum < 10) {
sprintf(buffer, "%s%d", buftmp, holdnum);
} else {
switch (holdnum) {
case 10: sprintf(buffer, "%sA", buftmp); break;
case 11: sprintf(buffer, "%sB", buftmp); break;
case 12: sprintf(buffer, "%sC", buftmp); break;
case 13: sprintf(buffer, "%sD", buftmp); break;
case 14: sprintf(buffer, "%sE", buftmp); break;
case 15: sprintf(buffer, "%sF", buftmp); break;
}
}
holdnum = 0;
binbuf += 4;
}
// return
strcpy(s1, buffer);
}
void bin(long number, char *s1) {
int remainder;
int counter2 = 0;
char bufhold[256];
char buffer[256];
if (number == 0) {
strcpy(s1, "00000000\0");
return;
}
while (number > 0) {
remainder = number % 2;
number /= 2;
strcpy(bufhold, buffer);
sprintf(buffer, "%d%s", remainder, bufhold);
counter2++;
}
buffer[counter2] = 0;
// ==============
// Pad with zeros
// ==============
while (strlen(buffer) % 8 > 0) {
strcpy(bufhold, buffer);
sprintf(buffer, "0%s", bufhold);
}
// return
strcpy(s1, buffer);
}
long power(int base, int exponent) {
long holdnum=base;
int i;
if (exponent == 0) { return 1; }
for (i=1; i<exponent; i++) { holdnum = holdnum * base; }
return holdnum;
}
//void strtohex(char *s1, char *s2) {
//}
//void strtobin(char *s1, char *s2) {
//}
main.cpp (for testing):
#include <stdio.h>
#include <conio.h>
#include "convert.h"
int main() {
// test hex
char test[256];
hex(255, test);
printf("0x%s\n", test);
getch();
return 0;
}
Incidentally, I don't feel that there's anything wrong with strcpy in certain situations. If you have a 256 character array, and you're copying a 30 character string literal to it, it's not going to harm anything.
Then use strncpy with a 256 argument.
It's slower because it has to do a checkbound and it's more typing.
And that, my friends, is why IIS has so many holes.
Quote from: $t0rm on July 23, 2004, 08:23 AMAnd that, my friends, is why IIS has so many holes.
huh? Zak's exactly right. strcpy with a hardcoded second argument is just fine, and there's absolutely no reason to go using strncpy in that situation. Of course, this assumes the code will only be maintained by people who aren't dumb enough to go shrink a buffer indiscriminately, although even shrinkage would be nothing more than an automatic stack corruption / automatic crash when run this code. :P Besides which, a good compiler can notice that you've hardcoded the constant and inline it as some constant stores.
Oh, I didn't see that he wanted a string constant.