标签: stack

rust 如何对堆栈分配的对象有多个可变引用?

假设我们有这样的 C 代码:

typedef struct A { int i; } A;
typedef struct B { A* a; } B;
typedef struct C { A* a; } C;

int main(void)
{
  A a = { .i = 42 };
  B b = { .a = &a };
  C c = { .a = &a };
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,A 是堆栈分配的,B 和 C 指向 A 所在的堆栈分配的内存。

我需要在 Rust 中做完全相同的事情,但每次我尝试创建多个可变引用时,它都会抱怨。

为了完成如此基本的事情而不得不与语言作斗争,这有点令人沮丧。

c stack pointers rust

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

类型双关、联合和 std::stack

假设我想将不同类型的数据推送到标准库堆栈中。为了简单起见,我只采用两种类型:

  • 整数
  • foo(类)

为此,我需要以下数据结构:

//generic class
class foo {
  private:
    float x_;
  public:
    foo(float x) : x_(x){};
};
 
//simple enum
enum element_type {
  footype,
  number
};

struct stack_element {
  element_type type_;
  union {
    foo* obj;
    int num;
  } data;
};
Run Code Online (Sandbox Code Playgroud)

然后我需要在结构体中创建一个构造函数以实现类型双关:

//actually this should be if/else statement in this case but on the code problem I have more types
stack_element(element_type type, foo* obj){
  switch(type){
    case footype: 
      this->type_ = type;
      this->data.obj = obj;
    break;
    case number:
      this->type_ = type;
      // Here …
Run Code Online (Sandbox Code Playgroud)

c++ stack struct unions

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

在堆栈或堆上创建的类成员?

需要知道是否会在堆栈中或堆上创建这样的3d矩阵,如果它在堆栈上如何新建它并正确初始化默认值(memset)

class Matrix {
     protected:
         int n[9000][420]; // is stack or heap if VVV is pointer?
};

void main()
{
         Matrix* t = new Matrix(); // created on heap
}
Run Code Online (Sandbox Code Playgroud)

c++ heap stack class

-2
推荐指数
1
解决办法
5151
查看次数

为什么"char ptr [n]; free(ptr);"​​会导致程序失败?

使用c:

  char ptr[n];
  free(ptr);
Run Code Online (Sandbox Code Playgroud)

在我看来:当"char ptr [n];" 使用,分配内存,ptr指向它,free(ptr)应该工作.程序失败了,为什么?(n == 5例如)任何深入分析?

c c++ memory heap stack

-2
推荐指数
1
解决办法
170
查看次数

如何在c#或Java中进行堆栈交换

我想交换两个堆栈,这基本上意味着堆栈的内容.我怎样才能在C#或Java或C中做到这一点

java algorithm stack data-structures c#-4.0

-2
推荐指数
1
解决办法
1093
查看次数

在java中从object转换为char

在我的一个问题中,我得到RuntimeException,因为有必要将对象转换为char.I尝试通过使用charValueOf()方法来获取对象的原始值但不能这样做.这是我的代码.....

while ((stack.size() > 0) && (stack.peek() != '('))
{
    if (ComparePrecedence(stack.peek(), infix[i]))
    {
    }
}
boolean ComparePrecedence(char top, char p_2)
{
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?谢谢..

java stack

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

如何克隆一堆整数?

这个看似极其简单的问题困扰了我好几个小时.我正在尝试克隆一堆整数对象.我试过了

Stack<Integer> newStack = (Stack<Integer>)oldStack.clone();
Run Code Online (Sandbox Code Playgroud)

然而,这给我一个错误说 clone() has protected access in java.lang.Object

我最好的猜测是因为Integer没有实现clone(),所以我收到了这个错误.那么我应该如何克隆一堆整数呢?

java stack integer clone

-2
推荐指数
1
解决办法
4025
查看次数

如果在堆栈或堆上完成分配,那对free()和delete []有用吗?

Free()知道要释放多少字节的内存但可以删除[]做同样的事情?如果我们从堆栈而不是堆分配,它们是否可以使用free()和delete []完美地工作?最后一个问题:我们需要在结尾分配NULL吗?

#include <stdio.h>
#include <stdlib.h>

char * malloc2()
{
    char * m = (char *)malloc(100);
    //so malloc(10000000) cannot cause stack-overflow?
    //cast from void * to char *
    return m;
}

char * malloc3()
{
    static char m[100];
    //can [1000000] cause stack overflow?
    return m;
}

char * newX()
{
    char * output = new char[100];
    return output;
}

int main(){

    char * p = malloc2();
    //sizeof(p) gives 8 because this is on 64 bit OS/CPU
    free(p);
    //free() knows the …
Run Code Online (Sandbox Code Playgroud)

c c++ heap stack

-2
推荐指数
1
解决办法
203
查看次数

检查给定的字符串是否是使用堆栈的回文

伙计们,

我最近接受了采访,并在Palindrome上提出了一个问题.

给定一个字符串(可能代表一个日期),使用Stack检查它是否是回文.

我试图提出解决方案,但他并不喜欢这样.

任何人都可以在Java中向我展示它的代码片段吗?

谢谢

PS:这不是作业,实际的面试问题.

java string stack palindrome

-2
推荐指数
2
解决办法
3万
查看次数

将中缀转换为后置修复表达式,关联性总是从左到右?

如果是,那为什么会这样呢?不正确的关联性对后缀表达有效吗?

c stack postfix-notation associativity

-2
推荐指数
1
解决办法
1692
查看次数