On 30 Nov 2005 20:51:39 -0800,
vashwath@rediffmail.com wrote:
[color=blue]
>Hi all,
>I am not able to figure out why the following program fails. Hope this
>long program does not irritate you.
>#include <stdlib.h>
>#include <string.h>
>#include <stdio.h>
>
>#define K_TITLE_LEN 14
>#define K_MAX_PLMS_ENTRIES 2000
>#define K_SUMMARY_TITLE_LEN 18
>typedef struct
>{
> int desc_code;
> int meas_num;
> char title[K_SUMMARY_TITLE_LEN];
>}cond_summ_data_t;
>typedef struct
>{
> int count;
> cond_summ_data_t data[K_MAX_PLMS_ENTRIES];
>
>}cond_summ_t;
>void listGetCondSumTitle(/* IN */ const cond_summ_t *plms_table,
> /* IN */ int num_meas,
> /* IN */ int num_desc,
> /* IN */ const int *meas_num,
> /* IN */ int *desc_code,
> /* OUT */char title[][K_TITLE_LEN])
>{
> int plms_ind = 0;
> int meas_ind = 0;
> int desc_ind = 0;
> typedef char temp_title_t[K_TITLE_LEN];
> temp_title_t **temp_title;
>
> temp_title = malloc(num_desc * sizeof(temp_title_t *));
> for (desc_ind = 0; desc_ind < num_desc; desc_ind++)
> {
> temp_title[desc_ind] = malloc(num_meas * sizeof(temp_title));[/color]
As others have pointed out, you are using the wrong sizeof. temp_title
is a pointer. Odds are its size is 4. So you have allocated
num_meas*4 bytes.
[color=blue]
> }
>
> for (desc_ind = 0; desc_ind < num_desc; desc_ind++)
> {
> for (meas_ind = 0; meas_ind < num_meas; meas_ind++)
> {
> strcpy(temp_title[desc_ind][meas_ind]," ");[/color]
Here you lie to the compiler. temp_title is a pointer to pointer to
array of 18 char. temp_title[...] is a pointer to such an array.
temp_title[...][...] is such an array. But you did not allocate space
for num_meas such arrays. You under-allocated by 75%. At some point,
strcpy will begin to overflow your allocated area. This is undefined
behavior. One common outcome of doing this is that you destroy the
data malloc uses to keep track of allocated memory.
[color=blue]
> }
> }
>
> /*For each plot meas entry*/
> for (plms_ind = 0; plms_ind < plms_table->count; plms_ind++)
> {
> /*Until no measurement number left in meas_num*/
> for (meas_ind = 0; meas_ind < num_meas; meas_ind++)
> {
> /*Compare with the measurement number of plotmeas meas_no*/
> if ( meas_num[meas_ind] ==
>(plms_table->data[plms_ind]).meas_num )
> {
> /*If Matches*/
> /*Compare with desc code in plotmeas*/
> for (desc_ind = 0; desc_ind < num_desc; desc_ind++)
> {
> /*Compare with desc code in plotmeas.*/
> if (desc_code[desc_ind] == (plms_table
>
>->data[plms_ind]).desc_code)
> {
> /*If matches get the title and store in
>temp_title*/
> strcpy(
> temp_title[desc_ind][meas_ind],
> (plms_table->data[plms_ind]).title);
> break;
> }
> }
>
> }
> }
> }
> for (desc_ind = 0; desc_ind < num_desc; desc_ind++)
> {
> for (meas_ind = 0; meas_ind < num_meas; meas_ind++)
> {
> printf("temp_title[%d][%d] = %7s ",desc_ind, meas_ind,
>temp_title[desc_ind][meas_ind]);
> }
> printf("\n");
> }
>
> for (desc_ind = 0; desc_ind < num_desc; desc_ind++)
> {
> free(temp_title[desc_ind]); /*Program crashes here*/[/color]
And since malloc is now completely confused, free gets confused to.
Be thankful, a program crash is one of the better manifestations of
undefined behavior.
[color=blue]
> }
> free(temp_title);
>}
>[/color]
snip code for main
<<Remove the del for email>>