-1

I'm trying to initialize the struct values with init() function. It looks good but somehow it don't assign it when the execution of the init() function is over. Futhermore, inside the init() function everything is ok but when it returns the assigned values are gone. I spent a few hours but can't figure out what is happening.
Here is my code and struct:

typedef struct svm_data
{
    int num_points;
    int num_dimensions;
    double* training_set;    
    double* descision_set;   
} svm_data_t;

void init()
{
  init_data(&test_data);
  init_data(&check_data);

  printf("in init: test_data->num_dimensions: %d\n", test_data.num_dimensions); 
  printf("in init: check_data->num_dimensions: %d\n", check_data.num_dimensions); 
  set_correct_descision_set(&check_data);
}

void init_svm_data(svm_data_t* data)
{
  int np = 3;
  int nd = 3;
  data = (void*)malloc(sizeof(svm_data_t));
  data->num_points = np;
  data->num_dimensions = nd;

  data->training_set = (void*)malloc(sizeof(double)*(NUM_DIMENSIONS+1));
  data->descision_set = (void*)malloc(sizeof(double)*(NUM_DIMENSIONS+1));

  printf("test_data->num_dimensions: %d\n", data->num_dimensions);
}

This prints:

test_data->num_dimensions: 3
test_data->num_dimensions: 3
in init: test_data->num_dimensions: 0
in init: check_data->num_dimensions: 0

But I want it to print

test_data->num_dimensions: 3
test_data->num_dimensions: 3
in init: test_data->num_dimensions: 3
in init: check_data->num_dimensions: 3

I can't understand what I'm doing wrong. Please help.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Denis P
  • 15
  • 4
  • I think the second malloc (for array init) is overwriting the first one (for struct init). Better use some kind of constructor. – lucasg Apr 29 '13 at 15:17
  • 1
    The `data` parameter to `init_svm_data` is never actually used -- the third line of the function body simply overwrites the pointer that was passed in, having no side effects other than leaking memory. – cdhowie Apr 29 '13 at 15:18
  • 2
    possible duplicate of [How to pass over a pointer and allocate space on heap](http://stackoverflow.com/questions/7927036/how-to-pass-over-a-pointer-and-allocate-space-on-heap) – Kiril Kirov Apr 29 '13 at 15:18

2 Answers2

4
data = (void*)malloc(sizeof(svm_data_t));

Here you are assigning the return value of malloc() to the data variable, so it shadows (overwrites) its original value (i. e. the address of the struct you passed in). You don't need this line at all, since you already pass in the address of a (stack-)allocated struct.

Remarks:

  1. You should not cast the return value of malloc().

  2. On the line you don't need, you actually loose the pointer to the malloc()ated memory when the function returns, so you even leak memory.

Community
  • 1
  • 1
0

Lets look at a few lines here:

init_data(&test_data);

Here you call init_data (I assume it's actually init_svm_data) with a pointer to what I assume is a normal structure, i.e. you globally declare svm_data_t test_data;.

Then in init_svm_data you do:

data = (void*)malloc(sizeof(svm_data_t));

Here you overwrite the pointer you pass into the function.

This leads to a couple of problems: The first is that the pointer in init_svm_data is local to the function, so changes (assignments) to it will not be noticable outside of init_svm_data. The second problem is that you, if I guessed correctly with the declaration of test_data, do not need to allocate this memory as it's already done by the compiler.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621