我正在测试valgrind并有一个泄漏 4 个字节的小 C 程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int* x = malloc(sizeof(int));
printf( "Address: %p\n", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用: 编译它gcc -g -o leak leak.c,然后运行它:
$ leak
Address: 0x55a72e303260
$ leak
Address: 0x55f370273260
Run Code Online (Sandbox Code Playgroud)
因此它显示了两次单独运行的两个不同地址。但是,如果我在 valgrind 下运行它,它总是显示相同的地址0x4a66040::
$ valgrind --leak-check=full --show-leak-kinds=all leak
==8186== Memcheck, a memory error detector
==8186== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8186== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info …Run Code Online (Sandbox Code Playgroud) 我想将宏定义为字符串,稍后在编译时包含基于字符串比较的代码:
#include <iostream>
#include <string_view>
constexpr bool strings_equal(char const * a, char const * b) {
return std::string_view(a)==b;
}
#define FOO "bar"
int main() {
#if strings_equal( FOO, "bar") == 0
std::cout << "got a bar!" << '\n';
#endif
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译这个
$ g++ -std=c++17 test.cpp -o my_test
Run Code Online (Sandbox Code Playgroud)
给出错误:
test.cpp:12:18: error: missing binary operator before token "("
12 | #if strings_equal( FOO, "bar") == 0
| ^
Run Code Online (Sandbox Code Playgroud)
编辑:
看来#if指令是否在函数内部很重要,因为如果它在函数内部,我们可以用它替换它if constexpr (...) { ... }但是如果它在 …
我正在尝试检查数组元素是否undef来自这样的XSUB:
void
print_array(array)
AV *array
PREINIT:
int len;
SV **sv_ptr;
SV *sv;
int i;
CODE:
len = av_len(array) + 1;
printf("[");
for (i = 0; i < len; i++) {
sv_ptr = av_fetch( array, i, 0 );
if (!sv_ptr) {
printf("empty");
}
else {
sv = *sv_ptr;
if (sv == &PL_sv_undef) {
printf("undef");
}
else {
printf("*");
}
}
if (i < (len - 1)) {
printf(", ");
}
}
printf("]\n");
Run Code Online (Sandbox Code Playgroud)
如果我从 Perl 脚本运行这个子程序:
use strict; …Run Code Online (Sandbox Code Playgroud) 以下Dockerfile适用于 Ubuntu:
FROM ubuntu:20.04
SHELL ["/bin/bash", "-c"]
ARG user=hakond
ARG home=/home/$user
RUN useradd --create-home -s /bin/bash $user \
&& echo $user:ubuntu | chpasswd \
&& adduser $user sudo
WORKDIR $home
USER $user
COPY --chown=$user entrypoint.sh .
RUN chmod +x entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
Run Code Online (Sandbox Code Playgroud)
这里entrypoint.sh是
#! /bin/bash
exec bash
Run Code Online (Sandbox Code Playgroud)
我如何在阿尔卑斯山做同样的事情?我试过:
FROM alpine:3.12
SHELL ["/bin/sh", "-c"]
RUN apk add --no-cache bash
ARG user=hakond
ARG home=/home/$user
RUN addgroup -S docker
RUN adduser \
--disabled-password \
--gecos "" \
--home $home …Run Code Online (Sandbox Code Playgroud) 我有这个 Bash 程序:
dict_fn='/usr/share/dict/american-english'
benchmark_repetitions=100
for i in `seq 1 $benchmark_repetitions` ; do
readarray -t syllables < 'syllables.txt'
for syllable in "${syllables[@]}" ; do
matches=$(grep "$syllable" $dict_fn | wc -l)
#echo "${syllable}: $matches"
done
done
Run Code Online (Sandbox Code Playgroud)
在我的笔记本电脑上运行大约 4 秒
$ time t.sh
real 0m3,488s
user 0m4,103s
sys 0m0,910s
Run Code Online (Sandbox Code Playgroud)
使用此输入文件syllables.txt:
art
bur
cic
dun
eik
fum
gaw
hiw
irc
jaw
kry
lus
mac
noq
old
pew
qla
rub
sur
tif
uks
Run Code Online (Sandbox Code Playgroud)
然后我尝试在 Perl 中做同样的事情:
use feature qw(say);
use strict;
use …Run Code Online (Sandbox Code Playgroud) 我正在用 perl 读取 json 文件,但在使用结果时遇到问题。
我想知道 json 文件中有多少视图。应该有 2
my $data = decode_json($json);
print Dumper($data);
my @tmp=$data->{'views'};
my $nviews=scalar @tmp;
print "nviews : $nviews\n";
Run Code Online (Sandbox Code Playgroud)
给我
$VAR1 = {
'views' => [
{
'key' => 0,
'value' => {
'ptr_wrapper' => {
'data' => {
'width' => 776,
'height' => 1024,
'id_view' => 0,
'filename' => '000118800_15821618907.jpg',
'id_pose' => 0,
'id_intrinsic' => 13,
'local_path' => '/'
},
'id' => 2147483649
}
}
},
{
'key' => 1,
'value' => {
'ptr_wrapper' …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的文本文件
{
"TYPE": "EMAIL",
"ITEMS": [
{
"SENT": "2016-02-01T19:03:02.00Z",
"SUBJECT": "UPCOMING EVENTS: ORIENTATION 2016",
"TIMEZONE": "AUSTRALIA/MELBOURNE",
"CONTENT": "WE'RE PLEASED TO BE WORKING WITH RMIT LINK'S ORIENTATION TEAM AND RUSU TO WELCOME ALL NEW STUDENTS TO CAMPUS THROUGH A SERIES OF EXCITING ORIENTATION EVENTS. THIS EMAIL SERVES AS A NOTIFICATION TO MAKE SURE YOU KNOW WHEN THE MAJOR EVENTS ARE OCCURRING, TO ENSURE THEY DON'T INTERRUPT YOUR WORK AND SO THAT YOU ARE ABLE TO ENCOURAGE ALL NEW STUDENTS TO ATTEND. …Run Code Online (Sandbox Code Playgroud) 我想使用Perl提取字符串中的所有正整数和负整数.该字符串包含以下字符和空格 - ,{}
例如:
$a = "{ { 0,256,0,0 },{ -2,256,2,0 },{ -4,256,4,0 },{ -6,255,7,0 }
,{ -7,254,10,-1 },{ -8,252,13,-1 },{ -10,251,16,-1 }
,{ -10,249,19,-2 },{ -12,247,23,-2 },{ -14,245,27,-2 }
,{ -14,242,31,-3 },{ -15,239,35,-3 },{ -15,236,39,-4 }
,{ -16,233,44,-5 },{ -17,230,48,-5 },{ -17,226,53,-6 }
,{ -18,222,58,-6 },{ -18,218,63,-7 }";
Run Code Online (Sandbox Code Playgroud)
我的o/p应该是@b包含0,256,0,0,-2,256,2,0 ......等数组的数组
我尝试了以下命令,但space/null元素也添加到我的o/p数组中.
my @b = split (/[,{}\s]/,$a);
Run Code Online (Sandbox Code Playgroud) 在我的代码中,我有一个多维数组
$rows[$x][$y]
Run Code Online (Sandbox Code Playgroud)
我将它传递给具有多种用途的子函数,但在某些时候,该函数将需要从主数组中删除(弹出)其中一个元素.
我相信传递它的正确方法是引用它,因为我传递的不仅仅是数组:
filterout(\@rows, $y, $data );
Run Code Online (Sandbox Code Playgroud)
但我不确定在子程序端解除引用它的语法.
非常感谢任何帮助,谢谢.
我试图在子例程之外访问子例程的私有变量。这该怎么做?
我试过我已发布该问题的代码,但它正在打印全局变量“名称”的值,并且条件是全局变量的名称和私有变量的名称必须相同。
print("Please Enter Your First Name:\n");
# declaration of global variable
$name = <>;
YourFirstName_StudentID($name);
sub YourFirstName_StudentID {
print("My name is $name\n");
print("Enter Your Student ID\n");
my $name = <>;
}
#printing outside subroutine
print("Student Id is: $name");
Run Code Online (Sandbox Code Playgroud)
当前输出是:请输入您的名字:我的名字是xyz
输入您的学生证学生证是:xyz
但是我想要这样请输入您的名字:我的名字是xyz
输入您的学生证学生证是:1234567