We help IT Professionals succeed at work.

converting unsigned char * to unsigned char**

astudent
astudent asked
on
553 Views
Last Modified: 2010-04-21
Hello expert,Please check following code, am i doing it right way or not.
My purpose is to store data read from a file and stored in single dim array to double dim array.



void far *Buffer ;
unsigned char *I ;  //Note I is different than Buffer
unsigned char **arr;
 
//read from file
fread((void *)Buffer, (size_t)1, (size_t)ImageSize, file_ptr);
	
//Make I Point to same as Buffer
I=(unsigned char *)Buffer
     
// now from I, fetch it in arr.
// first allocate memory to arr.
for(i=0;i<rows;i++)
{
    arr[i]=(unsigned char *)malloc(cols*sizeof(unsigned char));
}
 
for(i=0;i<rows;i++)
{
     for(j=0;j<cols;j++)
     {
	arr[i][j]=*(I+i*Iw+j);
     }
}

Open in new window

Comment
Watch Question

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
before you use arr in the first for() loop, you need to allocate it, like:

arr = (unsigned char **)malloc(rows*sizeof(unsigned char *));
CERTIFIED EXPERT
Top Expert 2009

Commented:
You need to allocate memory for Buffer too.

But why all the pointer casts and all the copying ? Why not simply :
unsigned char **arr = (unsigned char**) calloc(rows, sizeof(unsigned char*));
 
for (i = 0; i < rows; i++) {
    arr[i] = (unsigned char*) calloc(cols, sizeof(unsigned char));
    fread(arr[i], 1, cols, file_ptr);
}

Open in new window

CERTIFIED EXPERT
Top Expert 2009

Commented:
Or if you want to do everything in one read, you could even :

unsigned char *Buffer = (unsigned char*) calloc(rows * cols, sizeof(unsigned char));
fread(Buffer, 1, rows * cols, file_ptr);
 
unsigned char **arr = (unsigned char**) calloc(rows, sizeof(unsigned char*));
 
for (i = 0; i < rows; i++) {
    arr[i] = Buffer + (i * cols);
}

Open in new window

If you know the rows and cols in advance, you don't need to allocate from the heap but define the buffer at the stack:

const int ROWS = 100;
const int COLS  = 50;

unsigned char table[ROWS][COLS] = { 0 };
ifstream ifs(inputfile, ios::binary | ios:: in);
if (!ifs.read((char*)table, ROWS * COLS)) throw(MyException("Error when reading"));
ifs.close();

Note, I used C++ filestreams cause you posted in the C++ TA.

Regards, Alex

Author

Commented:
thanks all.
but actually i m reading data from  a file and does not know no. of rows, cols. this also becomes known when data has been read.

and the code given by jaime_olivares still required, even if i m writing
for(i=0;i
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> but actually i m reading data from  a file and does not know no. of rows, cols. this also becomes known when data has been read.

The code in your original post had ImageSize, rows and cols already known. If you don't know them, you can obtain that information from the file (if the file contains a header, then the header will most likely have that information - otherwise, you'll have to find the size of the file, and calculate the rows and cols values from that).

Author

Commented:
Please reply this.

>> and is the code given by jaime_olivares still required, even if i m writing
for(i=0;i
Software Architect
CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks,it worked.
Please also see :
https://www.experts-exchange.com/Programming/Languages/CPP/Q_23571959.html

now the other way, unsigned char ** to unsigned char *.
CERTIFIED EXPERT
Top Expert 2009

Commented:
I hope you took note of my replies too, as it's very important to allocate memory for Buffer too, and there are a lot easier ways of doing this that what you do (ie. you're over-complicating things).

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.