小编Dav*_*lke的帖子

动态分配结构数组

我已经在stackoverflow上无数次地找到了有关其他人问题的有用答案,但这是我第一次提出自己的问题.

我有一个C函数,它动态地需要为结构数组分配空间,然后用从文件中提取的值填充每个数组元素的结构成员.成员赋值在循环的第一次传递中正常工作,但是在第二次传递时我得到了分段错误.

我已经写了这个快速程序,说明了我遇到的问题的基本要点:

#include <stdlib.h>
#include <stdio.h>

typedef struct {
        int a;
        int b;
} myStruct;

void getData(int* count, myStruct** data) {
    *count = 5;
    *data = malloc(*count * sizeof(myStruct));

    int i;
    for (i = 0; i < *count; i++) {
        data[i]->a = i;
        data[i]->b = i * 2;
        printf("%d.a: %d\n", i, data[i]->a);
        printf("%d.b: %d\n", i, data[i]->b);
    }
}

int main() {
    int count;
    myStruct* data;
    getData(&count, &data);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

0.a: 0
0.b: 0
Segmentation fault
Run Code Online (Sandbox Code Playgroud)

我不确定我的问题在哪里.似乎malloc调用只为一个struct分配足够的空间,而它应该为5分配空间.

任何帮助将非常感谢.

c arrays malloc struct segmentation-fault

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

Java中奇怪的SpinnerNumberModel行为

我正在尝试使用SpinnerNumberModel设置JSpinner,其值为0.0005,min = 0.0,max = 1.0,步长= 0.0005.但是,当我使用这些参数创建一个微调器时,我会发现非常奇怪的行为.每次单击向上箭头,而不是从0.0005开始并增加0.0005,该值似乎保持为0.

为了确保这不仅仅是格式化问题,我在每次更改事件后打印出微调器的值.当然,每次单击向上箭头,控制台都会将值显示为0,然后显示0.0005,无论旋转器被点击多少次.

下面是我用来测试它的代码,包括一个值略有不同的微调器,可以很好地用于比较.

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(200, 80);
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        final JSpinner spinner1 = new JSpinner(new SpinnerNumberModel(0.0005, 0.0, 1.0, 0.0005));
        final JSpinner spinner2 = new JSpinner(new SpinnerNumberModel(0.05, 0.0, 1.0, 0.05));
        panel.add(spinner1, BorderLayout.NORTH);
        panel.add(spinner2, BorderLayout.SOUTH);
        frame.add(panel);
        spinner1.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                System.out.println("spinner1: " + ((Number) spinner1.getValue()).doubleValue());
            }
        }); …
Run Code Online (Sandbox Code Playgroud)

java swing jspinner

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

不确定删除对象的位置

我有以下课程,存储日期和价格对:

#include <vector>
#include <utility>
#include "boost/date_time/gregorian/gregorian.hpp"

using std::vector;
using std::pair;
using boost::gregorian::date;

class A {
private:
    vector<pair<date*, float> > prices;
public:
    A(pair<date*, float> p[], int length) : prices(p, p + length) { }
};
Run Code Online (Sandbox Code Playgroud)

使用以下函数创建并填充此类的对象:

A loadData() {
    // create price array
    pair<date*, float> *prices = new pair<date*, float>[10];

    // fill array with data (normally read from a file)
    for (int i = 0; i < 10; ++i) {
        prices[i].first = new date(2012, 4, 19);
        prices[i].second = 100;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ delete-operator

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

标签 统计

arrays ×1

c ×1

c++ ×1

delete-operator ×1

java ×1

jspinner ×1

malloc ×1

segmentation-fault ×1

struct ×1

swing ×1