简体   繁体   English

指针和复发 - 我正在努力节省记忆

[英]Pointers and recurency - I'm trying to save memory

I'm trying to write program that create squere for string. 我正在尝试编写为字符串创建squere的程序。 Squere has to be larger then string.length(). Squere必须大于string.length()。 If there is word 'C++' I need 2x2 array to fill it inside. 如果有'C ++'字样,我需要2x2数组来填充它。 So I have written code 所以我写了代码

 #include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int pole(int &a,const int* l);
int main(){
    string code;
    cin >> code;
    int wall=1;
    pole(wall,code.length());
    cout << wall;
    system("PAUSE");
    return 0;
}
int pole(int &a,const int* l){
    if (a*a > l) return a;
    else {
    a+=1;
    pole(a,l);
    }
}

I bet that using pointer with recunrency save a lot of memory but I can't compile it. 我敢打赌,使用具有recunrency的指针可以节省大量内存,但我无法编译它。 I'm trying to understand compilers error but is 2 hard for me ;/ 我正在努力理解编译器错误,但对我来说很难; /

Here is compiler list of errors 这是编译器错误列表

> in main() 
11 25 Error] invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int (*)(int&, const int*)' 
 6 5> [Error] in passing argument 1 of 'int pole(int&, const int*)' 
 in pole() 17 12 
>[Error] ISO C++ forbids comparison between pointer and
> integer [-fpermissive]

Here: 这里:

pole(pole, code.length());

You are passing as the second variable the result of length() , which is of type std::string::size_type , which the function pole accepts a pointer to int . 您将第二个变量作为length()的结果传递,它的类型为std::string::size_type ,函数pole接受指向int的指针。 Those two types are incompatible. 这两种类型是不兼容的。

The second problem is that one branch of your if statement inside pole does not contain a return statement, thus giving your program Undefined Behavior. 第二个问题是你的if语句在pole内的一个分支不包含return语句,因此给你的程序Undefined Behavior。

You may want to change your function pole this way: 您可能希望以这种方式更改功能pole

int pole(int &a, std::string::size_type l) {
//               ^^^^^^^^^^^^^^^^^^^^^^
//               Also, passing by reference is unnecessary here

    if (a*a > static_cast<int>(l)) return a;
//            ^^^^^^^^^^^^^^^^
//            Just to communicate that you are aware of the
//            signed-to-unsigned comparison here
    else {
    a+=1;
    return pole(a,l);
//  ^^^^^^
//  Do not forget this, or your program will have Undefined Behavior!
    }
}

Here you can see your modified program compile and run. 在这里,您可以看到您修改的程序编译和运行。

You are trying to use an unsigned integer (coming from std::string::length ) as a pointer in: 您正在尝试使用无符号整数 (来自std::string::length )作为指针:

pole(wall,code.length());

Change your pole declarations to: 将您的极点声明更改为:

int pole(int a, int l);

Saving memory on int is just nonsense there. int上保存内存只是废话。 Pointers are sometimes even more memory expensive than simple integers. 指针有时甚至比简单整数更昂贵。

You should learn to save memory with huge objects instead. 您应该学会用大型对象来节省内存。

int pole(int &a,const int* l){
    if (a*a > l) return a;
    else {
    a+=1;
    pole(a,l);
    }
}

first, you cannot initialize int* l with size_t argument. 首先,您不能使用size_t参数初始化int* l Also you do later comparison between address, not value pointed too. 你也可以在以后比较地址,而不是指向值。 Is this what you wanted? 这是你想要的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试读取 class 指向带有 for 循环的向量的指针,但它仅在我在循环内声明 object 时才有效 - I'm trying to read in class pointers to a vector with a for loop, but it only works if I declare object inside loop 当一个函数返回一个指针时,是否意味着我要负责指针的内存管理? - When a function returns a pointer, is it impling that i'm responsible for the pointers memory management? 如果我不尝试管理指向的对象的内存,是否应该使用unique_ptr? - Should I be using unique_ptr if I'm not trying to manage the memory of the object I'm pointing at? 我正在尝试在函数中为新的内存新的堆内存,但是它给出了一个错误 - I'm trying to new memory new heap memory in a function but it gives an error 我又对指针感到困惑 - I'm confused about pointers again 我正在尝试对 C++ 中的两个指针的值进行算术运算,但它有时有效,有时失败。 努力在网上寻找解释 - I'm Trying to do Arithmetic on the Value of Two Pointers in C++ but it Sometimes Works and Sometimes Fails. Struggling to Find Explanations Online 我对内存指针有些困惑 - I am a little confused about memory pointers 我可以通过指针访问受限的 memory 空间吗 - Can I access restricted memory space by pointers 从函数指针C保存内存数据 - Save memory data from function pointers c++ 我正在尝试使用Windows Imaging Component加载32位图像,将其翻转,然后再次将其保存在同一文件中 - I'm trying to load a 32 bit image with the Windows Imaging Component, flip it and then save it back again in the same file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM