小编tyr*_*na4的帖子

Vim搜索:避免评论中的匹配

使用vim搜索功能,是否可以避免在注释块/行上进行匹配.

作为一个例子,我想在这个python代码中匹配'image',但只在代码中匹配,而不是在注释中:

# Export the image
if export_images and i + j % 1000 == 0:
export_image(img_mask, "images/image{}.png".format(image_id))
image_id += 1
Run Code Online (Sandbox Code Playgroud)

使用正则表达式我会做这样的事情:/^[^#].*(image).*/gm 但它似乎不适用于vim.

python regex vim

7
推荐指数
2
解决办法
499
查看次数

如何正确检查传递给malloc()calloc()realloc()的大小及其返回值?

如果我已经正确理解,在与sizeof(desired_type)相乘后检查传递给malloc(),calloc()或realloc()的所需空间是否也不超过SIZE_MAX常量也很重要.为了确保没有发生,我开始在我的代码中做这样的事情:

#define MAX_CHAIN (50)


#ifndef SIZE_MAX
#define SIZE_MAX (~(size_t)0)
#endif

int main(int argc, char const *argv[]){

    char **parsed_string;

    if(MAX_CHAIN > SIZE_MAX/sizeof(char*) || 
    (parsed_string=(char**)malloc(MAX_CHAIN * sizeof(char*)))==NULL){
        printf("%s\n", "Error, cannot allocate memory");
        exit(1);
    }

    /*Otherwise pointer is valid and the execution continues*/


}
Run Code Online (Sandbox Code Playgroud)

所以它基本上检查了两件事:

  1. 请求的内存不大于SIZE_MAX
  2. malloc()返回的指针是有效的.

我的问题是:做这些检查是否正确?这被认为是好的做法还是我应该使用别的东西呢?此外,有人可以解释为什么分配受到SIZE_MAX的限制吗?

c size pointers allocation return-value

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

通过C中的非常量指针更改常量内存内容

假设我在C中声明了这个变量:

const char*** const strings; 
Run Code Online (Sandbox Code Playgroud)

现在如果我试试这个:

printf("character is : %c \n",***strings);

**strings="hello";

printf("strings is %s \n", **strings);

printf("character is : %c \n",***strings);
Run Code Online (Sandbox Code Playgroud)

有时,我得到以下输出:

character is :
strings is hello
character is : h
Run Code Online (Sandbox Code Playgroud)

但是有时我会得到一个段错误,因为(我假设)该指令

**strings="hello";
Run Code Online (Sandbox Code Playgroud)

我认为你可以用char* str = "hello";C语言编写,使str指向一个不可修改的字符串"hello".为什么这不适用于我的情况?任何人都可以解释为什么它不会给出相同的输出evertime?

c pointers const segmentation-fault

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

标签 统计

c ×2

pointers ×2

allocation ×1

const ×1

python ×1

regex ×1

return-value ×1

segmentation-fault ×1

size ×1

vim ×1