stbi_load an image, then write it back out generates access violation #1840
Answered
by
mmozeiko
quaesitor-scientiam
asked this question in
Q&A
-
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <stdint.h>
#define REPEAT_COUNT 16
#define TILE_SIZE 8
#define IMAGE_WIDTH (TILE_SIZE * REPEAT_COUNT)
#define IMAGE_HEIGHT TILE_SIZE
int main() {
char* filename = "tile_pattern2.png";
unsigned char* image_data = stbi_load(filename, IMAGE_WIDTH, IMAGE_HEIGHT, 4, 0);
stbi_write_png("sep_test.png", IMAGE_WIDTH, IMAGE_HEIGHT, 4, image_data, IMAGE_WIDTH * 4);
return 0;
}
What is wrong? |
Beta Was this translation helpful? Give feedback.
Answered by
mmozeiko
Sep 25, 2025
Replies: 1 comment
-
|
You're passing integers to 2nd to 4th arguments of stbi_load which expects pointers instead. So compiler produces code that tries to write to invalid address and thus crashes at runtime. Check your compiler output - it should produce warning about this. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
quaesitor-scientiam
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're passing integers to 2nd to 4th arguments of stbi_load which expects pointers instead. So compiler produces code that tries to write to invalid address and thus crashes at runtime. Check your compiler output - it should produce warning about this.
See correct usage example of stbi_load in the header: https://github.com/nothings/stb/blob/master/stb_image.h#L139-L152