tags:

views:

162

answers:

3

when i debug the following code, the string copy copies fine but as soon as the loop exits the char**(parent_var_names) is pointing NULL/0xfdfdfddf I am puzzled?!

parent_var_names=(const char**)calloc(net->nodes[x]->num_parents,sizeof(const char*));
for(int i(1);i<net->nodes[x]->num_parents;++i)
{
  parent_var_names[i]=(const char*)malloc(strlen(rhs_arr[net->nodes[x]->markov_parents[i]]));
  strncpy((char*)parent_var_names[i],(char*)rhs_arr[net->nodes[x]->markov_parents[i]],strlen(rhs_arr[net->nodes[x]->markov_parents[i]]));
}
+3  A: 

int i(1) in your for loop init should probably be int i(0).

Otherwise you're never setting parent_var_names[0] to anything other than the 0 that calloc() initializes it to.

Just for completeness (since this is mentioned a couple times in the comments), you're not taking into account the '\0' terminator for the strings you're copying. Since you're not copying the terminator in your strncpy() calls, you're not overflowing the buffers allocated, but your results aren't properly-terminated strings. It's possible that this is your intent, but that would be unusual. If it is intended, throw a comment in there...

Michael Burr
+1  A: 

If your parent_var_names aren't NULL terminated it's probably because you are using strlen when you allocate space for the strings. This fails to create space for the string and the NULL terminator. Try strlen()+1 instead.

You should probably be using std::string anyway...

Blastfurnace
ok, i took a break and had a look again with fresh eyes. the int i(1) was the obvious error. I copy and pasted further up the file for that loop(now i know why thats a no-no!). sometimes when youve been staring at the screen for hours you just can't see whats blatantly obvious staring you in the face. cheers anyway all.
jmgunn87
how do i rate people on here?
jmgunn87
oh and blastfurnace.. on the PM you sent me.. I take it mistakes are never made by you? I spose you probably dont work hard enough to have had that happen to you. learn some humility. If i didnt know arrays in most programming languages are 0-indexed i dont think i'd be getting much done and believe me i do.
jmgunn87
It was nothing personal. You just seemed to be getting upset with Steve Jessop and others that were offering advice on the code you posted. That the code had a fairly simple mistake was at odds with your assertion of expertise with C. I know we all make mistakes and I'm glad you found an answer to your problem. You should accept either Jim Brissom's or Michael Burr's answer if they were helpful. Have a nice day.
Blastfurnace
no problem. thanks for the advice.
jmgunn87
+3  A: 

Placing guard bytes (i.e. 0xFDFDFDFD) around an allocated region of memory is a feature of the (Microsoft) debug heap. Seeing that you encounter this value either means you are overwriting memory somewhere, or you are looking at the value of parent_var_names[0] without actually writing anything in there (i.e. take a close look at the value you initialize your loop variable iwith).

Furthermore, your code could be simplified to:

#include <string>
/* ... */

int ii = 0;
int parent_count = net->nodes[x]->num_parents;
char** parent_var_names = calloc(parent_count, sizeof(char*));

for(; ii < parent_count; ++ii)
{
    parent_var_names[ii] = strdup(rhs_arr[net->nodes[x]->markov_parents[ii]]);
}

Also make sure your markov_parents are definitely zero-terminated. Btw., in response to your comment that you want "C and C++ compatibility": Your code is not valid C code, so ...

Jim Brissom