Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9121359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:50:40+00:00 2026-06-17T05:50:40+00:00

I have this following code size_t returnSize(const char* s) { string string(s); return string.size();

  • 0

I have this following code

size_t returnSize(const char* s)
{
       string string(s);
       return string.size();
};

size_t returnSize(const int& i)
{
       return sizeof(i);
};


template<typename T>
vector<char> Serialize(const T& t)
{
    T* pt = new T(t);
    vector<char> CasttoChar;

    for (int i =0 ;i<returnSize(t);i++)
    {
        CasttoChar.push_back(reinterpret_cast<const char*>(pt)[i]);
    }
    delete pt;
    return CasttoChar;
};
template<typename T>
T DeSerialize(const vector<char> cstr)
{
    T* a = (T*)(&cstr[0]);

    return *a;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int x = 97;
    vector<char> c = Serialize(x);
    cout << DeSerialize<int>(c) << endl;

    string k = "blabla";
    vector<char> c3 = Serialize(k.c_str());
    cout << DeSerialize<const char*>(c3) << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

//output is 
//97
//blabla

Is this line T* a = (T*)(&cstr[0]); safe?

Also, I tried reinterpret_cast<T*>(&cstr[0]); instead of T* a = (T*)(&cstr[0]); but compiler complained about not being able to convert const char* to int*. so why does the C style cast work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:50:41+00:00Added an answer on June 17, 2026 at 5:50 am

    Refer the standard

    Why reinterpret_cast fails?

    5.2.10 Reinterpret cast [expr.reinterpret.cast]

    The reinterpret_cast operator shall not cast away constness (5.2.11).
    An expression of integral, enumeration, pointer, or pointer-to-member
    type can be explicitly converted to its own type; such a cast yields
    the value of its operand.

    Should I use C Cast?
    No. Using C Cast instead of C++ Cast is always unsafe. You are trying to remove the constness of an Object which is an UB.
    Using reinterpret_cast, will actually trap this error and advise you of during compile time of the potential pitfall.

    You should actually use const_cast in this situation. Its the only legal way to convert a const object to a non const object

    But Why does a C Cast works

    Quoting from the accepted answer from the Question When should static_cast, dynamic_cast and reinterpret_cast be used?

    A C-style cast is defined as the first of the following which
    succeeds:

    const_cast
    static_cast
    static_cast, then const_cast
    reinterpret_cast
    reinterpret_cast, then const_cast
    

    So fortunately, it tries the const_cast first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following code: void* my_alloc (size_t size) { return new char
IE what happens if you have this following piece of code? int mean(const vector<int>
Under $(document).ready(function() , I have the following code: num = $(#mainContentCategoryLinksWrapper > div).size(); This
I have this following code, here CssClass is of the String type. <asp:Panel ID=Panel1
I have this following code which will return all the current semesters. How do
I have the following code @implementation UIDevice(machine) - (NSString *)machine { size_t size; //
i have this following php code : $filename = '/front/style.css'; $cssfile='#h1{font-size:12px}'; if($id_file=fopen($filename, w+)) {
I have this following jquery text fly-in animation.Here is my code before I explain
Never thought I'd have this problem :) The following snippet of code works in
I have the following code: Parallel.ForEach(this.listView2.CheckedItems, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (CheckedItem)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.