LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem when using calloc function in CVI

I use calloc function to create 10 block of a struct. the pointer returned point a struct array.
 
p_testDB = (struct testDB*)calloc(sizeof(struct testDB),10); 
 
But to my surprise, the pointer p_testDB only point to a single struct testDB. I used this function serveral times. never see this kind of problem, i try to check my old file and this one so detaily, nothing difference found. anyone who can advise me this?
 
 
Thanks in advance!
Gerry Li
0 Kudos
Message 1 of 14
(4,887 Views)
I normally use this architecture and it works correctly allocating the desired array of structure elements:
 
// Variable definition
typedef struct {
  // Struct elements...
} myStruct;
myStruct      *myStructVar = NULL;
 
// Memory allocation
myStructVar = calloc (numOfElements, sizeof (myStruct));
I noted in your code that number of elements and element size are exchanged: this is probably a simple mistake in copying the code here...


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 14
(4,881 Views)

Hello Roberto

Thanks for you promote reply first.

actually i used this command in background printing tasking.  i want to copy the test  struct array  to a temp buff to avoid two threading access same buffer on same time, so i need allocate a buff that same as test struct array. in otherword, this command work well in another C file which in my project also. i try to copy this command to that C file , it's work propery (any place).   Indeed , i tried so clear to compare the include .h file in both C file. no so much difference.

I am a little bit headache on this. 😞

 

Gerry Li

0 Kudos
Message 3 of 14
(4,882 Views)

Hello Gerry,

I definitely confirm that calloc receives the number of elements in the first parameters and the size of each element in the second. This is practically equivalent to your notation since what is effectively allocated is a space of numOfElements * sizeOfElement bytes but can conceptually lead to some misunderstanding.

 

Given this, I don't know why you are seeing one single element only, unless it is only a problem of CVI variable window settings. Try this: break in the program and view p_testDB variable in the variables window; be sure the structure is in compressed format (the "+" icon shown on the left and no element shown) and execute Options >> Interpret as... menu function: select in the list the type struct testDB * instead of struct testDB that is probably selected; press OK and expand again your variable: now it should correctly show your 10 elements array of structures.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 14
(4,868 Views)

Hello Roberto

 Yes, you are right, follow the instruction of calloc function. the first parameter should be num of element.  but i do not know. why this fucntion work will in my project after i  exchanging  the parameter. i copy the screen and put in the attached file.for you reference.

 for my problem, i am sorry to tell you that , still not fixed. I use the function in option menu to get the number of element , only 1. see attachemnt for detail.

Thanks

Gerry  Li 

 

0 Kudos
Message 5 of 14
(4,840 Views)

Hello Roberto

I can not put the attachment to BBS. can you tell me you mailing address?

 

0 Kudos
Message 6 of 14
(4,840 Views)
You are overwriting p_TestDB in the next statement after you calloc it.
 
p_TestDB = calloc (100, sizeof(struct testDB);    // allocate memory for 100 elements of struct testDB
 
p_TestDB = ThisPtr -> hdlPrtDBPtr;    // you just reassigned p_TestDB to point to the hdlPrtDBPtr member of the structure at ThisPtr.  It no longer points to 100 testDB's.
0 Kudos
Message 7 of 14
(4,819 Views)

@ Al - as we can see in the variable window posted, the reassignment is not already performed at the breakpoint: address of p_TestDB is 10k higher than that of ThisPtr. Nevertheless, this will be a problem in further execution of the program since he wants to have a copy of memory structure while he is actually simply reassigning the memory block pointer.

@ Gerry - I see in the variable window that p_testDB is assigned PtrTestDB type. Can you reassign it to ptrTestDB * type instead?
In any case, look at my profile for my e-mail address.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 8 of 14
(4,786 Views)

Hello Roberto

 I tried according to you suggestion. no any effort.  also i try to define a struct in print.c file and result looks same.  So i do not think this is a problem in my struct defination. i send you this one , you may able to find some some details.

 

 

Thanks

Gerry Li

 

 

0 Kudos
Message 9 of 14
(4,755 Views)

Gerry, I see no evident reason for the behaviour you are observing: integrating your code in a program of mine leads to a correct definition of an array of structures.

Regardless the Variable window aspect, can you access individual array elements (e.g. assigning p1[1].a = 12; ) Or, have you tried using Options >> Estimate number of elements menu function?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 10 of 14
(4,742 Views)