小编War*_*ers的帖子

malloc(sizeof(int))vs malloc(sizeof(int*))vs(int*)malloc(sizeof(int))

我承认所有这三个都有不同的含义.但是,我不明白每种情况适用于哪些特定情况.任何人都可以分享这些例子吗?谢谢.

       malloc(sizeof(int))
       malloc(sizeof(int *))
(int *)malloc(sizeof(int))
Run Code Online (Sandbox Code Playgroud)

c memory malloc allocation

21
推荐指数
2
解决办法
6万
查看次数

make_unique 无法访问静态成员中的私有构造函数

我的班级有以下结构:

class S {
  public:
    S() {}
};

class T {
  private:
    std::unique_ptr<S> a;
    T(S);

  public:
    static std::unique_ptr<T> make_item() {
        std::unique_ptr<S> s_instance = std::make_unique<S>();
        return std::make_unique<T>(std::move(s_instance));
    }
};
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在 make_item 中创建 unique_ptr 时,它将构造函数视为私有的。

有没有办法允许在类本身的静态成员函数中使用私有构造函数?因为一个成员是 S(一个相当重的对象)的 unique_ptr,所以我们不希望使用副本。

c++ unique-ptr

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

速度找不到方法

我正在开发一个 web 应用程序,使用 Apache Velocity 作为模板引擎。我希望它显示一个 HTML5 选择,如下所示。

<select class="form-control" id="detailFunction">
    #foreach($function in $functions)
    <option id="$function.getId()">$function.getTitle()</option>
    #end
</select>
Run Code Online (Sandbox Code Playgroud)

我的Function课是这样的:

package com.stackoverflow;
class Function {
    private final int id;
    private final String title;

    Function(int id, String title) {
        this.id = id;
        this.title = title;
    }

    public int getId() {
        return this.id;
    }

    public String getTitle() {
        return this.title;
    }
}
Run Code Online (Sandbox Code Playgroud)

$functions是一个List<Function>。但是,当我运行此代码时,它说:

Object 'com.stackoverflow.Function' does not contain method getId() at /velocity/editor.vm[line 40, column 48]
Run Code Online (Sandbox Code Playgroud)

虽然它显然在那里。即使更改$functions为 …

java velocity

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

数组的自由第一个元素

当我使用分配数组时malloc,有没有办法只释放数组的第一个元素?

一个小例子:

#include <stdlib.h>
#include <string.h>

int main() {
    char * a = malloc(sizeof(char) * 8);
    strcpy(a, "foo bar");

    // How I would have to do this.
    char * b = malloc(sizeof(char) * 7);
    strcpy(b, a+1);


    free(a);
    free(b);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法释放第一个char a,以便我可以使用其余的字符串a+1

c arrays malloc free

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

python字符串到数组

我正在为学校创建一个简单的加密项目.我正在使用Python.

目标:用户输入字符串,例如hello world! 我必须将其转换为数组:space=0, a=1, b=2, ... z=26, .=27, ,=28, ?=29 and !=30

我用字典:

dict = {' ': 0, 'a': 1, 'b': 2, 'c': 3,...}
Run Code Online (Sandbox Code Playgroud)

我的代码

def messageToCode(message):
    xarray = [None]
    length = len(message)
    ctr = 0
    while not ctr == length:
        xarray.append = dict[message.charAt(ctr)]
        ctr = ctr + 1
    return xarray
Run Code Online (Sandbox Code Playgroud)

这不行.有什么建议?

python arrays string python-3.x

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

标签 统计

arrays ×2

c ×2

malloc ×2

allocation ×1

c++ ×1

free ×1

java ×1

memory ×1

python ×1

python-3.x ×1

string ×1

unique-ptr ×1

velocity ×1