*new* struct not getting initialized to 0

I'm doing the bunny excersise thingy, and i want to have a bunnyEvent struct.

problem is, when i create it, it doesn't get initialized to 0 ? which is important because it contains a "turns" variable that obviously has to start at 0

heres my code:
<bunny.hpp>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once

#include <string>
#include <stdint.h>
#include <cstdlib>
#include <time.h>

#define BUNNY_WHITE 1
#define BUNNY_BROWN 2
#define BUNNY_BLACK 4
#define BUNNY_SPOTTED 16


typedef struct
{
	uint32_t turns;
	uint8_t died:1, alive:1, born:1, bitten:1;

}BunnyEvent;

class Bunny
{
private:
	bool m_isMale;
	bool m_isRad;
	uint8_t m_color;
	uint8_t m_age;
	std::string m_name;

	BunnyEvent* m_pBunnyEvent;


	std::string randomName(std::string filepath);

	
public:
	Bunny(uint8_t bunnyColor);
	~Bunny();

	BunnyEvent UpdateTurn();

};



<bunny.cpp>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "bunny.hpp"



Bunny::Bunny(uint8_t bunnyColor)
{
	srand(time(NULL));
	m_isMale = (rand()%2) == true;
	m_isRad = (rand()%2) == true;
	m_color = bunnyColor;
	m_age = (rand()%11);
	m_name = randomName("test.txt");


	m_pBunnyEvent = new BunnyEvent;
	printf("%d\n",m_pBunnyEvent->turns);



}

Bunny::~Bunny()
{
	delete m_pBunnyEvent;
}

std::string Bunny::randomName(std::string filepath)
{
	return "test_name";
}

BunnyEvent Bunny::UpdateTurn()
{
		
	
	//NOTE(stav): check & update age
	


	return (*m_pBunnyEvent);
}


as you can see, im creating a new BunnyEvent on the heap and then printing out the "turns" variable.
but it prints out some garbage value instead of 0?

example output:
3496416
Last edited on
prints out some garbage value instead of 0?

The program doesn't know that you're expecting a zero. Make that zero a default value to your BunnyEvent (assumes non-obsolete compiler; prior to 2011 give it a constructor) or initialize it when it is created in the Bunny's constructor:
1
2
3
4
5
6
struct BunnyEvent
{
	uint32_t turns = 0;
	uint8_t died:1, alive:1, born:1, bitten:1;

};


This could also be a good time to stop using new/delete: if you're creating an event in constructor and destroying it in the destructor of bunny, it should be simply a member of bunny.
Last edited on
It's not being initialized to 0 because you never initialize it to 0. If you called the default constructor used value initialization (thanks mbozzi), it would get initialized to the default value of 0, however.

ex:
m_pBunnyEvent = new BunnyEvent;
becomes
m_pBunnyEvent = new BunnyEvent();

https://stackoverflow.com/questions/5914422/proper-way-to-initialize-c-structs

PS: The C++ headers for time.h and stdint.h are <ctime> and <cstdint>
Last edited on
new T and new T() are equivalent.
Last edited on
No. new T(); causes value initialization, while new T; causes default initialization.

http://en.cppreference.com/w/cpp/language/value_initialization
http://en.cppreference.com/w/cpp/language/default_initialization

In this case (where T is BunnyEvent), value-initialization means that the new object is zero-initialized, and that's it. Default-initialization simply calls the trivial default constructor, which does nothing.
Last edited on
Topic archived. No new replies allowed.