小编yot*_*moo的帖子

使用exit(1),c ++时释放内存

我正在完成一项学校作业,我们被告知每当我们输入错误时,我们应该打印一条消息并退出程序.显然我使用exit(1),但问题是我在使用这个函数时有内存泄漏.我不明白为什么 - 我使用的每个变量都在堆栈而不是堆上.

我该怎么做才能防止那些内存泄漏?谢谢!

c++ memory-leaks exit

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

错误:在c中重新定义typedef

我们在学校有一个作业,我们有一个头文件,我们需要实现它.标题定义:

typedef struct Board* BoardP;
Run Code Online (Sandbox Code Playgroud)

据我所知,BoardP是指向struct Board的指针.无论如何,我的实现是:

typedef struct Board
{
    int width;
    int height;
    char *board;
} *BoardP;
Run Code Online (Sandbox Code Playgroud)

但我一直得到:

Board.c:21: error: redefinition of typedef ‘BoardP’
Board.h:4: note: previous declaration of ‘BoardP’ was here
Run Code Online (Sandbox Code Playgroud)

关于为什么会这样的想法?谢谢!

编辑:另一个问题.正如您所看到的,我的struct包含一个字符数组.当我写一个构造函数时,我应该首先初始化(malloc(sizeof(height*width))数组然后结构吗?如何使用free()?我应该首先释放数组然后结构吗?谢谢

c struct

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

使用python检查字典中是否存在键时的AttributeError

我正在使用Python 2.7.2+,当试图查看字典是否包含给定的字符串值(名为func)时,我得到以下异常:

Traceback (most recent call last):
  File "Translator.py", line 125, in <module>
    elif type == Parser.C_ARITHMETIC : parseFunc()
  File "Translator.py", line 95, in parseFunc
    if unary.has_key(func) : 
AttributeError: 'function' object has no attribute 'has_key'
Run Code Online (Sandbox Code Playgroud)

这是我定义词典的地方:

binary = {"add":'+', "sub":'-', "and":'&', "or":'|'}
relational = {"eq":"JEQ" , "lt":"JLT", "gt":"JGT"}
unary = {"neg":'-'}
Run Code Online (Sandbox Code Playgroud)

这是引发异常的函数:

def parseFunc():
    func = parser.arg1 
    print func  
    output.write("//pop to var1" + endLine)
    pop(var1)
    if unary.has_key(func) : // LINE 95
        unary()
        return
    output.write("//pop to var2" + endLine)
    pop(var2)
    result …
Run Code Online (Sandbox Code Playgroud)

python attributeerror

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

试图在java中向下传播Object to File

我写了下一段代码:

private ArrayList<File> filter() {

    ArrayList<File> result = _filters.get(0).buildTree(_dir.listFiles());

    for (int i=1; i<_filters.size(); i++){
        File[] tempDir =  result.toArray();
        result = _filters.get(0).buildTree(tempDir);
    }

    return result;

}
Run Code Online (Sandbox Code Playgroud)

正如你可以看到我有一个FILE的ArrayList,然后我使用返回和Object []数组的result.toArray,但它之前是File,所以为什么我不能将它转发回File,因为我试图在第3个在循环线?我得到下一个错误:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.io.File;
at oop.ex1.filescript.Command.filter(Command.java:50)
at oop.ex1.filescript.Command.run(Command.java:28)
Run Code Online (Sandbox Code Playgroud)

我有什么选择?

java arraylist downcast

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

C初学者 - 数组和指针

这是我一直试图运行的简单而简短的代码:

#include <stdio.h>

int const SIZE = 5;

void a(int *arr);

int main(){
    int arr[5] = {1,2,3,4,5};
    a(arr);
    return 0;
}

void a(int *arr){
    int *i;
    for (i=arr; i<&a[5]; i++)
            printf("%d",*arr[i]);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误/警告:

main.c: In function ‘main’:
main.c:15: error: variable-sized object may not be initialized
main.c:15: warning: excess elements in array initializer
main.c:15: warning: (near initialization for ‘arr’)
main.c:15: warning: excess elements in array initializer
main.c:15: warning: (near initialization for ‘arr’)
main.c:15: warning: excess elements in array initializer
main.c:15: warning: …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers

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

在c中找不到内存泄漏

我运行createNewBoard,它调用createNewMatrix,我退出程序,我有一个内存泄漏,我找不到.这是代码

BoardP createNewBoard(int width, int high)
{
    BoardP board = (BoardP) malloc(sizeof(Board));

    if (board == NULL)
    {
        reportError(MEM_OUT);
        return NULL;
    }
    board->height = high;
    board->width = width;
    board->matrix = createNewMatrix(width,high);
    printf("%c",board->matrix[1][1]);
    if (board->matrix == NULL)
    {
        reportError(MEM_OUT);
        freeBoard(board);
    return NULL;
    }
return board;
}

static char** createNewMatrix(int width, int height){
    char** newMatrix = (char**) calloc(height,sizeof(char*));
    int i;
    for (i=0; i<height; i++)
    {
        newMatrix[i] = (char*) calloc(width,sizeof(char)); //LINE 71
        if (newMatrix[i] == NULL)
        {
            int j;
            for (j=0; j<i; j++)
            { …
Run Code Online (Sandbox Code Playgroud)

c memory

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

这些数组在C指针表示法中发生了什么?

我对指针数组有点困惑,我只是想确保我是对的.

当我写int *arr它时,它只是一个指向int变量的指针,而不是一个数组.只是我初始化它(比如说malloc)它变成了一个数组.我到目前为止对吗?

另外我还有另外一个问题:给了(在学校里)一个小功能,应该返回一系列成绩,第一个单元格是平均值.这个功能是刻意错误的:他们所做的就是设定

int *grades = getAllGrades();
Run Code Online (Sandbox Code Playgroud)

而且他们已经将指针减少了一个用于普通'细胞'

*(grades - 1) = getAverage();
 return *(grades - 1)
Run Code Online (Sandbox Code Playgroud)

我知道这是错误的,因为返回的值不是数组,我只是不知道如何解释它.当我设置指针时,机器/编译器如何知道我是否只需要指针或数组?

(如果我不清楚它,因为我试图询问一些对我来说仍然含糊不清的事情,我道歉)

c pointers

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

未能使用向量迭代器,c ++

我正在尝试迭代一个持有指向Student类型对象的指针的向量.向量声明如下:static vector<Student*> students;

无论如何,我试图在函数pickWinners()中使用迭代器:

vector<Student*>::iterator p1 = students.begin();
vector<Student*>::iterator p2 = p1;
p2++;
Run Code Online (Sandbox Code Playgroud)

据我所知,p1是指向Student的指针.但是当我尝试这个时(例如):

*p1->print();
Run Code Online (Sandbox Code Playgroud)

我收到下一个错误:

Hire.cpp:192:错误:'*p1 .__ gnu_cxx :: __ normal_iterator <_Iterator,_Container> :: operator-> with _Iterator = Student**,_Container = std :: vector>'中成员'print'的请求,其中是非类型'学生*'使:* [Hire.o]错误1

这对我没有任何意义.我知道问题不在print()中.我试过了

Student *student = students.at(0);
student->print();
Run Code Online (Sandbox Code Playgroud)

一切都很完美.我在这里很无能为力,有什么想法吗?谢谢!

c++ iterator vector

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

在读取文件python时丢弃'\n'符号

我正在解析一个大文本文件,如果我只能以某种方式读取没有'\n'符号的行,我的生活会轻松得多.例如:

Hello
World
Run Code Online (Sandbox Code Playgroud)

由python的返回readLines(){'Hello\n','World\n'},而我需要得到{'Hello','World'}.有没有办法只读取印刷的字符?

我知道可以使用正则表达式完成,但我希望有一种更简单的方法可以做到这一点.

谢谢!

python parsing

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

从DataInputStream读取而不知道输出类型

假设我DataInputStream用来接收数据,但我不知道是否应该使用(例如)readUTF,readInt或者readLong.有没有一种方法可以告诉我在另一边写了什么类型的数据?

java stream

0
推荐指数
1
解决办法
115
查看次数

javascript初学者

我只是想写一个打印用户输入的简单代码.这就是我所拥有的:

<script type="text/javascript">
    function displayText() {
        var input = document.getElementById('input').value;
        document.getElementById('p').innerHTML = input;
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

<font face="arial" size="5" color="blue">Input section</font> <br/>
Query Sequence
<form name="form">
<textarea id="input" rows="8" cols="60" id="input" ></textarea><br/>
<button type="button" style="height: 25px; width: 100px" onClick="displayText()">Display Date</button><br/>
<p id="p"></p>
</form>
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它不起作用......还有一个小问题:在html中为表单标签分配ID和名称有什么区别?谢谢!

html javascript

0
推荐指数
1
解决办法
368
查看次数

变量在内存中的位置,C

我在看过去几年的C考试,我遇到了一个我不太了解的问题.他们提供了一段简单的代码,并询问了内存中不同变量的位置.选项是heap stackunknows.我知道在堆栈中初始化的自动变量,以及动态分配的变量都在堆中.

那么,第一个问题 - 什么是未知的?

对于第二个问题,这里的代码和表格的答案正确:

#include <string.h>
#include <stdlib.h>

typedef struct
{
    char *_str;
    int _l;
} MyString;

MyString* NewMyString (char *str)
{
    MyString *t = (MyString *) malloc (sizeof (MyString)); //LINE 12
    t->_l = strlen (str);
    t->_str = (char*) malloc (t->_l + 1);
    strcpy (t->_str, str);
    return t;
}

int main()
{
    MyString ** arr = (MyString**) malloc (sizeof (MyString*)*10); //LINE 21
    int i;
    for (i=0;i<10;i++)
    {
        arr[i] = NewMyString ("item"); //LINE 25
    } …
Run Code Online (Sandbox Code Playgroud)

c memory-management

0
推荐指数
1
解决办法
239
查看次数

删除单个对象时的内存泄漏,cpp

我不知道为什么我在这里有内存泄漏,非常感谢任何建议.请注意,在进程终止之前,我调用destroy(),这是一个应该删除单例对象的静态成员函数.

这是相关的代码和valgrind的messaeg:

Manager.h:
class Manager {
public:
    // Constructor/destructor
    static Manager * instance();
    static void destroy();
    ~Manager();
        // Bunch of functions that I didn't write here

private:
    Manager();
    static Manager * _singleton; 
        // Bunch of fields that I didn't write here
};

Manager.cpp:
#include "Manager.h"

Manager * Manager::_singleton = NULL;

Manager * Manager::instance() {
    if (_singleton == NULL) {
            _singleton = new Manager();
        }
    return _singleton;
}

void Manager::destroy()
{
    delete _singleton;
    _singleton = NULL;
}

/*
 * Destructor
 */ …
Run Code Online (Sandbox Code Playgroud)

c++ valgrind memory-leaks

0
推荐指数
1
解决办法
952
查看次数