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 9174401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:45:04+00:00 2026-06-17T16:45:04+00:00

After a debug session I realised that the next sample code compile with no

  • 0

After a debug session I realised that the next sample code compile with no warnings (g++-4.7.2 -Wextra and -Wall)

char *p;
char *q = std::move(p);
*p = 'p';
*q = 'q';

I’m a bit shocked, and I have three questions about it.

1- Am I missing something about move semantics and there is a reason for not issuing a warning in this situation, or is gcc’s checking not good enough?

2- Should the next code throw a warning?

char c = 'c';
char *p = &c;
char *q = std::move(p);
*p = 'p';

As far I understand, after std::move(p) p can hold any value, therefore once p is moved is like an uninitialized variable.

3- Can other tools/compilers check these errors?

  • 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-17T16:45:05+00:00Added an answer on June 17, 2026 at 4:45 pm

    1- I’m missing something about move semantics and there is a reason for no throw a warning in this situation, or gcc checking is not good enough?

    The code is wrong, so a warning would be good. G++ isn’t smart enough to “see through” the call to std::move. It’s valid to pass an uninitialized variable by reference to a function (that function might initialize the variable) so calling std::move itself doesn’t trigger a warning. Because q gets assigned a value, it appears to be initialized to the compiler.

    If I turn on optimization so that G++ inlines the call to std::move then I get an error from G++ 4.7:

    f.cc: In function ‘int main()’:
    f.cc:6:9: warning: ‘p’ is used uninitialized in this function [-Wuninitialized]
    

    This is because the call to std::move is no longer “opaque” to the compiler, when it analyses the inlined code it can see that p never gets assigned a value. The compiler still isn’t smart enough to see that q never gets a good value, the cast inside std::move probably confuses the compiler.

    2- The next code should throw a warning?

    No. std::move doesn’t alter fundamental types such as pointers, so the value of p will not be changed. The standard library says that objects are left in a “valid but unspecified” state after being moved from, because the standard doesn’t generally define the exact behaviour of a move constructor or move assignment operator, but for fundamental types such as int and char* there is no move constructor. std::move(p) just casts the object to an rvalue, it doesn’t alter it, and initializing q with the value doesn’t alter it either – it just copies the value.

    3- Other tools/compilers can check these errors?

    Clang and ICC fail to warn about the first example even with optimization on.

    Note that although warnings are very useful, compilers are not perfect and it is impossible to warn about all unsafe code. You should not be totally surprised when unsafe code doesn’t get a warning (maybe open a bug report for the compiler to request it be improved) — it doesn’t mean the code is OK. Absence of warnings does not imply absence of bugs.

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

Sidebar

Related Questions

I have noticed that after awhile of debug/tweakcode/debug etc that eventually Silverlight starts to
Is there any way to debug what is going on after my .NET code
Seems that even after unchecking the option in the PyDev/Debug preferenecs pane to launch
I just had a long debug session that was caused by a stale interface
I use ant debug to build my project. After building with this command, my
Just finished a very long debug session. Please help me understand what root-cause/bad-practice caused
I commonly place into variables values that are only used once after assignment. I
After my queries(lookups) get cached session closes, in a new session, hibernate is evicting
I am using the command reset_session as per here to avoid Session Fixation. After
I'm trying to connect, using Net::SSH, to a server that immediately after login executes

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.