小编hel*_*hel的帖子

after execve, are memory in the previous process addr freed?

在 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)

linux execve

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

如何判断getopt中optind的值

我只是想测试一下全局变量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

c linux

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

如果在反引号中为变量分配了命令表达式,会发生什么

下面是bash的代码:

a=`echo hello`
echo $a
Run Code Online (Sandbox Code Playgroud)

输出是:

你好

但我认为它应该是:

你好
0

linux bash

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

为什么以下代码使用Iterator next()和remove()抛出ConcurrentModificationException?

我测试了接口的方法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)

java iterator

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

标签 统计

linux ×3

bash ×1

c ×1

execve ×1

iterator ×1

java ×1