66841

Store value in pointer var

Question:

How to store int values in *pData and display values from it?

int id = 12; int age = 14; unsigned char* pData = new unsigned char[8]; memcpy(pData,&id,4);/* using memcpy to copy */ pData = pData + 4; memcpy(pData,&age,4);/* using memcpy to copy */ // How to print value from buffer *pData

Answer1:

After using memcpy to copy the bytes of an int into an unsigned char buffer, the only correct way to display the ints is to copy the bytes back into an int. For example:

int temp_int; memcpy(&temp_int, pData, sizeof temp_int); std::cout << temp_int << '\n'; memcpy(&temp_int, pData + sizeof temp_int, sizeof temp_int); std::cout << temp_int << '\n';

Attempting to reinterpret the buffer as an int would cause undefined behaviour by violating the strict aliasing rule.

Answer2:

you can change pData into int *, then you can print the int valve.

<hr />cout<<*((int *)pData);

Answer3:

memcpy(pData,&id,4);

This line copies the four bytes data in id to pData as int but you declared it as char * . if you declare it as int *pData = new int[2]; then you can ptint exact values.

using namespace std; int id = 12; int age = 14; unsigned int* pData = new unsigned int[2]; memcpy(pData,&id,4); pData = pData + 1; memcpy(pData,&age,4); pData = pData - 1; cout<<"ID:"<<pData[0]<<"\nAge:"<<pData[1]<<endl;

This will print the values.

Answer4:

If you really need to allocate memory as unsigned char*, you can do it like below:

#include <iostream> #include <cstring> using namespace std; int main() { int id = 12; int age = 14; // const pointer to buffer which can contain 2 ints unsigned char* const pData = new unsigned char[2*sizeof(int)]; // non-const pointer to operate on data unsigned char* pDataPtr = pData; memcpy(pDataPtr,&id,sizeof(int)); pDataPtr += sizeof(int); memcpy(pDataPtr,&age,sizeof(int)); std::cout<<*reinterpret_cast<int*>(pData)<<std::endl; std::cout<<*reinterpret_cast<int*>(pData + sizeof(int))<<std::endl; delete [] pData; return 0; }

Answer5:

You can print pData using reinterpret_cast operator :

#include <iostream> #include <cstring> int main(void) { int id = 12; int age = 14; const size_t size = sizeof(int); unsigned char* pData = new unsigned char[2*size]; memcpy(pData,&id,size);/* using memcpy to copy */ pData = pData + size; memcpy(pData,&age,size);/* using memcpy to copy */ std::cout<<*reinterpret_cast<int*>(pData)<<std::endl; pData = pData - size; std::cout<<*reinterpret_cast<int*>(pData)<<std::endl; delete []pData; return 0; }

Recommend

  • Help me out with this MySql full outer join (or union)
  • Edit C Compiler used in MySQLdb Python
  • Stretch Bitmap without anti-aliasing
  • Merging result from 2 columns with same name and not over-writing one
  • MySql - use of ALIAS for multiple columns
  • can't install MySQL-Python
  • using restrict qualifier with C99 variable length arrays (VLAs)
  • How is a float represented hexadecimally?
  • How to set up multiple aliased joins between two models?
  • Mvapich2 buffer aliasing
  • (How) am I supposed to destroy a uv_async_t?
  • White spots when drawing mutliple lines close to each other
  • Converting a list of strings to ints (or doubles) in Python
  • How to copy pandas DataFrame values down to fill 0's?
  • Division in C# to get exact value [duplicate]
  • OSStatus error -50 (invalid parameters) AudioQueueNewInput recording audio on iOS
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • How to work with AMMediaType for video filters
  • Many to Many in Linq using Dapper
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • c++ regex_replace not doing intended substitution
  • How to unpack 32bit integer packed in a QByteArray?
  • Building Qt project for C++11 standard
  • C++ friend class std::vector
  • Access user's phone number on iOS 7
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Web.config system.webserver errors
  • Reduction and collapse clauses in OMP have some confusing points
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • MongoError: Incorrect arguments
  • Read a local file using javascript
  • Why is the size of this struct 32?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • 0x202A in filename: Why?
  • File not found error Google Drive API
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal