Assigning value to allocated char array fails - c++

I simply allocate some memory for a character and wanna do then some pointer arithmetic.
In this case I wanna write '\x0a' to byte 32 as follows:
#define HDR_SIZE 32
int size = 52;
unsigned char *readXPacket = (unsigned char *) malloc (size * sizeof (unsigned char));
*readXPacket + HDR_SIZE = '\x0a';
When I try doing that I get the following error message: non-value in assignment. Anyone an idea what is wrong here?
Thanks

Change your assignment to:
*(readXPacket + HDR_SIZE) = '\x0a';

What's wrong with the obvious:
readXPacket[HDR_SIZE] = '\x0a';
which is both shorter and clearer. and as you are using C++, why not say:
unsigned char * readXPacket = new unsigned char[size];
Or better still:
std::vector <unsigned char> readXPacket( size );
and have C++ manage the memory for you.

Try...
*( readXPacket + HDR_SIZE ) = '\x0a';

Related

how do I solve this C++ access violation problem?

I'm getting an error in the following code. Visual Studio throws an access violation error when writing to _buf. How can I fix this?
The Sendn function is a socket sending function. It's not the problem, you can ignore it.
It looks like _buf points at 0x00000000
The error message I'm seeing is
0xC0000005: 0x00000000 : access violation
void ?????::?????(int number, string title)
{
int titlesize = sizeof(title);
int bufsize = 4 + 4 + 4 + titlesize;
char *_buf = new char[bufsize];
_buf = { 0 };
// char _buf[bufsize] = { 0 }; (수정 내용)
int commands = 3;
int index = 0;
memcpy(_buf, &commands, sizeof(int));
index += sizeof(int);
memcpy(_buf + index, &number, sizeof(int));
index += sizeof(int);
memcpy(_buf + index, &titlesize, sizeof(int));
index += sizeof(int);
for (int i = 0; i < titlesize; i++)
{
memcpy(_buf + index, &title[i], sizeof(char));
index += sizeof(char);
}
Sendn(_buf, bufsize);
delete[] _buf;
return;
}
char *_buf = new char[bufsize];
_buf = { 0 };
This does not zero-fill the dynamically-allocated array pointed to by _buf. It sets the pointer _buf to be a null pointer. Since _buf is a null pointer, later attempts to dereference it lead to undefined behavior.
There's no need to zero-fill the array pointed to by _buf in this case, so you can simply remove the _buf = { 0 }; line.
Once you've fixed that problem, you also aren't allocating the right amount of memory. sizeof(title) will not give you the number of characters that title holds. It just gives you the static size of a std::string object, which is usually only a pointer and two integers. Use title.size() instead.
You're trying to copy the content of title together with 3 other integer numbers into _buf right? The problem is that sizeof(title) is not the length of the string stored in title. In order to get the length of title, you need to call the member function length on type std::string like this:
auto titlesize = title.length();
The sizeof operator only gives you the size of your std::string object on stack (in comparison, the actual string is stored on heap) and sizeof expressions are always constant expressions. On my computer, sizeof(std::string) is 24 regardless of what the actual string is.

Read/Write a single byte of a void* variable

If I have
void *temp = malloc(128);
memset(temp, 0 , 128);
And I want to read the first byte alone, following is what I'm doing.
char a[2];
strncpy(a, (char*)temp, 1);
int p = a[0];
//p will be zero in this case
Q1. I'm sure there is a more elegant way to achieve the same. If so, what would it be?
Q2. Is there a way I can alter the value of that single byte alone?
Say I want the first byte to have the value equivalent to the int value 48 (i.e. 00110000)
How would I do that?
I was able to make no progress with the write.
you can cast it to char * then access the memory
char *buff = temp;
char p = buff[0]; // read first byte

typecast array of shorts into bytearray

I am trying to convert a short array into a bytearray in qt.
is there any function available to do the casting.
if i have to use const char * for conversion how should i do it.
and is there any better way than to use reinterpret cast
Thanks in advance.
You can convert an array of short to bytes simply by casting a byte pointer to the short array
short s[10];
unsigned char *p = reinterpret_cast<unsigned char*>(s);
then use the pointer to go through all the bytes in the array where you copy the *p to a byte array if u wish.
for ( unsigned char *p = reinterpret_cast<unsigned char*>(s);
p < s + sizeof(s);
++p)
{...}
You can use reinterpret_cast to do this, but be aware that you are making your code very architecture specific. Use of reinterpret_cast doesn't exactly guarantee your code is wrong, but it should be ringing warning bells.
If what you want to do is, given an array of shorts, produce an array of bytes with the same values, you probably want this:
void copy(char *to_byte_array, short const *from_short_array, std::size_t size)
{
for (std::size_t pos = 0; pos != size; ++pos)
{
to_byte_array[pos] = from_short_array[pos];
}
}
if you use reinterpret_cast, your short array containing say 20, 30, 40, will look like an array of chars containing 0, 20, 0, 30, 0, 40 (or possibly 20, 0, 30, 0, 40, 0, depending on architecture).

Writing data to memory in C++

I want to write a a mix of int, char, real in void *data.
I am using a file pointer to run through the data block.
Now my question is that since the data type is void, I have to typecast it to int while writing integer and char for writing string.
While typecasting I used the following sample code:
*((int *)data+0) = 14; //writing int
*((int *)data+4) = 5; //writing int, left a space of 4 bytes for int
*((char *)data+8) = 'a'; //writing char
*((char *)data+9) = 'f'; //writing char
But then while reading the values back it didnt give the correct value.
cout<<*((int *)data+0);
cout<<*((int *)data+3);
cout<<*((char *)data+8);
Is the way my code is written correct? I am doubtful about it as data is void.
*((int *)data+4) = 5; // writing 4th int
cout<<*((int *)data+3); // but reading third one
And just in case, ((int *)data+4) points to 4th integer (that is, 16th byte given int size = 4), not to 4th byte. That is, you code overwrites bytes 0-3, then 16-19, then 8th, then 9th. What you probably meant is: *(int *)( (char*)data + X )
Edited to correct mistake pointed out by MSalters
Apart from the typo that others have mentioned (data+3 instead of data+4), you also need to change e.g.
*((int *)data+4)
to
*((int *)data+1)
because adding 4 to an int * doesn't add 4 to the address, it adds 4 * sizeof (int).
If you need to write to an offset that is not a multiple of sizeof(int) (say, 7), you need:
*(int *)((char *)data+7)
For this reason, it might be better to make data a char * to start with, so you can just say
*(int *)(data+7)
Use a class or a struct.
Here, pointer arithmetics is misleading you. When you add 4 to an int * you are adding actually four times sizeof int.
If your data has a constant layout, why don't you just use a struct such as
struct MemoryLayout {
int _first;
int _second;
char _c1;
char _c2;
};
?
You're writing the second one with:
*((int *)data+4) = 5; // offset = 4
and reading it back with:
cout<<*((int *)data+3); // offset = 3
In addition, the (int*) cast is binding to data, not data+4 so that your 4 is scaled up by the size of an int.
If you really want to do this (and a struct is not a possibility due to variances in the data formats), you should cast data to a char*, then add the number of bytes to get the char offset, then cast that to your desired type.
That would be something like:
*((int*)((char*)(data + 0)) = 14; //writing int
*((int*)((char*)(data + 4)) = 5; //writing int
*((char*)data + 8) = 'a'; //writing char
*((char*)data + 9) = 'f'; //writing char
int Data;
//char Data;
//float Data;
FILE *File = fopen("File.txt","wb");
fwrite((char *)&Data,sizeof(Data),1,File);
fclose(File);
File = fopen("File.txt","rb");
fread((char *)&Data,sizeof(Data),1,File);
fclose(File);
...
int DataInt1 = 200;
char DataChar1 = 'N';
FILE *File = fopen("File.txt","wb");
fwrite((char *)&DataInt1,sizeof(DataInt1),1,File);
fwrite((char *)&DataChar1,sizeof(DataChar1),1,File);
fclose(File);
int DataInt2 = 0;
char DataChar2 = 0;
File = fopen("File.txt","rb");
fread((char *)&DataInt2,sizeof(DataInt2),1,File);
fread((char *)&DataChar2,sizeof(DataChar2),1,File);
fclose(File);
printf("%d %d!\n",DataInt2,DataChar2);

what is wrong here

unsigned __int8 result[]= new unsigned __int8[sizeof(username) * 4];
IntelliSense: initialization with '{...}' expected for aggregate object
The types are not the same; you cannot initialize an array with a pointer.
new unsigned __int8[sizeof(username) * 4]; returns a unsigned __int8*, not unsigned __int8[]
change your code to
unsigned __int8* result = new unsigned __int8[sizeof(username) * 4];
unsigned __int8 *result = new unsigned __int8[sizeof(username) * 4];
new returns a pointer, not an array. You should declare
unsigned __int8* result = ....
here, result is an array of __int8, so you can't assign one value into the entire array. You actually want:
unsigned __int8* p_result = new unsigned __int8[sizeof username * 4];