Re: Help - how to pass an array of struct from one module to another?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thursday 10 June 2004 14:12, l x wrote:
> I create two modules modA and modB (real files
> attached below: modA.c, modB.c and modA.h).  I intend
> to make them work like this:
>
> - "insmod modA.o" will initialze the array of struct,
> pA[0], pA[1] and pA[2].
> - "insmod modB.o" will call the function
> modA_func_1(ptr) defined in modA, which copies the the
> pointers "ptr = pA".  I assume I should see the
> following 3 line on the console:
>
> ################ ptr[0].i=0
> ################ ptr[1].i=10
> ################ ptr[2].i=20
>
> But I only see 3 ramdom numbers (not 0, 10, 20).  This
> means modA_func_1(ptr) defined in modA doesn't do "ptr
> = pA" correctly, or somehow the ptr value gets changed
> when function modA_func_1(ptr) returns.

There is nothing module or kernel specific here.  In modB your ptr will 
be unchanged after the call to modA_func_1(), because you never 
dereference it, so when you print out the values you are printing out 
the contents of an uninitialized structure.  There are also two memory 
leaks.  There are several ways to fix this depending on what you are 
trying to do.  

1) Have modB skip the kamlloc and pass a pointer to ptr.  e.g. 
modA_func_1(&ptr)

2) Have modA skip the kmalloc and the ptr = pA assignment and use 'ptr' 
directly in the loop.  You'll need to fix up the type for 'ptr'.

3) ???




--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux