我有以下两个文件:
在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的隐式声明.
说我有以下两个功能:
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从堆栈中消失,我们留下指向未定义内存的指针.
那么我什么时候需要注意返回临时值的地址?
我想从我分配的二叉树中释放内存,这样做最好的遍历是什么?
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) 我有以下设计:我有一个抽象类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) 我有以下代码:
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是同步的.为什么输出是随机的?我假设同步方法与同步块相同但块的输出是同步的,而方法则不是.
我有以下面试问题:
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?
为什么以下代码输出等于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) c ×3
java ×3
binary-tree ×1
compilation ×1
free ×1
generics ×1
iterator ×1
linker ×1
preemption ×1
return ×1
return-value ×1