小编vim*_*loc的帖子

Eclipse步入调试阶段

我只是把eclipse放在我的笔记本电脑上,当我使用Step Into调试工具时,它不仅仅把我带到我的代码的下一部分.一个例子是如果我调用.size();一个数组列表,它将带我进入数组列表类并通过所需的所有代码.size();

但是在我的桌面上它只需要我下一段代码.我做:

System.out.println("hello world!");
Run Code Online (Sandbox Code Playgroud)

如果我点击"Step Into"(从我的桌面),hello world将出现在控制台上.

相比之下,如果我System.out.println("hellow world");在笔记本电脑上"Step Into" ,它首先拉出PrintStream.class,然后点击Writter.class,然后是String.class,然后是BufferedWritter.class等.

我在调试时只是使用"Step Over",但是有一些调用,一个基本的例子就是mergeSort(arr, 0, arr.size());如果我跨过它,它会跳过整个事情,但如果我进入它,它会拉起arr.size();方法,我将不得不点击所有这些,然后再回到我的东西.

在我的桌面上,我正在运行windows和eclipse版本3.4.1.在我的笔记本电脑上,我正在运行linux和eclipse版本3.5.1.

思考?建议吗?那有意义吗?

java eclipse debugging step-into

6
推荐指数
2
解决办法
9571
查看次数

在c ++中重新分配指针的正确方法

编辑:我知道在这种情况下,如果它是一个实际的类,我最好不要将字符串放在堆上.但是,这只是一个示例代码,以确保我理解该理论.实际代码将是一棵红黑树,所有节点都存储在堆上.

我想确保在继续之前我有这些基本想法是正确的(我来自Java/Python背景).我一直在网上搜索,但还没有找到这个问题的具体答案.

当您重新指定指向新对象的指针时,是否必须先在旧对象上调用delete以避免内存泄漏?我的直觉告诉我是的,但在继续之前我想要一个具体的答案.

例如,假设您有一个存储指向字符串的指针的类

class MyClass
{
private:
    std::string *str;

public:
MyClass (const std::string &_str)
{
    str=new std::string(_str);
}

void ChangeString(const std::string &_str)
{
    // I am wondering if this is correct?
    delete str;
    str = new std::string(_str)

    /*
     * or could you simply do it like:
     * str = _str;
     */ 
}
....
Run Code Online (Sandbox Code Playgroud)

在ChangeString方法中,哪个是正确的?

我想如果你不使用new关键字第二种方式我会被挂起,它仍然会像你预期的那样编译和运行.这只会覆盖此指针指向的数据吗?或者它做了别的什么?

任何建议都会受到很大的批评:D

c++ pointers memory-management

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

C++ pthread阻塞队列死锁(我认为)

我遇到了pthreads的问题,我认为我遇到了僵局.我创建了一个我认为正在工作的阻塞队列,但在做了一些测试后我发现如果我尝试取消阻塞在blocking_queue上的多个线程,我似乎陷入僵局.

阻塞队列非常简单,如下所示:

template <class T> class Blocking_Queue
{
public:
    Blocking_Queue()
    {
        pthread_mutex_init(&_lock, NULL);
        pthread_cond_init(&_cond, NULL);
    }

    ~Blocking_Queue()
    {
        pthread_mutex_destroy(&_lock);
        pthread_cond_destroy(&_cond);
    }

    void put(T t)
    {
        pthread_mutex_lock(&_lock);
        _queue.push(t);
        pthread_cond_signal(&_cond);
        pthread_mutex_unlock(&_lock);
    }

     T pull()
     {
        pthread_mutex_lock(&_lock);
        while(_queue.empty())
        {
            pthread_cond_wait(&_cond, &_lock);
        }

        T t = _queue.front();
        _queue.pop();

        pthread_mutex_unlock(&_lock);

        return t;
     }

priavte:
    std::queue<T> _queue;
    pthread_cond_t _cond;
    pthread_mutex_t _lock;
}
Run Code Online (Sandbox Code Playgroud)

为了测试,我创建了4个线程来拉动这个阻塞队列.我向阻塞队列添加了一些print语句,每个线程都进入pthread_cond_wait()方法.但是,当我尝试在每个线程上调用pthread_cancel()和pthread_join()时,程序就会挂起.

我还用一个线程对它进行了测试,它完美无缺.

根据文档,pthread_cond_wait()是一个取消点,因此在这些线程上调用cancel会导致它们停止执行(这只适用于1个线程).但是pthread_mutex_lock不是取消点.可能会在调用pthread_cancel()时发生某些事情,取消的线程在终止之前获取互斥锁并且不解锁它,然后当下一个线程被取消时它无法获取互斥锁和死锁?或者还有别的我做错了.

任何建议都很可爱.谢谢 :)

c++ deadlock pthreads blockingqueue

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

Flask在unittest中禁用CSRF

在我的项目中__init__.py我有这个:

app = Flask(__name__)
app.config.from_object('config')
CsrfProtect(app)
db = SQLAlchemy(app)
Run Code Online (Sandbox Code Playgroud)

我的开发配置文件如下所示:

import os
basedir = os.path.abspath(os.path.dirname(__file__))

DEBUG = True
WTF_CSRF_ENABLED = True
SECRET_KEY = 'supersecretkey'
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'project.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
Run Code Online (Sandbox Code Playgroud)

在我的unittest setUp中我有这个:

from project import app, db

class ExampleTest(unittest.TestCase):
   def setUp(self):
        app.config['TESTING'] = True
        app.config['WTF_CSRF_ENABLED'] = False
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
        self.app = app.test_client()
        db.create_all()
Run Code Online (Sandbox Code Playgroud)

理论上,在这里将WTF_CSRF_ENABLED设置为False应该可以防止单元测试的CSRF,但是如果我在单元测试时进行POST,我仍然会遇到CSRF错误.我认为这是因为我已经调用了CsrfProtect(app)而WTF_CSRF_ENABLED为True(当我导入app时,它被调用).如果我在配置文件中设置WTF_CSRF_ENABLED = False,它将按预期工作.

无论如何我可以在启用后禁用CSRF吗?或者我在这里咆哮错误的树?

flask flask-wtforms

6
推荐指数
2
解决办法
3994
查看次数

一般调试日志实践

由于最近的事件,我试图弄清楚我应该使用多少调试日志代码.

我一直在做的是非常谨慎地使用调试日志,并且只是在我想要一些额外信息或者你有什么的情况下.这对我来说很有意义,因为看起来你不应该记录你的代码所做的每一件小事,因为这可能会让你充斥着如此多的信息,以至于更容易错过一些真正重要的东西(或者从挖掘中发疯通过并验证日志).

另一方面,我举一个例子:我刚开始使用logback/slf4j作为我的java项目,并测试我正确设置了.xlm文件我在初始化gui的方法的末尾添加了一个调试日志语句组件.通常情况下,我从来没有在那里放置日志语句,因为当你运行程序时你的gui组件没有正确初始化是非常明显的.然而这次我运行程序,并且低并且看到日志显示gui组件被初始化两次,即使它们只显示了一组.一个体面的大小的bug,但是如果没有那些调试语句,我可能不会抓到它.

所以我的问题是:在调试日志时是否有任何"最佳实践"?在信息日志,异常,错误等方面,我已经看到了许多最佳实践问题,但在调试日志方面没有找到太多.

谢谢 :)

java debugging logging

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

是否可以将函数(指针?)保存到对象中?

我已经搜索了这个,但我想我只是让自己更加困惑.

我想要做的是在一个对象中保存一个函数指针,并在稍后的另一个线程中调用它.

我想象的是一个构造函数,它将获取一个函数指针和将传递给该函数指针的参数.该对象还将具有run()方法,该方法将运行所述函数指针和wait_until_completed()方法,该方法将阻塞直到该函数已运行.

如果有意义的话,函数指针应该是来自另一个对象的函数.例如

Foo::*Bar(int);
Run Code Online (Sandbox Code Playgroud)

我有使用pthread_cond_t的wait_until_completed()工作,但我坚持这个函数指针的事情,感觉我只是在圈子里跑来跑去.

有什么建议?

编辑:这是为学校(任何我的一般理解)所以第三方图书馆不能工作:/

我觉得我做了一个非常糟糕的工作来解释这个,让我给出一些示例代码(不包括所有同步的东西)

class Foo
{
public:
    Foo(void (Bar::*function) (int), int function_param)
    {
        // Save the function pointer into this object
        this->function = &function;

        // Save the paramater to be passed to the function pointer in this object
        param = function_param;
    }

    run()
    {
        (*function)(param);
    }

private:
    // The function pointer
    void (Bar::*function) (int) = NULL;

    // The paramater to pass the function pointer
    int param;
}
Run Code Online (Sandbox Code Playgroud)

这简直就是我要做的事情.但是,我不确定它是语法还是我是愚蠢的,但我无法弄清楚如何实际执行此操作并将其编译.

几点思考?并感谢到目前为止的所有建议:)

c++ function-pointers

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

C++删除不起作用?

我遇到了删除和析构函数的问题(我确信我在这里犯了一个愚蠢的错误,但是到目前为止还没能弄清楚).

当我逐步进入析构函数,并尝试在指针上调用delete时,消息显示"无法访问地址某个地址的内存".

相关代码是:

/*
 * Removes the front item of the linked list and returns the value stored
 * in that node.
 *
 * TODO - Throws an exception if the list is empty
 */
std::string LinkedList::RemoveFront()
{
    LinkedListNode *n = pHead->GetNext(); // the node we are removing
    std::string rtnData = n->GetData(); // the data to return

    // un-hook the node from the linked list
    pHead->SetNext(n->GetNext());
    n->GetNext()->SetPrev(pHead);

    // delete the node
    delete n;
    n=0;

    size--;
    return rtnData;
} …
Run Code Online (Sandbox Code Playgroud)

c++ destructor delete-operator

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

从Java学习C++,尝试创建链表

我刚刚开始学习C++(来自Java)并且在做任何事情时遇到了一些严重的问题:P目前,我正在尝试制作一个链表,但是必须做一些愚蠢的事情,因为我一直得到"空值不被忽略,因为它应该是"编译错误(我把它标记在下面扔它的地方).如果有人能帮我解决我做错了什么,我将非常感激:)

此外,我不习惯选择通过引用,地址或值传递,以及一般的内存管理(目前我的所有节点和数据都在堆上声明).如果有人对我有任何一般性建议,我也不会抱怨:P

LinkedListNode.cpp的密钥代码

LinkedListNode::LinkedListNode()
{
    //set next and prev to null
    pData=0; //data needs to be a pointer so we can set it to null for
             //for the tail and head.
    pNext=0;
    pPrev=0;
}

/*
 * Sets the 'next' pointer to the memory address of the inputed reference.
 */
void LinkedListNode::SetNext(LinkedListNode& _next)
{
    pNext=&_next;
}

/*
 * Sets the 'prev' pointer to the memory address of the inputed reference.
 */
void LinkedListNode::SetPrev(LinkedListNode& _prev)
{
    pPrev=&_prev; …
Run Code Online (Sandbox Code Playgroud)

c++ java pointers linked-list parameter-passing

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

尝试抛出异常时c ++模板化函数错误

我有一些代码,我试图在模板化方法中抛出自定义异常.当我尝试编译它时,我收到以下警告:

there are no arguments to ‘Invalid_State_Exception’ that depend on a 
template parameter, so a  declaration of ‘Invalid_State_Exception’ must
be available 
note: (if you use ‘-fpermissive’, G++ will accept your code, but
allowing the use of an undeclared name is deprecated)
Run Code Online (Sandbox Code Playgroud)

到目前为止,我还没有找到解决方法.任何建议都会很棒.这是一些示例代码,解释了我的内容(Foo.h):

 template <class T> class Foo
 {
     public:
         void do_stuff(T t)
         {
             if(bar == true)
             {
                 throw Invalid_State_Exception("FooBar error occurred");
             }
         }

      ....
};

class Invalid_State_Exception : public std::runtime_error
{
    public:
        Invalid_State_Exception(const std::string& msg) :
            std::runtime_error(msg) { }
};
Run Code Online (Sandbox Code Playgroud)

c++ templates exception

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

C strcat垃圾字符

我在C中有一个函数,我试图从两个不同的位置(未知的大小,可能是安静的大)获取字符串,并将它们组合成一个字符串并返回它们.如果我只打印两个字符串然后我得到正确的结果,但是当我尝试使用strcat组合字符串时,我最终得到5个垃圾字符,然后是组合字符串的结果.

有人对我做错了什么有一些建议吗?以下是一些示例代码,用于演示我正在做的事情:

static int get_information(char** results)
{
    size_t s1_length;
    size_t s2_length;

    /* DEBUGGING - Prints the correct string */
    printf(get_string_1());
    printf(get_string_2());
    printf("\n");

    /* Allocate memory for new string */
    s1_length = strlen(get_string_1());
    s2_length = strlen(get_string_2());
    *results = malloc(sizeof(char) * (dir_length + file_length));

    if(results == NULL)
        return -1;

    /* Combine the strings */
    strcat(*results, get_string_1());
    strcat(*results, get_string_2());

    /* DEBUGGING - prints 5 garbage characters then the correct string */   
    printf(*results);
    printf("\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c cstring strcat

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