Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #12151

Re: Argument type of function and safe types and types, arguments can handle safely

From 88888 Dihedral <dihedral88888@googlemail.com>
Newsgroups comp.lang.c++
Subject Re: Argument type of function and safe types and types, arguments can handle safely
Date 2011-12-08 00:31 -0800
Organization http://groups.google.com
Message-ID <24498188.511.1323333110485.JavaMail.geo-discussion-forums@prgg19> (permalink)
References <jbplnk$qrf$1@dont-email.me> <Xns9FB568AC8EB33myfirstnameosapriee@216.196.109.131>

Show all headers | View raw


On Thursday, December 8, 2011 4:17:22 PM UTC+8, Paavo Helde wrote:
> ittium <itt...@gmail.com> wrote in news:jbplnk$qrf$1...@dont-email.me:
> 
> > Groups,
> > I have few doubts regarding argument type of function and types, 
> > arguments can handle safely. 
> 
> It depends on what you mean by "safely". It may mean a lot of things, 
> starting from "the code is so clear, robust and self-contained that it 
> will not need any change or refactoring in the next 30 years" up to "the 
> argument content can be accessed safely from other threads while the 
> first thread is sitting in this function call, but only in physically 
> read-only mode, and until a given semaphore elsewhere in the code has not 
> been signalled".
> 
> > I know these issues have been discussed at 
> > multiple places on internet. I wanted to see the big picture 
> 
> The big picture is that the language provides a multitude of different 
> ways to pass information, each with their own trade-offs, and the 
> programmer is obliged to select the correct one according to the actual 
> needs. And the devil is in the details as always.
> 
> > 
> > When you pass by value - Copy constructor is called (except for basic 
> > data type like int, float). 
> 
> Copy ctor calls may be elided by the compiler, even when it can cause 
> visible side-effects.
> 
> > you can pass the **same type variable** only
> > 
> 
> Nope, you can pass derived type objects as well, but they get sliced 
> down. Not sure if this qualifies as "safely".
> 

> A const reference can also bind to a temporary if direct binding is not 
> possible, and there is a suitable conversion available. Some of the 
> nastiest bugs I have seen were due to a (buggy) compiler binding a non-
> const reference to an automatically created temporary.
> 

 A small structure, say 16 to 128 bytes in the stack, is not big and can be
accessed efficiently than a big chunk of a multi-dimensional array that sits
on a heap classified in  L2 or the lousy slow DRAM banks.

This assumes that the stack is accessed in L0 or L1 in the run time. 

No deep stack probing and fat chunks in the heap is the reason to copy by 
values of 16 to 128 bytes. 
  
> > When you pass by pointer - pointer is assigned to passed value  you can 
> > pass **same type of derived type**.
> 
> You ignore the const-non-const topic here, which for pointers is both 
> simpler (no converted temporaries) and more complex (pointers themselves 
> can be const or non-const, which turns especially tricky with several 
> levels of indirection).
> 
> > 
> > Cases written above are safe conversion (no casting is needed). **C++ 
> > compiler will not allow any other conversion **. You can still use 
> > casting for not allowed cases.
> 
> Only if this does not make you code invalid ;)
> 
> > 
> > My doubts are
> > - Have I captured all the cases or there are some more cases (I will 
> > appreciate a link to a small reference)
> 
> There certainly are; string literals and rvalue references pop in mind 
> first.
> 
> > 
> > -What casts are safe e.g in case of pass by value casting from short to 
> > int seems perfectly safe. (I will appreciate a link to any document 
> > explaining safe casting)
> 
> Much more tiny evil details. Short-to-int does not require a cast, at 
> least not when signedness is the same. Otherwise, each cast has its own 
> rules. Static_cast validness depends on the value you are casting. 
> Const_cast validness may depend on a lot of things. Reinterpret_cast 
> validness depends on the implementation details of the particular 
> compiler and platform combination.
> 
> Cheers
> Paavo

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Argument type of function and safe types and types, arguments can handle safely ittium <ittium@gmail.com> - 2011-12-08 12:01 +0530
  Re: Argument type of function and safe types and types, arguments can handle safely Paavo Helde <myfirstname@osa.pri.ee> - 2011-12-08 02:17 -0600
    Re: Argument type of function and safe types and types, arguments can handle safely 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-08 00:31 -0800
    Re: Argument type of function and safe types and types, arguments can handle safely ittium <ittium@gmail.com> - 2011-12-08 16:57 +0530
      Re: Argument type of function and safe types and types, arguments can handle safely Paavo Helde <myfirstname@osa.pri.ee> - 2011-12-08 12:32 -0600
        Re: Argument type of function and safe types and types, arguments can handle safely Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-08 20:00 +0000
          Re: Argument type of function and safe types and types, arguments can handle safely Paavo Helde <myfirstname@osa.pri.ee> - 2011-12-08 14:30 -0600
  Re: Argument type of function and safe types and types, arguments can handle safely Goran <goran.pusic@gmail.com> - 2011-12-09 03:05 -0800

csiph-web