문제

In my code I have effectively the following:

wchar_t* buffer = new wchar_t[size];
// bonus irrelevant code here
delete[] reinterpret_cast<char*>( buffer );

Types in question are all built-in and so they have trivial destructors. In VC++ the code above works allright - new[] just allocates memory, then delete[] just frees it.

Is it acceptable in C++? Is it undefined behaviour?

도움이 되었습니까?

해결책

상자 접근 방식은 다른 열 세트가있는 페이지에 2 개의 뷰를 추가하고 웹 파트의 특성을 특정 사용자 그룹에 대상으로 설정합니다.

웹 파트를 편집 할 때;"고급"탭에서 "대상 잠재 고객"옵션이 나타납니다.

다른 팁

It is undefined behaviour because delete[] invokes the wrong destructor. However, wchar_t and char are PODs, so they have no dedicated destructor and all delete[] does is calling the heap implementation to free up the pointer. Therefore, it is most likely to work, no byte is lost. But strictly speaking it is still undefined.

이를 시도해보십시오. 오버로드 사용 :

 public void pathToSum(int sum) {
    pathToSum(root, sum);
}

private boolean pathToSum(Node n, int sum) {
    if (null != n) {
        sum -= n.data;
        boolean found = pathToSum(n.left, sum);

        if (!found) {
            found = pathtoSum(n.right, sum);
        }
        if (found) {
            println(n.data);
                            return found;
        }
    }
    return 0 == sum ? true : false;
}
.

이 코드는 다음과 같은 클래스로 테스트됩니다.

import java.util.LinkedList;
import java.util.Queue;
public class BST {
Node root;
public BST(){
    root = null;
}

public void insert(int el){

    Node tmp = root, p=null;
    while(null!=tmp && el != tmp.data){
        p=tmp;
        if(el<tmp.data)
            tmp=tmp.left;
        else
            tmp=tmp.right;
    }
    if(tmp == null){
        if(null == p)
            root = new Node(el);
        else if(el <p.data)
            p.left= new Node(el);
        else
            p.right=new Node(el);
    }
}//

public void pathToSum(int sum) {
    pathToSum(root, sum);
}//

private boolean pathToSum(Node n, int sum) {
    if (null != n) {
        sum -= n.data;
        boolean found = pathToSum(n.left, sum);

        if (!found) {
            found = pathToSum(n.right, sum);
        }
        if (found) {
            System.out.println(n.data);
            return found;
        }
    }
    return 0 == sum ? true : false;
}

public static void main(String[] args){
    int[] input={50,25,75,10,35,60,100,5,20,30,45,55,70,90,102};
    BST bst = new BST();
    for(int i:input)
        bst.insert(i);
    bst.pathToSum(155);
}
}

class Node{
public int data;
public Node left;
public Node right;
public Node(int el){
    data = el;
}
}
.

결과 :

45
35
25
50
.

iso14882 section 5.2.10.3:

The mapping performed by reinterpret_cast is is implementation defined

iso14882 section 5.3.5.2:

The value of the operand of delete[] shall be the pointer value which resulted from a previous array new-expression

In other words, it's implementation defined whether or not the delete[] invokes undefined behaviour. Steer clear.

나는 아니오를 말할 것입니다.

사용자 가이 스크립트를 사용하여 도메인에 마지막으로 로그온 할 때 정보를 찾을 수 있습니다.

# Load the SharePoint cmdlets
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
if ($snapin -eq $null) 
{    
    Write-Host "Loading SharePoint Powershell Snapin"    
    Add-PSSnapin "Microsoft.SharePoint.Powershell"  -EA SilentlyContinue
}

# Import ActiveDirectory cmdlets
Import-Module ActiveDirectory

# Here's the function that will return the last logon date and time
function Get-ADUserLastLogon([string]$userName)
{
  $dcs = Get-ADDomainController -Filter {Name -like "*"}
  $time = 0
  foreach($dc in $dcs)
  { 
    $hostname = $dc.HostName
    $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon 
    if($user.LastLogon -gt $time) 
    {
      $time = $user.LastLogon
    }
  }
  $dt = [DateTime]::FromFileTime($time)
  Write-Host $username "last logged on at:" $dt 
}

# Get the user profiles
$site=Get-SPSite "https://intranet.company.com/"
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context) 
$profiles = $profileManager.GetEnumerator()

# Iterate all profiles and grab the users last logon date time and write to comsole
foreach($user in $profiles)
{
     Get-ADUserLastLogon -UserName $user["UserName"]
}
.

다음과 같은 출력을 얻을 수 있습니다.

SP2010Dev last logged on at: 08/08/2013 09:02:16
SP2010UPS last logged on at: 08/08/2013 01:00:10
SP2010Search last logged on at: 02/08/2013 23:55:42
SP2010Setup last logged on at: 01/01/1601 01:00:00
SP2010Crawl last logged on at: 08/08/2013 00:04:51
SP2010Farm last logged on at: 08/08/2013 06:11:01
Administrator last logged on at: 10/02/2011 14:04:13
SP2010App last logged on at: 05/08/2013 18:55:43
SP2010SqlSvc last logged on at: 05/08/2013 12:10:29
testtwo last logged on at: 18/03/2013 15:38:43
test6 last logged on at: 20/06/2013 11:35:04
test4 last logged on at: 13/06/2013 13:10:39
.

Active Directory 호출에 대한 참조 : 결정사용자의 마지막 로그온 시간

The delete[] operator internally uses a loop of some form to destruct the elements of your array. If the elements are different objects a different destructor will be used -- which can potentially cause undefined behaviour. Since the is a wchar and a char -- primitive types -- it probably won't cause any undesireable behaviour.

WARNING: IF YOU CONTINUE READING DO SO AT YOUR OWN PERIL !! GROSS DESCRIPTIONS OF UNDEFINED BEHAVIOUR AHEAD. THIS IS FOR EDUCATIONAL PURPOSES ONLY.

Example 1:

If you had two objects which were the same size and all their destructor did was zero out the memory then again it would probably not cause undersireable behaviour.

Example 2:

However if you had two objects where one type encapsulated a single 4 byte handle to a resource and the other had two such elements and you casted an array of the later into the singular case -- well then you would leak half of the handles of your array. The situation would look as follows:

..2:[1|2][1|2]FREE..

where the '2:' represents the size of the array. After a downcast the compiler will generate a delete that perceived the data as so :

..2:[1][1]FREE...

therefore after the free things would look like so:

..FREE[1|2]FREE..

Why are you using reinterpret_cast<..>? If you are writing something in pure C++, then you don't need reinterpret cast. In your case you are not allocating the memory for a object. You are allocating the memory for wchar_t. Why don't use to string instead of array of wchar_t?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top