解除引用结构后,为什么结构成员未初始化

时间:2015-03-17 18:54:19

标签: c++ pointers struct

我正在编写一个HBridge类,以便更轻松地处理HBridges。我不确定为什么我在我的HBridge实例上调用.print(),它打印出来:

---------
BRIDGE ONE
Bridge Use:->pin: 1392293344
Bridge Idle->pin: 1392293408
Freq:0
Dir:0
---------
BRIDGE TWO
Bridge Use:->pin: 8
Bridge Idle->pin: 214125355
Freq:0
Dir:0

注意Bridge UseBridge Idle是如何未初始化的。我用指针在某个地方犯了错误吗?

这是我的代码。

HBridge.h

//-----------------------|
#define true 1         //|
#define false 0        //|
//-----------------------|
#define PIN_OFF 0      //|
#define PIN_ON  1      //|
//-----------------------|
#define PIN_FORWARD 0 //|
#define PIN_BACKWARD 1//|
//-----------------------|



typedef struct{
    int pin;
} Pin;

typedef struct {
    Pin *inuse;
    Pin *idle;
    int freq;
    int direction;
}Bridge;

typedef enum{ A, B} hbridge_pins;

class HBridge {
    private:
    Bridge bridge_a, bridge_b;
    Bridge *pins[2];
    public:
        HBridge(int, int, int, int);
        void setPinFrequency(hbridge_pins, int);
        void setPinDir(hbridge_pins, int);
        void turnPinOFF(hbridge_pins);
        void update(void);
        void print();
};

HBridge.cpp

#import "./HBridge.h"
#import <stdio.h>


Pin pinWith(int num) {
    // pinMode(num, OUTPUT);
    Pin p;
    p.pin = num;
    return p;
}

void swap_pins(Pin *a, Pin *b) {
        Pin temp = *a;
        *a = *b;
        *b = temp;
}

void print_pin(const char * name, Pin *p){
    printf("%s->pin: %d\n",name,p->pin);
}
void print_bridge(Bridge *b){
    print_pin("Bridge Use:", b->inuse);
    print_pin("Bridge Idle", b->idle);
    printf("Freq:%d\nDir:%d\n",b->freq,b->direction);

}

void HBridge::turnPinOFF(hbridge_pins pin) {
    pins[pin]->freq = PIN_OFF;
}

HBridge::HBridge(int pinAA, int pinAB, int pinBA, int pinBB) {

    Pin a_use = pinWith(pinAA);
    Pin a_idle = pinWith(pinAB); 
    Pin b_use = pinWith(pinBA);
    Pin b_idle = pinWith(pinBB);


    bridge_a.inuse = &a_use;
    bridge_a.idle = &a_idle;

    bridge_b.inuse = &b_use;
    bridge_b.idle = &b_idle;




    /*---(DEFAULT DIRECTIONS)---*/
    bridge_a.direction = PIN_FORWARD;
    bridge_b.direction = PIN_FORWARD;
    bridge_a.freq = PIN_OFF;
    bridge_b.freq = PIN_OFF;

    /*---(ARRAY OF POINTERS TO THE TWO PINS FOR EASY ACCESS BY INDEX)---*/
    pins[0] =&bridge_a;
    pins[1] =&bridge_b;
}

void HBridge::setPinFrequency(hbridge_pins pin, int freq) {
    pins[pin]->freq = freq;
}

void HBridge::setPinDir(hbridge_pins pin, int dir) {
    if ((pins[pin]->direction == PIN_FORWARD && dir == PIN_FORWARD) || (pins[pin]->direction == PIN_BACKWARD && dir == PIN_BACKWARD)) {

    } else if (pins[pin]->direction == PIN_FORWARD && dir == PIN_BACKWARD) {

        /*----(SWAP POINTERS)----*/
        swap_pins(pins[pin]->inuse, pins[pin]->idle);
        pins[pin]->direction = PIN_BACKWARD;

    } else if (pins[pin]->direction == PIN_BACKWARD && dir == PIN_FORWARD) {

        /*----(SWAP POINTERS)----*/
        swap_pins(pins[pin]->inuse, pins[pin]->idle);
        pins[pin]->direction = PIN_BACKWARD;

    }
}

void HBridge::update(void)/*pointer to an int to save memory*/ {
    /*THE FIRST BRIDGE*/
    // analogWrite(pins[0]->inuse->pin, pins[0]->freq);
    // analogWrite(pins[0]->indle->pin, pins[0]->PIN_OFF);

    // THE SECOND BRIDGE
    // analogWrite(pins[1]->inuse->pin, pins[1]->freq);
    // analogWrite(pins[1]->indle->pin, pins[1]->PIN_OFF);
} 

void HBridge::print(void){

    printf("---------\nBRIDGE ONE\n");
    print_bridge(pins[0]);

    printf("---------\nBRIDGE TWO\n");
    print_bridge(pins[1]);
}


int main(int argc, const char*argv[]){
    HBridge b(31,42,33,4);
    b.setPinFrequency(A,200);
    b.print();
}

2 个答案:

答案 0 :(得分:2)

Pin a_use是构造函数的本地,并且您正在从中创建指针,尽管在contsructor返回时它将被释放。

inuseidle声明为Pin inuse;Pin idle;并且不要创建指针,只是为它们分配pinWith()返回的内容,值将为被复制,问题就会消失。

如果你需要它们作为指针,那么首先请求像这样的堆空间

bridge_a.inuse = new Pin;

然后执行此操作

memcpy(bridge_a.inuse, &a_use, sizeof(Pin));

如果Pin不包含任何类但只包含POD个字段,则会有效,否则将需要更详细的副本。

答案 1 :(得分:0)

回应@iharob。你在pinWith()函数中遇到同样的问题,你在Pin函数的堆栈中返回pinWith(),这个位置变成了垃圾&#34;一旦你从功能返回。您需要在Pin中从堆中分配pinWith()来解决此问题。

Pin pinWith(int num) {
    // pinMode(num, OUTPUT);
    Pin p = new Pin;
    p.pin = num;
    return p;
}