|
I removed all of the storage allocations out of my DLL and it seemed to worked by tonite I should be able put this piece to rest.
if not i will consider and/or take your advice
thanks
|
|
|
|
|
Hi
I have the following code which I basically take a hex array and convert it to a ascii char * e.g 0X47F0F01A so the array becomes 303770307030316500
I marked off where I get dialogbox message from CRT and I cannt for the life of me figure out why
the memcpy where I mark off the string with a null is 8 bytes into the 70 bytes allocated
char* convertToString(unsigned char* a, int size)
{
int i;
char ch;
char getch(char);
char* t = new char[70];
char* k = t;
char thenullptr = 0x00;
char* s;
if (size == 80)
__debugbreak();
for (i = 0; (i < size && i < 62); i++) {
ch = a[i];
ch = ch >> 4;
ch = ch & 0x0f;
ch = getch(ch);
memcpy(k, &ch, 1);
k++;
ch = a[i];
ch = ch & 0x0f;
ch = getch(ch);
memcpy(k, &ch, 1);
k++;
}
if (i >= 62)
{
s = new char[63];
::memcpy(s, t, 62);
k = s + 62;
::memcpy(k, &thenullptr, 1); <------- call which causes read exception or overwrite
delete t;
return s;
}
::memcpy(k, &thenullptr, 1);
s = new char(strlen(t) + 1);
;
::memcpy(s, t, strlen(t) + 1);
delete t;
return s;
}
char getch(char ch)
{
if (ch <= 0x09)
ch += 48;
else
ch += 55;
return ch;
}
|
|
|
|
|
ForNow wrote:
s = new char[63];
::memcpy(s, t, 62);
k = s + 62;
::memcpy(k, &thenullptr, 1); <------- call which causes read exception or overwrite
why are you doing it so complicated?
Why not just
s = new char[63] {0};
::memcpy(s, t, 62);
and you won't need to copy the terminated 0x00 byte.
Or just set it form the very begin:
s = new char[63];
s[62] = '\0';
::memcpy(s, t, 62);
modified 10-Jan-23 11:17am.
|
|
|
|
|
Ok let me redo it thanks for responding thing is it doesn’t happen the first time the sub gets called only after a number if calls
Thank you again
|
|
|
|
|
That would suggest you probably have a bug elsewhere in you program. Maybe a buffer overflow or an uninitialized variable is writing where it shouldn't. If you haven't already, turn up the warning level on the compiler, and then fix everything it flags. If this still happens, you'll need to dig into your debugger and look into watch points and the like.
Keep Calm and Carry On
|
|
|
|
|
You are using new[] , so you must use a matching delete[] :
char* t = new char[70];
delete[] t;
This might not be the cause of the problem you are having, but it will cause problems.
|
|
|
|
|
Just saw that but that is only for C++ as I build this DLL as C
Before thank you
|
|
|
|
|
C doesn't have new/delete at all - you are writing C++
|
|
|
|
|
|
that's is my scenario I pass a hex array pointer to DLL function and allocate the storage there and pass the pointer back to the application I have to read what you sent I have Dr appointment in 15 min but thanks for the info
|
|
|
|
|
You are welcome.
BTW, Good luck with the doctor.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
If I got you, the code should return the hexadecimal representation of a bunch of bytes.
In order to avoid CRT-conflicts, you have to make the caller responsible of the output buffer allocation/deallocation. Something like this
#include <iostream>
#include <cstddef>
using namespace std;
size_t to_hex( unsigned char in[], size_t size_in, char out[], size_t size_out)
{
size_t n;
for (n=0; (n < size_in) && (n < (size_out-1)b/2); ++n)
{
unsigned char nibble;
nibble = (in[n] >> 4); out[2*n] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
nibble = (in[n] & 15); out[2*n+1] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
}
if ( 2*n < size_out)
out[2*n] = '\0';
return n;
}
int main()
{
unsigned char small[] = { 0x25, 0x37, 0x48, 0x42, 0x42 };
char buffer[41];
size_t size = to_hex( small, sizeof(small), buffer, sizeof(buffer));
cout << "converted bytes " << size << ", hex string " << buffer << "\n";
unsigned char large[256];
for (size_t n=0; n<256; ++n)
{
large[n] = n;
}
size = to_hex( large, sizeof(large), buffer, sizeof(buffer));
cout << "converted bytes " << size << ", hex string " << buffer << "\n";
}
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Ooops, wrong forum section. Can a moderator please delete this thread (the Delete link is disabled for me)?
modified 10-Jan-23 8:24am.
|
|
|
|
|
That doesn't look like C++ to me. Are you sure you didn't mean to post this in the C# forum[^] instead?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
If this is truly C# code, mightn't you be better served posting it in the C# forum?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Hi
I have a number of hex data I am trying to get to printable data I mean ascii
I read them as a byte[] array where byte is typedef for usigned char
I just got this idea it might be easier to manipulate it as string i mean like std::string
so I developed this routine
string convertToString(unsigned char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
s += a[i];
}
return s;
}
Here is the piece of code that calls it
returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4)
here is the definition
BYTE statementnumber[4];
the data in statement ithe statement number in hex so statement 1 would be
x'00000001'
so my goal the would be to retrun
retrun->stmt as character 0001 or x30303031
Now me thinks that since return->stmt is defined as a string
struct source
{
string stmt;
string loc;
string addr1;
string addr2;
that the string operator= is invoked becasue of this
I end up here in Microsoft code in a member utility and get a read exception
this is the path
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\include\utility
is there any way I can tell MSVC compiler that all I am trying to do with = operator is accept a return value from my subroutine
template <class _Ty, class _Other = _Ty>
_CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
conjunction_v<is_nothrow_move_constructible<_Ty>, is_nothrow_assignable<_Ty&, _Other>>) {
_Ty _Old_val = static_cast<_Ty&&>(_Val);
_Val = static_cast<_Other&&>(_New_val);
return _Old_val;
}
|
|
|
|
|
Since one of the constructors for std::string is
std::string(const char *, size_t count) you can do your assignment using
unsigned char datum[6]{ 'A', 'B', 'C', 'D', 'E', 'F' }; std::string str(reinterpret_cast<const char *>(datum), 4);
Since you're getting a read exception, that suggests to me that given
returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4); then probably your sourcrecordpointer is invalid.
Keep Calm and Carry On
|
|
|
|
|
Just an fyi
When I removed the return value
Meaning
I just had convetToString(sourcerecordpointer->statementnumber,4);
I didn’t get the exception me thinks it has something to do with the lvalue
|
|
|
|
|
ForNow wrote: I have a number of hex data I am trying to get to printable data I mean ascii
I read them as a byte[] array where byte is typedef for usigned char
I just got this idea it might be easier to manipulate it as string i mean like std::string
I think it is not a good idea to consider a byte array as a string. It is only possible if you are 100% sure there is no any NULL (0x00) byte within this byte array, otherwise you string would represent only the part of the array up to the first NULL byte.
|
|
|
|
|
That’s a good idea when I read the byte array for any value between 0x00 and 0x09 I’ll add 48
Those values between 0x0a and 0x0f I’ll add 55
After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?
|
|
|
|
|
ForNow wrote: After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?
If you mean an std::string then you you don't have to add a terminating null character.
|
|
|
|
|
Thanks I think the fact that I was inserting a null byte into a string caused the assertion
Thank you
|
|
|
|
|
I compiled, with Visual C++ 2022, and run (successfully) the following code:
#include <iostream>
using namespace std;
typedef unsigned char BYTE;
struct source
{
string stmt;
string loc;
string addr1;
string addr2;
};
string convertToString(unsigned char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
s += a[i];
}
return s;
}
int main()
{
BYTE statementnumber[4];
source * returnsource = new source;
returnsource->stmt = convertToString(statementnumber, 4);
delete returnsource;
}
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
I look at my code again the only difference I see is that my code is part of a DLL the pointer “return source” is a parameter passed by the invoking program it allocated on the heap via new
Thanks
|
|
|
|
|
Hi,
I want to work with a MYSQL database with jdbc using C++.
I have started with connection and I saw in an example the following statement:
con = driver->connect("host", "usert", "password");
I found the connect declaration in the driver.h header as a connection type, but I don't know where to find the definition of the connect.
Can anybody help me please with finding its definiton?
Thank you,
|
|
|
|