小编mar*_*ary的帖子

编译程序中的多个C文件

我有以下两个文件:

在file1.c

int main(){
  foo();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

file2.c中

void foo(){

 }
Run Code Online (Sandbox Code Playgroud)

我可以将两个文件编译并链接在一起,以便file1.c识别该foo功能而不添加extern吗?

更新了原型.

gcc file1.c file2.c throws:warning:函数foo的隐式声明.

c linker compilation

14
推荐指数
3
解决办法
6万
查看次数

C中局部变量的返回地址

说我有以下两个功能:

1

int * foo()
{
  int b=8;
  int * temp=&b;
  return temp;
}
Run Code Online (Sandbox Code Playgroud)

2

int * foo()
{
   int b=8;
   return &b;
}
Run Code Online (Sandbox Code Playgroud)

我没有得到任何警告第一个(例如函数返回局部变量的地址)但我知道这是非法的,因为b从堆栈中消失,我们留下指向未定义内存的指针.

那么我什么时候需要注意返回临时值的地址?

c return return-value

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

释放二叉树C的内存

我想从我分配的二叉树中释放内存,这样做最好的遍历是什么?

typedef struct Node{
struct Node * right;
struct Node * left;
void * data;
}Node;


typedef int (*cmp) (void*,void *);


Node* init(void * element){
    Node * newNode=(Node*)malloc(sizeof(Node));
    newNode->data=element;
    newNode->left=NULL;
    newNode->right=NULL;
    return newNode; 
}

void  insert(void * element, Node** root,cmp compareTo){
    if(*root==NULL){
        *root=init(element);
        return;
    }
    if(compareTo(element,(*root)->data)==1)
        insert(element,&((*root)->left),compareTo);
    else
        insert(element,&((*root)->right),compareTo);
}
Run Code Online (Sandbox Code Playgroud)

c free binary-tree

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

java中的通用迭代器实现

我有以下设计:我有一个抽象类Instance,我有一个Library扩展的类,Instance我有一个File也扩展了实例的类

我已经创建了自己的链表实现,它的定义如下:

public class List<T extends Instance> implements Iterable {
    //some other code here

     public Iterator iterator(){
         return new ListIterator(this);

}
Run Code Online (Sandbox Code Playgroud)

现在我创建了一个类

public class ListIterator<T extends Instance> implements Iterator<T> {
    private List thisList;
    private Node current;

    public ListIterator(List l){
        thisList=l;
        current=thisList.head.next;
    }
    @Override
    public boolean hasNext() {
        if(current==null)
            return false;
        return false;
    }

    @Override
    public T next() {
        Node temp=current;
        current=current.next;
        return temp.data;
    }
}
Run Code Online (Sandbox Code Playgroud)

哪里Node

public class Node<T extends …
Run Code Online (Sandbox Code Playgroud)

java generics iterator

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

线程和同步方法

我有以下代码:

public class MyThread extends Thread {
    private int i;
    public static int sum=0;
    public MyThread(int k){
      i=k;
    }




    public static void main(String[] args) throws InterruptedException{

       Thread t=new MyThread(1);
       Thread s=new MyThread(2);
       Thread p=new MyThread(3);
       t.start();
       s.start();       
    }


public synchronized void doSomething(){
    for(int i=0; i<100000; i++){
        System.out.println(this.i);
    }

}

    @Override
    public void run() {
        doSomething();

    }
}
Run Code Online (Sandbox Code Playgroud)

doSomething是同步的.为什么输出是随机的?我假设同步方法与同步块相同但块的输出是同步的,而方法则不是.

java multithreading synchronization

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

基本的多线程

我有以下面试问题:

class someClass
{
    int sum=0;
    public void foo()
    {
        for(int i=0; i<100; i++)
        {
            sum++
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有两个并行线程在foo方法中运行.最后总和的价值从100到200不等.问题是为什么.据我所知,只有一个线程获得一个CPU,并且线程在运行时被抢占.在什么时候干扰会导致总和达不到200?

multithreading preemption

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

在java中通过引用传递 - 编辑另一个函数中的值

为什么以下代码输出等于0?

public class A{


    public static void main(String [] args){

        int a=0;
        foo(a);
        System.out.println(a);
    }

    public static void foo(int a){
        a=78;
    }


}
Run Code Online (Sandbox Code Playgroud)

java pass-by-reference

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