根据我对尾递归的理解,以下函数不是尾递归函数.
(define (map f L)
(if (null? L)
'()
(cons (f (car L)) (map f (cdr L)))))
Run Code Online (Sandbox Code Playgroud)
cons将不得不等到(map f (cdr L))返回完成其工作.这可以防止它被尾递归.
我对吗?
C++标准说明了ODR,因为它适用于内联函数(强调我的):
3.2一个定义规则
3每个程序应包含该程序中每个非内联函数或变量的一个定义; 无需诊断.该定义可以在程序中明确显示,可以在标准或用户定义的库中找到,或者(在适当的时候)隐式定义(见12.1,12.4和12.8).内联函数应在每个使用它的翻译单元中定义.
它没有说明内联函数是否可以在不同的翻译单元中具有不同的实现.我尝试了以下方法:
test-1.cc
#include <iostream>
inline std::ostream& foo(std::ostream& os)
{
return os << "Foo_1";
}
void test_1()
{
foo(std::cout) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
test-2.cc
#include <iostream>
inline std::ostream& foo(std::ostream& os)
{
return os << "Foo_2";
}
void test_2()
{
foo(std::cout) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
main.cc
extern void test_1();
extern void test_2();
int main()
{
test_1();
test_2();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期待看到以下输出:
Foo_1
Foo_2
Run Code Online (Sandbox Code Playgroud)
相反,我看到:
Foo_1
Foo_1
Run Code Online (Sandbox Code Playgroud)
我用它来测试它g++ 4.7.3.
g ++在选择其中一个内联实现时是否正确?是否不可能在不同的翻译单元中提供内联函数的不同实现?
我正在学习C中的指针.我有一个2D整数数组,我试图在一行中添加所有元素.2D数组名称是a.我知道这a是一个指向数组行的指针,它们本身就是指向整数元素的指针.我想遍历整个数组a.所以,我宣布一个指针**pp等于a,因为a它是指向指针的指针.但该程序在运行时崩溃.
int main(void){
int sum=0;
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int **pp = NULL;
int *p = NULL;
for (pp=a;pp<a+3;pp++){
for (p=*pp;p<*pp+3;p++)
sum += *p;
printf("sum = %d\n", sum);
sum = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道2D数组的第一个对象的地址很简单a.第二个对象的地址是a+1等等.i,j例如,矩阵元素将是*(*(a+i)+j).换句话说,如果我将指针变量设置pp为(a+i),我知道我在第i行.第i行的第一个整数元素的地址*(a+i),这就是我做的原因p=*pp.所以元素(i,0) 应该*p是相同的*(*(a+i)+0).那么,为什么代码不能运行呢?
如果按下外部停止键,我试图逃离主循环.目前,通过RS485通信将AT32UC与ATmega128通信,其中实现了START和STOP键.如果在接收器侧有要处理的数据,则调用RS485接收器中断,其中0x10 =开始,0x11 =停止.
我的问题是启动和停止键被很好地识别并且如果启动则继续主循环,并且如果按下停止键我想终止主循环.所以我相应地设置了开始标志和停止标志.但是,我正在努力停止(逃避)实施.下面是中断例程和主循环的简要片段.
__attribute__((__interrupt__)) static void rs485RxInterrupt(void)
{
uint32_t data;
static char RxDatalength = 98;
data = AVR32_USART2.RHR.rxchr;
if(data & 0x00000100) // rs485 9 bit check
{
if((data & 0x000000ff) == 0x92) //dsp board address = 0x92
{
rxBuf[0] = data;
addr_flag = true;
rxInCtr = 1;
}
else
{
addr_flag = false;
return;
}
}
else if (addr_flag == true) // if 9 bit is checked
{
rxBuf[rxInCtr++] = data;
if(rxInCtr == 2) // command check
{
if(data …Run Code Online (Sandbox Code Playgroud) class A
{
public:
int v;
A * p;
A& operator*(const A& a)
{
return this->v*a.v// here is a red line under this say error initial value of reference to non-const must be an value
}
~A()
{
this;
}
};
int main()
{
A a;
a.p = new A;
delete a.p;
return 0;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
重载 * 运算符我不能用它来表示对象本身。为什么会这样。
阅读以下使用reinterpret_cast的程序.
#include <iostream>
class A
{
public:
A() : m_i(0) { }
protected:
int m_i;
};
class B
{
public:
B() : m_d(0.0) { }
protected:
double m_d;
};
class C : public A , public B
{
public:
C() : m_c('a') { }
private:
char m_c;
};
int main()
{
C c;
A *pa = &c;
B *pb = &c;
bool z = (reinterpret_cast<char*>(pa) == reinterpret_cast<char*>(pb));
std::cout << z;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行此程序后,它打印0,任何人都可以解释为什么z在这个程序中出现错误?
好吧,所以我正在尝试使用makefile编译我的代码,我只有2个.c文件和1个.h文件,我使用math.h中的"sqrt()"函数(仅在main中),这是我的生成文件:
a.out: GBST.o main.o
gcc GBST.o main.o
GBST.o: GBST.c GBST.h
gcc -c GBST.c
main.o: main.c
gcc -c main.c -lm
Run Code Online (Sandbox Code Playgroud)
仍然,我得到main.c :(.text + 0x170):未定义引用`sqrt'错误,它可以是什么?(顺便说一句,我之前在GBST专栏中写了-lm并没有帮助,所以我删除了它)
我有一个简单的程序:
int main() {
asm ("movl $-2, %ecx");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我用
gcc soc.c
Run Code Online (Sandbox Code Playgroud)
它构建文件没有任何问题。但是,当我用
gcc -std=c99 soc.c
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息。
soc.c: In function ‘main’:
soc.c:2:4: warning: implicit declaration of function ‘asm’ [-Wimplicit-function-declaration]
asm ("movl $-2, %ecx");
^
/cygdrive/c/Users/rsahu/AppData/Local/Temp/ccEMHjZ4.o:soc.c:(.text+0x15): undefined reference to `asm'
/cygdrive/c/Users/rsahu/AppData/Local/Temp/ccEMHjZ4.o:soc.c:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `asm'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
为什么使用-std=c99改变关键字asm的处理方式?
我有以下函数用于使用Newton-Raphson方法计算数字的平方根.
double get_dx(double y, double x)
{
return (x*x - y)/(2*x);
}
double my_sqrt(double y, double initial)
{
double tolerance = 1.0E-6;
double x = initial;
double dx;
int iteration_count = 0;
while ( iteration_count < 100 &&
fabs(dx = get_dx(y, x)) > tolerance )
{
x -= dx;
++iteration_count;
if ( iteration_count > 90 )
{
printf("Iteration number: %d, dx: %lf\n", iteration_count, dx);
}
}
if ( iteration_count < 100 )
{
printf("Got the result to converge in %d …Run Code Online (Sandbox Code Playgroud) 整个代码的时间复杂度是否为O(log(N))?(摊销后)。如果可以,那么我们是否可以每次都使用这种方法进行排序?最初,需要对一些数字进行排序。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int numbers[]={60 , 2 , 3 , 35, 67, 111, 5, 7};
set<int> s (numbers,numbers+8);
for(auto x : s)
{
cout << x << " ";
}
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)