-2

So I am trying to assign a string value to an object in an array.

The object code is:

typedef struct BiTreeData_ {
    char* word;
    int start_word_count;
    int end_word_count;
    int start_ranking;
    int end_ranking;
} BiTreeData;

In my main I make an array of pointers to BiTreeData using:

BiTreeData **dataarray;
dataarray=(BiTreeData**)malloc(sizeof(BiTreeData*)*maintree.size);

I try to assign a value to one of the variables within the struct with:

int z = dataarray[i]->start_word_count;

The program crashes. Any help would be appreciated. Thank you.

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • Please, don't cast the return value of `malloc()`! –  Nov 09 '12 at 20:39
  • You are allocating an array of pointers. The pointers in the array are still uninitialized and point to invalid memory locations. You must allocate a new one-dimensional array for each item in the `dataarray` array, or use *only one* `*` if you don't need a two-dimensional array. – Niklas R Nov 09 '12 at 20:40
  • @H2CO3 I've never heard that statement. Could pleae give arguments or point me to an article or alike? – Niklas R Nov 09 '12 at 20:41
  • @NiklasR [Here it is.](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) –  Nov 09 '12 at 20:47

4 Answers4

2

Your code here

BiTreeData **dataarray;

dataarray=(BiTreeData*)malloc(sizeof(BiTreeData)*maintree.size);

allocates the memory space for an array of BitTreeData* but you are not allocating any space for the BitTreeData objects themselves. You just have a dynamic array of pointers that point to uninitialised memory.

It would probably be far easier if you just create an array of BitTreeData rather than of pointers. You could achieve this by using

 BiTreeData *dataarray;
 dataarray=(BiTreeData*)malloc(sizeof(BiTreeData)*maintree.size);

This just creates an array of BitTreeData - you can then access each item in the array like you would any other array (remembering now that the array is of BitTreeData not BitTreeData*)

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
0

Is there a reason you are defining

BiTreeData **dataarray;

To get this to work the way you want I think you need to define it as:

BiTreeData *dataarray;

This will allow you to malloc an array of BiTreeData structs.

To create the array of structs:

BiTreeData *dataarray;

To allocate memory on the heap for the array of structs:

dataarray = malloc(sizeof(BiTreeData)*maintree.size);

To set the value of a struct element in your array:

int z = 5;
dataarray[i].start_word_count = z;

To extract a value from your struct array:

int z = dataarray[i].start_word_count;
dinkelk
  • 2,676
  • 3
  • 26
  • 46
0

you have to allocate memory for each element in the array of dataarray

dataarray[i] = (BiTreeData *) malloc ((sizeof(BiTreeData)

the followingline code you put

array=(BiTreeData**)malloc(sizeof(BiTreeData*)*maintree.size);

with this code, you have allocated array of pointers. but theses pointers are not pointed to any content yet. so you have to allocate memory for each pointer in the array

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
-1

Since you're allocating an array of pointers, the size of element should be the size of BiTreeData *, not BiTreeData:

dataarray= (BiTreeData **)malloc(sizeof(BiTreeData *) * maintree.size);

EDIT
The question has since been edited to correct this mistake.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • No, this is not the problem. –  Nov 09 '12 at 20:39
  • @H2CO3 To the downvoter: I was responding to the question which at the time contained the line `(BiTreeData*)malloc(sizeof(BiTreeData)*maintree.size)` which is an incorrect way to allocate an array of pointers to `BiTreeData`. – user4815162342 Nov 09 '12 at 20:45
  • @user48XXXX Yes, it's true, but 1. it makes things even worse (because OP uses wrong code, so allocating in fact less memory will cause another problem), 2. this doesn't solve the problem at all. –  Nov 09 '12 at 20:47
  • @H2CO3 It does solve the problem of incorrect allocation of the array of pointers to `BiTreeData`. There might be other problems with the code—such as failing to allocate the individual structs—but we can't know that, since the remainder of the code is missing. – user4815162342 Nov 09 '12 at 20:48