allocating memory and processing thru function. 
Author Message
 allocating memory and processing thru function.

Used: C,djgpp, Rhide.IDE..WinME.
Project: Mouse display on graphic mode 800x600 16-bit color.
Dear All,
    The program is quite strange. When trying to load a red square:64x64pix
for cursor, no problem the red square shows, but the location is not center
as intended. When trying to load a red arrow, 64x64pix for cursor, no
display at all and the IDE Rhide get hanged. Have to quit and restart Rhide
again. Part of the program which I suspect cause the problem is trying to
pass external pointer to a function which allocate memory and processed some
data whch should be stored back to the pointed location.

struct pixel
{
  BYTE red;
  BYTE green;
  BYTE blue;

Quote:
};

typedef struct tagMOUSEBITMAP MOUSEBITMAP;
 struct tagMOUSEBITMAP {
   int hot_x;
   int hot_y;
   word data[64*64];//mouse size
   MOUSEBITMAP *next;
   };

 typedef struct {
   byte on;
   byte button1;
   byte button2;
   byte button3;
   int num_buttons;
   sword x;
   sword y;
   word under[64*64];//mouse bitmap size
   MOUSEBITMAP *bmp;
   } MOUSE;

int ld_converted_image(char filename_ex[101],MOUSEBITMAP *(*k)) {
char choice, fileHeaderBuffer[54];
word converted;
int width, height, i;
float r1,g1,b1;
struct pixel *h,*start;
start =load_image(fileHeaderBuffer, &width,&height, filename_ex);
*k=(MOUSEBITMAP *)malloc(sizeof(MOUSEBITMAP));//to store the converted
bitmap
memset(*k,0,sizeof(*(*k)));
i=0;
   for (h=start;h<(start+width*height);h++) {
  _24toRGB_2(*h, &r1, &g1, &b1);
  converted=RGBto16(r1,g1, b1);
  (*k)->data[i]=converted;
  i++;
  }
free(*k);

Quote:
}

int main() {
MOUSE  mouse;
  MOUSEBITMAP *mb[NUM_MOUSEBITMAPS];

ld_converted_image("arrow",&mb[0]);//bitmap file.
 ld_converted_image("red",&mb[1]);

free(mb[0]);
free(mb[1]);

Quote:
}

Is there a better method to pass &mb[0] to the function and expect itself to
be filled with the required data? The one above is
quite uncertain to me?
Please advise.
Thank you.

Patrick



Wed, 12 Jan 2005 10:24:56 GMT  
 allocating memory and processing thru function.
On Sat, 27 Jul 2002 10:24:56 +0800, "Patrick Chen"

Quote:

>Used: C,djgpp, Rhide.IDE..WinME.
>Project: Mouse display on graphic mode 800x600 16-bit color.
>Dear All,
>    The program is quite strange. When trying to load a red square:64x64pix
>for cursor, no problem the red square shows, but the location is not center
>as intended. When trying to load a red arrow, 64x64pix for cursor, no
>display at all and the IDE Rhide get hanged. Have to quit and restart Rhide
>again. Part of the program which I suspect cause the problem is trying to
>pass external pointer to a function which allocate memory and processed some
>data whch should be stored back to the pointed location.

I don't understand these comments but you are freeing the same area
twice which may explain whatever problem you are having.

Quote:

>struct pixel
>{
>  BYTE red;
>  BYTE green;
>  BYTE blue;
>};

>typedef struct tagMOUSEBITMAP MOUSEBITMAP;
> struct tagMOUSEBITMAP {
>   int hot_x;
>   int hot_y;
>   word data[64*64];//mouse size
>   MOUSEBITMAP *next;
>   };

> typedef struct {
>   byte on;
>   byte button1;
>   byte button2;
>   byte button3;
>   int num_buttons;
>   sword x;
>   sword y;
>   word under[64*64];//mouse bitmap size
>   MOUSEBITMAP *bmp;
>   } MOUSE;

>int ld_converted_image(char filename_ex[101],MOUSEBITMAP *(*k)) {

On the first call to this function, k is &mb[0].  *k is therefore
mb[0].

Quote:
>char choice, fileHeaderBuffer[54];
>word converted;
>int width, height, i;
>float r1,g1,b1;
>struct pixel *h,*start;
>start =load_image(fileHeaderBuffer, &width,&height, filename_ex);
>*k=(MOUSEBITMAP *)malloc(sizeof(MOUSEBITMAP));//to store the converted
>bitmap

This causes mb[0] to contain the address of the allocated area.  You
should not cast the return from malloc since doing so can never help
but can hide problems which you need to know about.

Quote:
>memset(*k,0,sizeof(*(*k)));
>i=0;
>   for (h=start;h<(start+width*height);h++) {
>  _24toRGB_2(*h, &r1, &g1, &b1);
>  converted=RGBto16(r1,g1, b1);
>  (*k)->data[i]=converted;
>  i++;
>  }
>free(*k);

This frees mb[0] the first time.  Any attempt to access the memory
that mb[0] points to causes undefined behavior.  Considering that you
went to a lot of trouble to initialize the area mb[0] points to, this
line is probably a mistake.

Quote:
>}

>int main() {
>MOUSE  mouse;
>  MOUSEBITMAP *mb[NUM_MOUSEBITMAPS];

>ld_converted_image("arrow",&mb[0]);//bitmap file.
> ld_converted_image("red",&mb[1]);

>free(mb[0]);

This frees mb[0] a second time.  You have just invoked undefined
behavior.

Quote:
>free(mb[1]);
>}
>Is there a better method to pass &mb[0] to the function and expect itself to
>be filled with the required data? The one above is

This is the usual approach.  It is hard to imagine how it could be
much simpler.  If you remove the call to free from your function,
things may improve.

Quote:
>quite uncertain to me?

<<Remove the del for email>>


Wed, 12 Jan 2005 14:23:44 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. allocating memory in one function and freeing it with another function

2. Any way to see memory allocated per process?

3. Do the string functions allocate memory?

4. standard variadic functions allocate memory?

5. Allocating memory inside a function

6. allocating memory with functions

7. allocate memory in a function

8. allocate memory for function

9. getline function that allocates memory on-the-fly

10. Allocating memory for function argument

11. How to access memory allocated in function

12. Freeing memory allocated inside a function

 

 
Powered by phpBB® Forum Software