Search Results
6/20/2025, 11:24:00 PM
6/9/2025, 7:01:49 AM
>>105530074
So it's my understanding that returning pointers to local variables in C is UB. However when using Vulkan I've encountered this pattern:
VkShaderModule create_shader(
VkDevice device,
const std::vector<char>& shader_code)
{
VkShaderModuleCreateInfo create_info {};
create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
create_info.codeSize = shader_code.size();
create_info.pCode = static_cast<const uint32_t*>(
static_cast<const void*>(shader_code.data()));
VkShaderModule shader_module {};
auto rv = vkCreateShaderModule(device,
&create_info,
nullptr,
&shader_module);
if (rv != VK_SUCCESS)
{
throw std::runtime_error("Failed to create shader module");
}
return shader_module;
}
Where VkShaderModule is a typedef struct VkShaderModule_T* VkShaderModule;
Why exactly does this work? Is it because the memory for VkShaderModule lives somewhere else? I'm not clear on this.
So it's my understanding that returning pointers to local variables in C is UB. However when using Vulkan I've encountered this pattern:
VkShaderModule create_shader(
VkDevice device,
const std::vector<char>& shader_code)
{
VkShaderModuleCreateInfo create_info {};
create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
create_info.codeSize = shader_code.size();
create_info.pCode = static_cast<const uint32_t*>(
static_cast<const void*>(shader_code.data()));
VkShaderModule shader_module {};
auto rv = vkCreateShaderModule(device,
&create_info,
nullptr,
&shader_module);
if (rv != VK_SUCCESS)
{
throw std::runtime_error("Failed to create shader module");
}
return shader_module;
}
Where VkShaderModule is a typedef struct VkShaderModule_T* VkShaderModule;
Why exactly does this work? Is it because the memory for VkShaderModule lives somewhere else? I'm not clear on this.
Page 1