yesterday I had posted a question: How should I pass a pointer to a function and allocate memory for the passed pointer from inside the called function?
From the ans
It also looks like, You are using uninitialized rows and cols variables
rows
cols