在 execve 函数中,参数通过指针数组传递。如果这些指针指向前一个堆栈中的内存,这些内存是否仍然可以在新的过程映像中访问。
#include <stdio.h>
#include <unistd.h>
int main(void)
{
char filename[20] = "a.out";
char str[20] = "hello\n";
char *argv[3];
argv[0] = filename;
argv[1] = str;
argv[2] = NULL;
execve("/hel/a.out", argv, NULL);
return 0;
}
/* /hel/a.out code */
#include <stdio.h>
int main(int argc, char *argv[], char *envp[])
{
printf("%s\n", argv[1]); /** Here, should the memory pointed by argv[1]
* be freed after execve has been called?
*/
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我只是想测试一下全局变量optind,做如下测试。如何判断optind的值?
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char optStr[] = "ab";
int c;
while ((c = getopt(argc, argv, optStr)) != -1) {
printf("optind: %d\n", optind);
switch (c) {
case 'a':
printf("-a\n");
break;
case 'b':
printf("-b\n");
break;
case '?':
printf("error\n");
break;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
./a.out -ab
optind: 1
-a
optind: 2
-b
下一个:
./a.out -a
optind: 2
-a
我测试了接口的方法next()和remove()方法Iterator.我得到以下异常:
线程"main"java.util.ConcurrentModificationException中的异常
这是我的代码:
import java.util.*;
public class ListTest {
public static void main(String[] args) {
Collection<Integer> list = new ArrayList<Integer>();
Iterator<Integer> iterator = list.iterator();
Collections.addAll(list, 1, 2, 3, 4, 5);
if (iterator.hasNext()) {
iterator.next();
iterator.remove();
}
System.out.println(list);
}
}
Run Code Online (Sandbox Code Playgroud)