# same 8bit created surface resulting in a different pitch value

Given:

const int w = 10;
const int h = 10;
const int d = 8;
uint8_t* pixels = new uint8_t[w * h]

SDL_Surface* src = SDL_CreateRGBSurfaceFrom(pixels, w, h, d, w, 0, 0, 0, 0);
SDL_Surface* dst = SDL_ConvertSurface(src, src->format, 0);
// src->pitch == 10;
// dst->pitch == 12;
//std:: cout << src->pitch == dst->pitch << std::endl;


My expected result is that the two pitches must match.

In this case I have the "weird" 12 pitch value for the dst image create from the src.

• Is that due to memory alignment optimization?
• Is there any reasonable explanation about it?
• Are those padding bytes used somehow or just wasted?

The following is based on a quick look at the SDL source code for SDL_ConvertSurface:
SDL_ConvertSurface calls SDL_CreateRGBSurface, which determines the used pitch with SDL_CalculatePitch, which aims to pad the surface to 4-bytes for speed reasons. This makes sense as memory is likely to be fetched in minimum 32-bit chunks (at least on x86).