标签: pass-by-value

如何在函数内正确分配结构体?

我有这段代码,这是我缩小问题范围的地方。当我使用调试器单步执行时,似乎没问题,但一旦它离开函数, freeCount 就会丢失分配给它的值。它应该是 100,直到最后一个 } 它显示它分配的值很好,但一旦函数中断,它就会返回到某个 16 位值。谁能指出我的错误?非常感激!

   void initializeTree(TREE* empList)
   {
        empList = (TREE *)malloc(sizeof(TREE));
        int i;
        for (i = 1; i <= maxEmp; i++)
        {
            time_t seed = time(null);
            empList->freeList[i] = (int)(rand() % seed);
        }
        empList->freeCount = maxEmp;
        empList->parent = null;
        empList->root = null;
    }
Run Code Online (Sandbox Code Playgroud)

c malloc function pass-by-value

1
推荐指数
1
解决办法
693
查看次数

将字符串与指向字符串的指针作为函数参数传递时,时间复杂度有什么区别?

假设我有一个字符串 s。

 s := "helloworld"
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是,如果 s 有 'n' 个字节,那么如果我将 s 传递给函数与将 &s 传递给函数然后访问字符串的第 i 个字节,则相对于 'n' 的时间复杂度是多少。

如果将 &s 传递给函数并访问字符串的第 i 个字节需要 O(1) 时间,那么当我将 s 传递给函数然后访问字符串的第 i 个字节时是否需要 O(n) 时间(因为整个字符串将被复制)?

我试过这个,发现复制一个字符串确实改变了指向它的指针。希望能更清楚地了解这一点。

func main() {
    str := "helloworld"
    fmt.Println("string pointer 1:", &str)
    printStringPointer(str)
}

func printStringPointer(s string) {
    fmt.Println("string pointer 2:", &s)
}
Run Code Online (Sandbox Code Playgroud)

输出:

string pointer 1: 0xc000010200
string pointer 2: 0xc000010210
Run Code Online (Sandbox Code Playgroud)

string pointers pass-by-value go

1
推荐指数
1
解决办法
82
查看次数

为什么可以使用另一种方法而不是赋值运算符来更改函数中 Ruby 中局部变量的值?

我试图将 Ruby 的概念理解为传递引用值语言。使用我在站点上找到的示例...

def uppercase(value)
  value.upcase!
end

name = 'William'
uppercase(name)
puts name
Run Code Online (Sandbox Code Playgroud)

我们得到输出“WILLIAM”。因此 value 和 name 都指向同一个对象,该对象最初持有的值是“William”,然后 .upcase 将其更改为“WILLIAM”。我会将其理解为传递引用。

但是如果我.upcase改为=

def uppercase2(value)
  value = "WILLIAM"
end

name = 'William'
uppercase2(name)
puts name
Run Code Online (Sandbox Code Playgroud)

输出变成“威廉”。这是按值传递。

为什么赋值运算符会影响 Ruby 中变量的处理方式?

ruby variables pass-by-reference pass-by-value

1
推荐指数
1
解决办法
190
查看次数

使用java的list add方法传递值问题

ArrayList<ArrayList<String>> list1 = new ArrayList<ArrayList<String>>();
ArrayList<String> list2 = new ArrayList<String>();

list2.add("foo");
System.out.println(list2); //[foo]
list1.add(list2);
System.out.println(list1); //[[foo]]
list2.set(0, "bar");
System.out.println(list2); //[bar]
System.out.println(list1); //[[bar]]
Run Code Online (Sandbox Code Playgroud)

上面的代码显示了2个列表.当我添加list2(包含foo)list1它们现在都包含foo.但是当我修改list2bar,list1也会改变.我一直认为add方法只给出了一份list2list1我做什么list2也不会改变对list1.

我如何才能加入list2,list1但允许list2在将来自由修改,以免list1再次受到影响?

java arraylist pass-by-reference pass-by-value

0
推荐指数
1
解决办法
2725
查看次数

函数的const参数是否自动通过引用传递?

如果我有一个如下所示的代码:

void foofunc(const int fooarg)
{
    // something here
}
Run Code Online (Sandbox Code Playgroud)

fooarg按值或按引用传递?至于fooargconst不是会得到修改,因此,如果按引用传递是确定的.


如果我有一个看起来像下面的代码:

void foofunc(int fooarg)
{
    // something here, but fooarg is not gonna get modified
}
Run Code Online (Sandbox Code Playgroud)

fooarg通过值或通过引用传递,也将是const或不是?情况与上述相同.它不会被修改.

c++ const pass-by-reference pass-by-value

0
推荐指数
1
解决办法
129
查看次数

哪个更快?通过引用传递vs传递值C++

我认为通过引用传递应该更快然后传递值,因为计算机不是复制数据,它只是指向数据的地址.

但是,请考虑以下C++代码:

#include <iostream>
#include <cassert>
#include <cmath>

using namespace std;

// do not use pass by reference& with this function, it will become so slow
unsigned long long computePascal(const unsigned int row, const unsigned int position) {
    if (position == 1 || position == row)
    return 1L;
    unsigned long long left = computePascal(row-1, position-1);
    unsigned long long right = computePascal(row-1, position);
    unsigned long long sum = left + right;
    return sum;
}

int main() {
    int row, position;
    cout …
Run Code Online (Sandbox Code Playgroud)

c++ recursion pass-by-reference pass-by-value pass-by-const-reference

0
推荐指数
1
解决办法
1904
查看次数

Ruby修改"each"块中的哈希值.传递价值的微妙之处?

根据这个问题,红宝石是严格按价值传递的.我遇到了一个案例,其中包括修改哈希,如下所示:

h = {"one" => ["un", "ein"], "two"=> ["deux", "zwei"]}
h.each { |k,v| v << "overriden"}
Run Code Online (Sandbox Code Playgroud)

导致:

{"one"=>["un", "ein", "overriden"], "two"=>["deux", "zwei", "overriden"]}
Run Code Online (Sandbox Code Playgroud)

但是,以下行为有所不同:

h = {"one" => "un", "two"=> "deux"}
h.each { |k,v| v = "overriden"}
Run Code Online (Sandbox Code Playgroud)

导致:

{"one"=>"un", "two"=>"deux"}
Run Code Online (Sandbox Code Playgroud)

我怎么能预测到这个?

ruby pass-by-value

0
推荐指数
1
解决办法
98
查看次数

C ++内联函数,参数按值传递

很好,我上次检查内联函数是一个函数,该函数的主体在调用该函数的程序的每个点中都直接替换。所以当我这样做时:

#include <iostream>
inline void increment(int n) { n = n + 1; }`
int main() { 
   int n = 0;
   increment(n);
   std::cout << "Result " << n;
}
Run Code Online (Sandbox Code Playgroud)

我应该有:结果1。相反,我得到0。

那么内联函数如何工作?

c++ inline pass-by-value

0
推荐指数
2
解决办法
1379
查看次数

为什么这些代码块的行为有所不同?

我是Perl的新手,我无法弄清楚这一点.我有两组看似相同的代码,但是一个子程序更新了值而另一个没有.在第一组代码中,我的理解是传递对数组的引用,然后更新该引用指向的值.然后在离开子例程时,值已更改.但是,在第二个中,我希望发生同样的事情.它会更新数组,但在离开子程序后会忘记它.有人可以用第二套代码向我解释幕后发生的事情吗?

第一套代码:

#!/usr/bin/perl -w

use strict;

{
    my @array = (1, 2, 3);
    removeSecondElement(\@array);
    print @array;  #output: 13
    print("\n");
}

sub removeSecondElement{
    my ($arrayReference) = @_;
    splice(@$arrayReference, 1, 1);
    print @$arrayReference;  #output: 13
    print "\n";
}
Run Code Online (Sandbox Code Playgroud)

第二代码集:

#!/usr/bin/perl -w

use strict;

{
    my @array = (1, 2, 3);
    removeSecondElement(\@array);
    print @array;  #output: 123
    print("\n");
}

sub removeSecondElement{
    my ($arrayReference) = @_;
    my @array = @$arrayReference;
    splice(@array, 1, 1);
    print @array;  #output: 13
    print "\n";
}
Run Code Online (Sandbox Code Playgroud)

perl pass-by-reference pass-by-value subroutine pass-by-pointer

0
推荐指数
1
解决办法
56
查看次数

传递字符串'按值'更改本地值反映原始值

为什么我的局部变量值的变化会反映到原始变量中?我传递给它的值在C++中.

#include <string>
#include <iostream>

void test(std::string a)
{
    char *buff  =  (char *)a.c_str();

    buff[2] = 'x';
    std::cout << "In function: "  << a;

}
int main()
{
    std::string s = "Hello World";
    std::cout  << "Before : "<< s << "\n" ;
    test(s);
    std::cout << "\n" << "After : " << s << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

Before : Hello World
In function: Hexlo World
After : Hexlo World
Run Code Online (Sandbox Code Playgroud)

c++ string pass-by-value

0
推荐指数
1
解决办法
74
查看次数