C language > Beginner questions

Pointer Problem?

(1/2) > >>

Max_Power_Up:
Good Morning Everybody.

I seem to be having a very basic problem, Im sure this must be one of the
most common problems in the world.

I've been programming in C for a while now and I simply love the new
debugger thats built into PellesC IDE
Well, the problem Im having is with a function call to a .dll that I've written.

The DLL contains two functions :

--- Code: ---typedef struct tagRGBT
{
unsigned char R;
unsigned char G;
unsigned char B;
}RGBT;


void WINAPI (dllexport )func1(char *name)
{
strcpy(name,"this is a test");
}
void WINAPI (dllexport)func2(RGBT *bits)
{
int i;
for(i =0 ; i < 256 ; i++)
    {
       bits[i].R = 255;bits[i].G = 255; bits[i].B = 255;
   }

}



--- End code ---

The only problem with this code is that when I call it using :


--- Code: ---func1(&mylocalarray);
func2(&mylocalrgbtarray);

--- End code ---

then *mylocalarray* gets updated with the correct values, but
the *mylocalrgbtarray* does not even get passed properly,
is there any standard limit to the types of pointers that we
can pass into dll's or the memory mapping modes or something?
Some help would be appreciated, I will post the full source once I get
confirmation that somebody is actually reading these forums lol!

Thanks again Pelle for the great IDE...
[/color]

Diddl:
it is not the same thing. first example is a pointer to char. second is a pointer to a struct (not a pointer to a struct array).

I suppose it works fine for i=0.


--- Code: ---by incrementing i it grows to 1.
bits[i] means *(bits +i).
bits is a pointer, and length of a pointer in Pelles for win32 is 4 byte.
*(bits +1) points to an address 4 bytes after bits.
*(bits +2) points to an address 8 bytes after bits.

I suppose (bits +1) points in middle of your structure.
--- End code ---

sorry my english is bad, I hope you understand me.

frankie:
Yes Diddl there is something wrong the way Max_Power_Up have defined the variables (or the way he pass them).
What is the definition of "mylocalarray" and "mylocalrgbtarray" ?
(post a sample of your code).

Max_Power_Up:

--- Code: ---//these functions are in the dll...


int WINAPI OPVisualizationDetails(char *name,int bflen)
{
//name = malloc(strlen(OUR_VIS_NAME)+1);
int terminator = 0;
int ourbflen = strlen(OUR_VIS_NAME);
if(ourbflen > bflen)
terminator = bflen;
else
terminator = ourbflen;

int robocop;
for(robocop =0 ; robocop < terminator ; robocop++)
name[robocop] = OUR_VIS_NAME[robocop];
//strcpy(name,OUR_VIS_NAME);
//we malloc it and our 'parent' frees it.
return 0;
}

int WINAPI OPVisualizationRender(unsigned char *bits, float *spectrum, int w, int h)
{
//automatic...
memset(bits,255,(w * h  * 3));

//manual
//unsigned char * p = (unsigned char *)bits;

int i;
for(i = 0 ; i < (256) ; i++)
{
//*p++ = (unsigned char)196;
bits[i] = (unsigned char) 64;
// bits[i] = 63;
}
//do something that will show on the screen...
return 0;
}




//these functions are in the calling code :

MI_VISUALIZATIONS[MI_CURRENTVIS].VIS_RENDER((unsigned char *)MEDIA_INFORMATION.MI_SPEC_IMG.I_PIXELS,
(float *)MEDIA_INFORMATION.MI_SPECTRUM,
(int) MI_SPEC_BM_W,
(int) MI_SPEC_BM_H);

//the VIS_RENDER is actually the pointer to that function
//since I stepped through it with the debugger and it is entering the function
//the only problem is that it does not set the values for the bit... whic is :


typedef struct tag_RGBT
{
unsigned char R;
unsigned char G;
unsigned char B;
}RGBT;

typedef struct tag_IMG
{
int I_W;
int I_H;
int I_X;
int I_Y;
RGBT *I_PIXELS;
}IMG;

--- End code ---

which doesn't work since I get the first function (OPVisualizationDetails) to
input the correct values for us (OUR_VIS_NAME) is defined as "OurFirstVis", and that bit is working... I can't for the life of me figure it out...please help...?

frankie:
I think that there is a little mess here.  ::)
You define a structure of three chars, the colors, and pass it as a char pointer to a function to represent a multidimensional array of pixel structures.  :P Am I correct ?
Then you access sequencially the colour components treating them as a contiguous array of bytes.
Have you considered the packing that the compiler apply?
The structure RGBT occupy 12bytes not three, because by default the compiler alignes on DWORD boundary unless you instruct it to behave differently.
As first option modify the structure declaration applying packing pragma:

--- Code: ---#pragma pack(1) //No alignement
typedef struct tag_RGBT
{
unsigned char R;
unsigned char G;
unsigned char B;
}RGBT;
#pragma pack() //Restore default alignement
--- End code ---
Now your scanning scheme should work accessing the correct structure members.
Anyway I suggest to use a more direct approach passing a structures pointer, and accessing directly the members.
Be sure to recompile all the sources after modification or a different alignement btween modules will crash the program.

Navigation

[0] Message Index

[#] Next page

Go to full version