Question about passing a struct to a function by reference. One of the properties reads -3689348814741910324

I'm trying to learn how to allocate memory reading a tutorial. I wanted to allocate 128mbs of memory. 64 for permanent memory and another 64 for allocating temporary memory.

1
2
3
4
5
6
7
8
9
10
11
struct MemoryManager {
  unsigned char* memory;
  unsigned char* next;
  long long bytesRemaining;
};

struct Management {
  MemoryManager memory;
  MemoryManager tempMemory;
};


When I pass the Management struct above as reference to the following function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void InitManager(Management* management) {

  long long permMemorySize = megabytes(64);
  long long tempMemorySize = megabytes(64);
  long long totalMemorySize = permMemorySize + tempMemorySize; 

 	unsigned char* memory = (unsigned char*)VirtualAlloc((LPVOID) gigabytes(1), totalMemorySize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

	management = (Management*)memory;
	
	CreateMemoryManager(&management->memory, memory + sizeof(Management), permMemorySize - sizeof(Management));
	CreateMemoryManager(&management->tempMemory, memory + permMemorySize, tempMemorySize);

        // I get the right amount here!
	std::cout << "Bytes Remaining 1: " << management->memory.bytesRemaining << std::endl;
}



...Inside the function the total remaining bytes I cout at the end is correct for each memory allocator. (67108832 bytes each allocator)


...but after the function finishes, if I read and cout the bytes remaining for any of the allocators, I get -3689348814741910324.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Game {
	Management management;
};


int main() {

	Game game;

	
	InitManager(&game.management);

	std::cout << "Bytes Remaining 2: " << game.management.tempMemory.bytesRemaining << std::endl;

	return 0;
}



I'm not sure why this happens :( I'm clearly not understanding something. Would anybody have any idea?

-----

Just in case it's useful, I'll add the CreateMemoryManager functions below. Thanks!

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
void CreateMemoryManager(MemoryManager* manager, unsigned char* memoryIN, long long size) {

	manager->memory				= memoryIN;
	manager->next				= memoryIN;
	manager->bytesRemaining		= size;
}

unsigned char* AllocateMemory(MemoryManager* manager, long long size) {
	assert(manager->bytesRemaining >= size);
	unsigned char* mem = manager->next;
	manager->next += size;
	return mem;
}

// ..


long long kilobytes(unsigned int kb) {
	return kb * 1024;
}
long long megabytes(unsigned int mb) {
	return kilobytes(mb * 1024);
}
long long gigabytes(unsigned int gb) {
	return megabytes(gb * 1024);
}
At a glance: You assign the value of memory to management.
management is a local pointer variable passed by value. By assigning something to management, you are essentially overwriting whatever value for management you passed into the function.

The &game.management object you pass in is never modified.
Perhaps you meant to pass it by reference? (Management*& management)
Last edited on
Hey Ganado that worked perfectly, thanks a lot :)
Topic archived. No new replies allowed.