Advertisement

Getting 0xcccccccc error

Started by September 26, 2017 02:57 AM
15 comments, last by matt77hias 7 years, 2 months ago

you're problem is that you are declaring a global in a header file like that. What happens is that each cpp file that includes that utils header, gets its own copy of the static variable. You end up with many, and depending how you initialize them, destructor mayhem.


Simple Test:


//util.h
#pragma once
#include <iostream>
#include <string>

static int globali = 5;
  
//main.cpp
#include "utilglobals.h"
#include "cpp2.h"

void main() {

	testGlobal();
	std::cout << "main" << (int)&globali << std::endl;

	std::cin.get();
}
//cpp2.h
#pragma once
#include "utilglobals.h"
void testGlobal();
  
//cpp2.cpp
#include "cpp2.h"
void testGlobal() {
	std::cout << "cpp2" << (int)&globali << std::endl;
}

 

if you want to do a global you'd use a singleton of some sort:


class Singleton {
	Singleton();

	static Singleton mInstance;
public:
	static Singleton* getInstance() {
		return &mInstance;
	}

	void func();
};
//in a cpp
Singleton Singleton::mInstance;	//only exists in 1 cpp file

Singleton::getInstance()->func();

 

Advertisement

@h8CplusplusGuru Thanks for letting me know, I though that pragma once would ensure only one copy of the thing (so now I have no idea about what pragma once is doing, and furthermore the usage of static keyword...

This is my output:

Quote

00092390 Constructor
00092448 Constructor
00092500 Constructor
000925B8 Constructor
00092670 Constructor
00092728 Constructor
000927E0 Constructor
00092898 Constructor
00092950 Constructor


00092950 Destructor
00092950 Font was 0030FC88
00092898 Destructor
00092898 Font was 0030A7C8
000927E0 Destructor
000927E0 Font was 00305308
00092728 Destructor
00092728 Font was 002FFE48
00092670 Destructor
00092670 Font was 002FA988
000925B8 Destructor
000925B8 Font was 002F54C8
00092500 Destructor
00092500 Font was 002F0008
00092448 Destructor
00092448 Font was 002C5D68
00092390 Destructor
00092390 Font was 002C0440

so basically I was creating and destroying 9 identical Font objects, very wasteful xD 

Your header files are managed by each cpp, not the singular program. Pragma once means it's only included once per a cpp. Static is going to be redundant at a module scope, which is always program lifetime - at other scopes, it effectively makes them module in lifetime. Static is going to be used inside classes, inside functions.

On a somewhat related topic, you want to include as few headers in other headers as possible, and include them in cpps instead. You want to have as little code other than interfaces in headers as possible, because when the header changes, the cpp users compile. Both of these tips will increase your compile speeds. (Also, on msvc, enable, /MP, which is multiprocessor compilation ).

Historically one used the following construct via macros


#ifndef MY_HEADER_HPP

#define MY_HEADER_HPP

// some DEFINITIONS (doesn't matter for DECLARATIONS only)

#endif

to ensure at most one "actual include" in each translation unit, otherwise you would have clashes. 

All C/C++ compilers support this (It is actually the preprocessor who support this).

The following construct


#pragma once

is supported by some compilers such as MVC++ and achieves the same result with the additional benefit of not polluting your namespace of globally defined symbols (which negatively influences your preprocessor's and probably "intelligent" IDE's performance due to some larger lookup tables) with one defined symbol for each header.

🧙

On 28-9-2017 at 5:20 PM, MarcusAseth said:

static keyword

The static keyword has multiple use cases including imposing internal linkage. static struct/class member variables and methods, have, however, nothing to do with internal linkage. It basically adds data and functionality to the struct/class instead of the instances of that struct/class.

🧙

This topic is closed to new replies.

Advertisement