Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

char * gaining extra characters 1

Status
Not open for further replies.
May 13, 2002
75
GB
Hello,

you'll have to excuse how i ask this question because i am not a C programmer (sorry!) but i am writing a small program using a set of third party libraries to do some scientific calculations.

I can post some code if you like, but basically the problem is that i have what i would call a string..

char *smiles;

and i have some code that takes some data from an external database call (ignore the dt_stringvalue function)
Code:
str = dt_stringvalue(&lens, sob); /*str is what the user gave us*/
This string is actually two strings separated by a pipe so i split them up...
Code:
char *search_char = "|";
tsmirks = strstr(str, search_char);
work out where the pipe is..
Code:
index = str - tsmirks;
get the first string up to the pipe..
Code:
smiles = (char *)calloc(index, sizeof(char));
strncat(smiles, str, index);
At this point everything is ok, i can output my string to the console at it looks good....
Code:
fprintf(stderr, "SMILES ok: %s \n", smiles);
Now, here's my problem. Only for certain strings, when i call third party functions that do not involve the smiles string, then output the smiles string, it has an extra character appended (sometimes a "9" sometimes a "n" with a squiggle over it, sometimes something different) i.e.

Code:
 if (NULL_OB == (xform = dt_smirkin(strlen(smirks), smirks))) {
	 fprintf(stderr, "SMIRKS bad: %s \n", smirks);


   } else {
	 fprintf(stderr, "SMIRKS loaded into transform ok: %s \n", smirks);
  }
where smirks is a different string, then i output the smiles string it has changed. Is it something to do with memory allocation ? pointers ?

For example, when i use..

Nc1cccc(c1)S(=O)(=O)O.c1ccccc1C(=O)O|([*;$([NX3][C,c]):1][H]).([*;$([CX3](=O)[OX2;H1]):2][*;$([OH]),$(OC(=O)C):99])>>[*:1][*:2].[*:99][H]

everything is fine, but when i use..

CN1C(=O)CN=C(c2ccccc2)-c3cc(Cl)ccc13|[*;$([CX3](=O)[NX3]):1](=O)>>[*:1]([H])[H]

The smiles string (characters up to the pipe) gets extra characters appended after i call other functions.

I'm sorry if i haven't explained myself too well, i'm more than happy to supply any further information, thanks for any tips you can give me.



Alistair [monkey]
 
I'm trying to make sense of your code snippets but what does stand out to me is that you seem to work out the length of the substring up to the pipe character and then call that index.

You then calloc a memory area of that size and copy the string into this area. If this is a correct understanding the string that you have just created is not NULL terminated.

Any attempt to print the contents of the string will print all the characters from the start of your string until a NULL byte is found. This will be at some random place in memory depending on external factors.

You should actually allocate an area one byte more via calloc, in that way the final byte will be a NULL byte.

Personally I would have used strtok() to split the string in place by replacing the pipe with a NULL character in-situ but that's up to you

Cheers
 
Yes, thanks, that works a treat. Sorry my code snippets were hard to decipher!

Thanks for your help

Alistair [monkey]
 
Hi,

Smells like memory leakage!

Try changing ..

smiles = (char *)calloc(index, sizeof(char));

to ..

smiles = (char*)malloc(sizeof(128));

or

smiles = (char*)calloc(char,sizeof(128));

Regards,
j@yb0t[afro]

"Always know what you say, but don't always say what you know!"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top