• Welcome to Valhalla Legends Archive.
 

Another damned pointer issue!

Started by Eli_1, July 16, 2004, 05:04 PM

Previous topic - Next topic

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:

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.

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.

Maddox

#2
It would help if you pasted all of the 2nd function.
asdf.

Sargera

And are you sure it's happening at that exact statement?

Eli_1

#4

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;

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.

Eli_1

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?


Sargera

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.

Banana fanna fo fanna

DON'T
USE
STRCPY!

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

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:

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);

}

Eli_1

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?

Sargera

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.  ;)

Banana fanna fo fanna

NEVER use it; ALWAYS strncpy.

Sargera

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?

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.