Valhalla Legends Forums Archive | C/C++ Programming | Another damned pointer issue!

AuthorMessageTime
Eli_1
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:
[code]
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".

...
[/code]



Function 2:
[code]
void bin(long number, char *s1) {
...
// return
strcpy(s1, buffer);
}
[/code]


Can someone help me figure out what I'm doing wrong? TIA.
July 16, 2004, 10:04 PM
Adron
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.
July 16, 2004, 10:15 PM
Maddox
It would help if you pasted all of the 2nd function.
July 16, 2004, 10:37 PM
Sargera
And are you sure it's happening at that exact statement?
July 16, 2004, 10:40 PM
Eli_1
[code]
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);
}
[/code]


[quote author=Sargera link=board=30;threadid=7734;start=0#msg70865 date=1090017646]
And are you sure it's happening at that exact statement?
[/quote]

No. All I know is the function worked when I called it like this:

[code]
char test[256];

bin(100, test);
[/code]

And gave me this error when I called it like this:
[code]
char *test = new char[256];

bin(100, test);
delete test;
[/code]
July 16, 2004, 10:41 PM
Sargera
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.
July 16, 2004, 10:47 PM
Eli_1
[quote author=Sargera link=board=30;threadid=7734;start=0#msg70867 date=1090018058]
Use a debugger then. Your error is probably happening in the body of the function, not the function call. [/quote]

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?

July 16, 2004, 10:49 PM
Sargera
[quote author=Eli_1 link=board=30;threadid=7734;start=0#msg70868 date=1090018177]
[quote author=Sargera link=board=30;threadid=7734;start=0#msg70867 date=1090018058]
Use a debugger then. Your error is probably happening in the body of the function, not the function call. [/quote]

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]

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.
July 16, 2004, 10:55 PM
St0rm.iD
DON'T
USE
STRCPY!

If I ever catch you doing that again, I'm taking away your C++ license.
July 16, 2004, 11:23 PM
Eli_1
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:
[code]
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);

}
[/code]
July 16, 2004, 11:27 PM
Eli_1
[quote author=$t0rm link=board=30;threadid=7734;start=0#msg70875 date=1090020193]
DON'T
USE
STRCPY!

If I ever catch you doing that again, I'm taking away your C++ license.
[/quote]

Eeek! What's wrong with strcpy?
July 16, 2004, 11:28 PM
Sargera
[quote author=Eli_1 link=board=30;threadid=7734;start=0#msg70877 date=1090020518]
[quote author=$t0rm link=board=30;threadid=7734;start=0#msg70875 date=1090020193]
DON'T
USE
STRCPY!

If I ever catch you doing that again, I'm taking away your C++ license.
[/quote]

Eeek! What's wrong with strcpy?
[/quote]

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. ;)
July 16, 2004, 11:43 PM
St0rm.iD
NEVER use it; ALWAYS strncpy.
July 17, 2004, 12:00 AM
Sargera
[quote author=Eli_1 link=board=30;threadid=7734;start=0#msg70876 date=1090020429]
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:
[code]
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);

}
[/code]
[/quote]

Is power() one of your functions or is it pow() from the library cmath?
July 17, 2004, 12:23 AM
Eli_1
power is my function. I wasn't aware there was a pow function untill after I wrote the function and saw it on google.
July 17, 2004, 12:46 AM
Sargera
[quote author=Eli_1 link=board=30;threadid=7734;start=0#msg70888 date=1090025191]
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]

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.
July 17, 2004, 12:48 AM
Eli_1
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. :)

[code]
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */
/* |||||||||| 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) {
//}
[/code]



main.cpp (for testing):
[code]
#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;
}

[/code]
July 17, 2004, 12:54 AM
Zakath
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.
July 23, 2004, 2:48 AM
St0rm.iD
Then use strncpy with a 256 argument.
July 23, 2004, 2:55 AM
Sargera
It's slower because it has to do a checkbound and it's more typing.
July 23, 2004, 4:12 AM
St0rm.iD
And that, my friends, is why IIS has so many holes.
July 23, 2004, 1:23 PM
Kp
[quote author=$t0rm link=board=30;threadid=7734;start=15#msg72026 date=1090589024]And that, my friends, is why IIS has so many holes.[/quote]

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.
July 23, 2004, 6:38 PM
St0rm.iD
Oh, I didn't see that he wanted a string constant.
July 24, 2004, 2:18 AM

Search