Malloc code
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 28 Dec 2007 13:58:02 -0800
Hello,
To the ones that are familliar with my previous post entitled ("Simple
question about headers and malloc") ( hummm not as simple as *I* thought!) ,
here are a few things to keep in mind.
You have most probably noticed that I was using an array called "xxx[3]" and
was assigning it contents of my structure like so:
xxx[0]= px[0].A0;
xxx[1]= px[0].A1;
xxx[2]= px[0].A2;
This was because the watch window in my compiler is not able to read the
contents of "px[0].ItemName". This is a known issue to them and are currently
working on it. Therefore any strucutre that I allocate in memory, I must
assign its contents to an array for me to be able to view the data !
Also as stated by David:
Whatever is happening is probably more to do with the fact that you're
declaring pointers in header files.
I still left the pointers in the header files for this example, but rest
assure I will change this and do it as David said. For now I would just like
to see if there is something else that causes the problem.
Okay! here it goes. I was very reluctant in posting such a lenghty sample
but I don't know how else to get everyone to see the full picture so that I
get help that will specifically solve the problem. So here it is.
First and for most, in main, I call the TCP_CONFIG_GMM function which will
set up some data in a strucuture called GMM (Graphics message mask). This
structure will hold very little information for 3 out of the dozens of
messages stored in external flash! So therefore if GMM contains the following:
//GRAPHICS MESSAGE MASK
struct GMM{
int MSG; //MESSAGE #. ( 0= no message required at
this location)
int VPIX_START; //VERTICAL MESSAGE START PIXEL
int HPIX_START; //HORIZONTAL MESSAGE START PIXEL
}
aGMM[]= //Array of Graphics message masks:
{1, 10, 10, //1st MESSAGE NUMBER / VERTICAL PIX / HORIZONTAL PIX
5, 50, 30, //2nd MESSAGE NUMBER / VERTICAL PIX / HORIZONTAL PIX
44, 100, 0}; //3rd MESSAGE NUMBER / VERTICAL PIX / HORIZONTAL PIX
Then, this means that message #1 will be displayed starting at pixel (10,10)
, and message #5 will be displayed starting at pixel (50,30) and message #44
will be displayed starting at pixel (100,0). As you will see later in the
sample code, innitially the GMM structure is reset with default values like
so:
//GRAPHICS MESSAGE MASK
struct GMM{
int MSG; //MESSAGE #. ( 0= no message required at
this location)
int VPIX_START; //VERTICAL MESSAGE START PIXEL
int HPIX_START; //HORIZONTAL MESSAGE START PIXEL
}
aGMM[]= //Array of Graphics message masks:
{0, 255, 255, //1st MESSAGE NUMBER / VERTICAL PIX / HORIZONTAL PIX
0, 255, 255, //2nd MESSAGE NUMBER / VERTICAL PIX / HORIZONTAL PIX
0, 255, 255}; //3rd MESSAGE NUMBER / VERTICAL PIX / HORIZONTAL PIX
The GMM structure is filled with informations via the TCP_CONFIG_GMM function
prior to any screen displaying activity or when new messages are regiquired
for any subsequent screens. The Graphics Message Mask contains all the
messages required for the next screens that will be painted. For now, the GMM
can contain a maximum of 3 messages each with their respective
vertical/horizontal start pixels coordinates. The VPIX_START and HPIX_START
values depict the start pixel locations X/Y for every message.
In the sample code below, when I call the TCP_CONFIG_GMM function, it will
fill in new values in the GMM and create the neccessary arrays of structures
by calling the TCP_LOAD_MCB_X function.
In TCP_LOAD_MCB_X function, you will notice I fill in the items of the
arrays of structures with bogus data such as 1,2,3,4,5,6,7,8,9,10,11,12.
These values are sequentially assigned to the xxx[12] array so I can view the
contents of every item! Up to here I have no problems, everything gets
assigned correctly!
Now, I exit the TCP_CONFIG_GMM function and I am back in main. The next
function I call is the LCD_PAINTSCREEN function which resides in a file
called LCD.c. In this function, I call the Fetch() function which will scan
every pixel location with the ones stored in the GMM. More precicely, the
actual verification is done in the GET_MSG_MSK_REC_FROM_PIXLOC() function. If
255 is returned, then the pixel where any of the messages would start is not
reached. If there is a match, then I return the message # so I know which
pMCB I have to read. Once I return from the GET_MSG_MSK_REC_FROM_PIXLOC()
function I create a memory allocation with the following line:
pCL_B = malloc(15*(sizeof(struct CL_B)));
if(pCL_B==NULL)
{BAD_POINTER=1;}
And up to here everything is fine.
Then I assign the correct pMCB pointer to a general MCB pointer called px.
Now I go on and read the items in the MCB structure that I assigned the
1,2,3,4,5,6,7,8,9,10,11,12 by doing:
for(i=0;i<11;i++)
{
xxx[0]= px[i].ENDOFCHAR;
xxx[1]= px[i].A2;
xxx[2]= px[i].A1;
xxx[3]= px[i].A0;
xxx[4]= px[i].LEADING_PIX;
xxx[5]= px[i].TRAILING_PIX;
xxx[6]= px[i].CHARSPACING;
xxx[7]= px[i].CHAR_TRSP;
xxx[8]= px[i].mCHAR_BCOLOR;
xxx[9]= px[i].lCHAR_BCOLOR;
xxx[10]= px[i].mTEXT_COLOR;
xxx[11]= px[i].lTEXT_COLOR;
}
And when i = 0, I read, (error,2,3,4,5,6,7,8,9,10,11,12) and when i = 1, I
read, (1,2,3,4,5,6,7,8,9,10,11,12) and when i = 2, I read,
(1,2,3,4,5,6,7,8,9,10,11,12) up to when i = 5. So when i = 5, I read
(1,2,3,4,30,128,195,0,9,10,11).
WHen i = 6,7, I read (1,2,3,4,5,6,7,8,9,10,11,12). When i = 8, I read,
(1,5,227,4,7,8,7,8,9,10,11,12) and this is where I pretty much become
confused!
Given my code sample below, I would appreciate to know if I require to pass
back my malloc pointer from TCP_CONFIG_GMM() and pass it back into FETCH()
function?
Here is the full sample code and does compile without errors!
============================================ACM152.c
#include <DeviceSettings_M.h> //DEVICE INNITIALIZATIONS
#include <stdlib.h> //STANDARD LIBRARY
#include <stdlibm.h> //FOR MALLOC
#include <TCP.h> //TCP HEADER
#include <LCD.c> //LCD FUNCTIONS
#include <TCP.c> //TEXT CODE PAGE
void main()
{
//Just assign one message starting at 10,10 !
TCP_CONFIG_GMM(
1, 10, 10,
0, 255, 255,
0, 255, 255);
//PAINTS THE LCD SCREEN
LCD_PAINTSCREEN(
176, //SETS RIGHT HORZ WINDOW EXTENT
0, //SETS LEFT HORZ WINDOW EXTENT
132, //SETS LOWEST VERT WINDOW EXTENT
0, //SETS HIGHER VERT WINDOW EXTENT
24, //MOST SIGNIFICANT START ADDRESS BYTE IN FLASH
0, //MID SIGNIFICANT START ADDRESS BYTE IN FLASH
0); //LEAST SIGNIFICANT START ADDRESS BYTE IN FLASH
}
==================================================
===============================================LCD.c
//PAINTS THE LCD SCREEN
void LCD_PAINTSCREEN (
int SETWINEXTHORZ_MSB,
int SETWINEXTHORZ_LSB,
int SETWINEXTVERT_MSB,
int SETWINEXTVERT_LSB,
int A2,
int A1,
int A0)
{
int i,k;
//Call fetch() at every pixel!
for(i=SETWINEXTVERT_LSB;i<SETWINEXTVERT_MSB;i++)
{
for(k=SETWINEXTHORZ_LSB;k<SETWINEXTHORZ_MSB;k++)
{
FETCH(
&A2, //MSB OF CURRENT MESSAGE ADDRESS TO PAR FLASH
&A1, //MIDSB OF CURRENT MESSAGE ADDRESS TO PAR FLASH
&A0, //LSB OF CURRENT MESSAGE ADDRESS TO PAR FLASH
&i, //CURRENT VERTICAL LOCATIONS
&k); //CURRENT HORIZONTAL LOCATIONS
}
}
}
====================================================
============================================== TCP.h
//defines the pixel color depht (16 bits per pixel)
#define RGB_COLORDEPTH 16
//Defines bytes per pixel
#define BYTESPERPIXEL 2
//Defines number of pixels per horizontal line
#define PIXELSPERHORZLINE 176
//Defines the size of the message indexes in the aGMM array. Please note that
//the aGMM array contains an array of Graphics message mask structures!
//ex: size of GMM structure = 3 items, size of array holding 3 structures =
9 items
//Therefore, 9/3 = 3 messages
#define GMM_BLOCK (sizeof(aGMM)/sizeof(struct GMM))
//Number of bytes per MCB line
#define B_P_MCB_L 16
//GRAPHICS MESSAGE MASK
struct GMM{
int MSG; //MESSAGE #. ( 0= no message required at this
location)
int VPIX_START; //VERTICAL MESSAGE START PIXEL
int HPIX_START; //HORIZONTAL MESSAGE START PIXEL
}
aGMM[]= //Array of Graphics message masks:
{0, 255, 255, //1st MESSAGE NUMBER / VERTICAL PIX / HORIZONTAL PIX
0, 255, 255, //2nd MESSAGE NUMBER / VERTICAL PIX / HORIZONTAL PIX
0, 255, 255}; //3rd MESSAGE NUMBER / VERTICAL PIX / HORIZONTAL PIX
struct MCB{
int ENDOFCHAR; //:TRUE:=End of char / :FALSE:=Valid char
int A0; //LSB OF MESSAGE ADDRESS IN PAR FLASH
int A1; //MIDSB OF MESSAGE ADDRESS IN PAR FLASH
int A2; //MSB OF MESSAGE ADDRESS IN PAR FLASH
int LEADING_PIX; //LEADING SPACES, BEFORE CHARACTER
int TRAILING_PIX; //TRAILLING SPACES, AFTER CHARACTER
int CHARSPACING; //SPACES BETWEEN CHARACTERS
int CHAR_TRSP; //CHARACTER TRANSPARENCY
int mCHAR_BCOLOR; //MSB OF BACK COLOR
int lCHAR_BCOLOR; //LSB OF BACK COLOR
int mTEXT_COLOR; //MSB OF TEXT COLOR
int lTEXT_COLOR; //LSB OF TEXT COLOR
};
//Declare a pointer of type "struct MCB"
struct MCB* pMCB1;
struct MCB* pMCB2;
struct MCB* pMCB3;
struct CL_B{
int BYTE1;
int BYTE2;
};
//Declare a pointer of type "struct CL_B"
struct CL_B* pCL_B;
void TCP_CONFIG_GMM(
int MSG1, int VPX1, int HPX1, //1st MSG WITH VERT/HORIZ START LOCATIONS
int MSG2, int VPX2, int HPX2, //2nd MSG "
int MSG3, int VPX3, int HPX3); //3rd MSG "
void TCP_LOAD_MCB_X(int j);
void FETCH(
int *A2, //MSB OF CURRENT MESSAGE ADDRESS TO PAR FLASH
int *A1, //MIDSB OF CURRENT MESSAGE ADDRESS TO PAR FLASH
int *A0, //LSB OF CURRENT MESSAGE ADDRESS TO PAR FLASH
int *VPX1, //CURRENT VERTICAL START LOCATIONS
int *HPX1); //CURRENT HORIZONTAL START LOCATIONS
void *SizeMCB_X(int m);
int GET_MSG_MSK_REC_FROM_PIXLOC(
int *VPX1, //VERTICAL START LOCATIONS
int *HPX1); //HORIZONTAL START LOCATIONS
====================================================
==============================================TCP.c
void TCP_CONFIG_GMM(
int MSG1, int VPX1, int HPX1,
int MSG2, int VPX2, int HPX2,
int MSG3, int VPX3, int HPX3)
{
int a; //Counter
//LOAD THE GMM STRUCTURE WITH THE CURRENT MASK INFORMATION
//1st Message index
aGMM[0].MSG=MSG1;
aGMM[0].VPIX_START=VPX1;
aGMM[0].HPIX_START=HPX1;
//2nd Message index
aGMM[1].MSG=MSG2;
aGMM[1].VPIX_START=VPX2;
aGMM[1].HPIX_START=HPX2;
//3rd Message index
aGMM[2].MSG=MSG3;
aGMM[2].VPIX_START=VPX3;
aGMM[2].HPIX_START=HPX3;
//Scroll through the GMM structure
for(a=0;a<GMM_BLOCK;a++)
{
if(aGMM[a].MSG > 0) //If no message, then exit loop!
{
TCP_LOAD_MCB_X(a); //CURRENT GMM STRUCTURE
}
}
}
void TCP_LOAD_MCB_X(int j)
{
int i,k;
struct MCB* px;
int xxx[12];
px = (struct MCB *)SizeMCB_X(j); //Size & create array of MCB structures
for(i=0;i<11;i++)
{
k=0;
px[i].ENDOFCHAR = 1; k++;
xxx[k-1]= px[i].ENDOFCHAR;
px[i].A2 = 2; k++;
xxx[k-1]= px[i].A2;
px[i].A1 = 3; k++;
xxx[k-1]= px[i].A1;
px[i].A0 = 4; k++;
xxx[k-1]= px[i].A0;
px[i].LEADING_PIX = 5; k++;
xxx[k-1]= px[i].LEADING_PIX;
px[i].TRAILING_PIX = 6; k++;
xxx[k-1]= px[i].TRAILING_PIX;
px[i].CHARSPACING = 7; k++;
xxx[k-1]= px[i].CHARSPACING;
px[i].CHAR_TRSP = 8; k++;
xxx[k-1]= px[i].CHAR_TRSP;
px[i].mCHAR_BCOLOR = 9; k++;
xxx[k-1]= px[i].mCHAR_BCOLOR;
px[i].lCHAR_BCOLOR = 10; k++;
xxx[k-1]= px[i].lCHAR_BCOLOR;
px[i].mTEXT_COLOR = 11; k++;
xxx[k-1]= px[i].mTEXT_COLOR;
px[i].lTEXT_COLOR = 12; k++;
xxx[k-1]= px[i].lTEXT_COLOR;
}
}
void FETCH(
int *A2, //MSB OF MESSAGE ADDRESS IN PAR FLASH
int *A1, //MIDSB OF MESSAGE ADDRESS IN PAR FLASH
int *A0, //LSB OF MESSAGE ADDRESS IN PAR FLASH
int *VPX1, //CURRENT VERTICAL START LOCATIONS
int *HPX1) //CURRENT HORIZONTAL START LOCATIONS
{
int c,i,BAD_POINTER;
int xxx[16];
struct MCB *px; //Local variant pointer of type MCB
c = GET_MSG_MSK_REC_FROM_PIXLOC(
VPX1, //VERTICAL START LOCATIONS
HPX1); //HORIZONTAL START LOCATIONS
//If pixel reached then display text!
if(c!=255)
{
//Allocate memory for containerLineBuffer. containerLineBuffer is
//used for special storing of data in other code in this function
//which is not shown and not relevant to this issue !
pCL_B = malloc(15*(sizeof(struct CL_B)));
if(pCL_B==NULL)
{BAD_POINTER=1;}
switch(c)
{
case 0:
px = pMCB1;
break;
case 1:
px = pMCB2;
break;
case 2:
px = pMCB3;
break;
default:
break;
}
//JUNK DISPLAYED HERE!!!!
for(i=0;i<11;i++)
{
xxx[0]= px[i].ENDOFCHAR;
xxx[1]= px[i].A2;
xxx[2]= px[i].A1;
xxx[3]= px[i].A0;
xxx[4]= px[i].LEADING_PIX;
xxx[5]= px[i].TRAILING_PIX;
xxx[6]= px[i].CHARSPACING;
xxx[7]= px[i].CHAR_TRSP;
xxx[8]= px[i].mCHAR_BCOLOR;
xxx[9]= px[i].lCHAR_BCOLOR;
xxx[10]= px[i].mTEXT_COLOR;
xxx[11]= px[i].lTEXT_COLOR;
}
free(pMCB1);
free(pMCB2);
free(pMCB3);
free(pCL_B);
}
}
void *SizeMCB_X(int m)
{
#define __CTNRSIZE_X (11*B_P_MCB_L)
switch (m)
{
case 0:
//Assign the address of an array of n structres created in heap to
a pointer !
pMCB1 = malloc(__CTNRSIZE_X*(sizeof(struct MCB)));
return pMCB1;
break;
case 1:
//Assign the address of an array of n structres created in heap to
a pointer !
pMCB2 = malloc(__CTNRSIZE_X*(sizeof(struct MCB)));
return pMCB2;
break;
case 2:
//Assign the address of an array of n structres created in heap to
a pointer !
pMCB3 = malloc(__CTNRSIZE_X*(sizeof(struct MCB)));
return pMCB3;
break;
default:
break;
}
}
int GET_MSG_MSK_REC_FROM_PIXLOC(
int *C_VPX1, //VERTICAL START LOCATIONS
int *C_HPX1) //HORIZONTAL START LOCATIONS
{
int i;
int rvCMMR=255;
//Do as many times as there are graphics message mask records!
for(i=0;i<GMM_BLOCK;i++)
{
//Continue only if any of the graphics message mask
//start pixels equals current LCD pixel location
if(aGMM[i].VPIX_START == *C_VPX1)
{
if(aGMM[i].HPIX_START == *C_HPX1)
{
//Check if the message is required (>0)!
if(aGMM[i].MSG==0) //No message exists.
continue; //Skip the rest of logic and continue
for loop!
rvCMMR = i; //Assign current message mask
break; //Leave this function!
}
}
}
return rvCMMR;
}
===================================================
I hope I have made my problem clearer than my previous posts and I am
*absolutely* open to any suggestions such as no variable declarations in
header files, better emplacements of mallocs or returning of malloc pointers
and so forth. This is because now I finally have a small peice of code sample
where I can actually try all your suggestions!
Again, sorry for the long post and all the details but I fugured I would
explain it the best I could once and for all.
Thankyou all in advance!
--
Sincere and appreciative Regards
Robert
.
- Follow-Ups:
- RE: Malloc code
- From: Robby
- Re: Malloc code
- From: David Webber
- Re: Malloc code
- From: ajk
- RE: Malloc code
- Prev by Date: Re: Simple question about headers and malloc!
- Next by Date: Re: Malloc code
- Previous by thread: Re: MSDN volatile sample
- Next by thread: Re: Malloc code
- Index(es):
Relevant Pages
|