2

I have some thing like this

char *temp, xyz;
temp = (char *)malloc(sizeof(somestring));

xyz = (char *) malloc(sizeof(temp));
xyz= strrchar(temp, "_"); // temp does not contain "_", but need to check for error validation

Now If I try to do free(temp); its crashing;

any idea, I am a beginer.

sam
  • 197
  • 2
  • 17

2 Answers2

14

The first problem is that xyz is not being declared as a char *, only a single char.

Given the following variable declaration:

char *temp, xyz;
  • temp is a pointer to a char.
  • xyz is only a char.

The * doesn't apply to both variables. To make both variables pointers to char, use:

char *temp, *xyz;

Next: you're confusing the usage of sizeof with strlen. sizeof gives you the size occupied by a type or a variable of that type. So if temp is a char *, then sizeof(temp) will be the size of a pointer, not the length of a C string. To get C string lengths - e.g. to get the buffer size that you'll need to copy a given C string - use strlen.

Finally, unless you've omitted some significant code there's another problem: simply using malloc doesn't do anything with the newly allocated memory, so after:

temp = (char *)malloc(sizeof(somestring));

temp points to an uninitialized bit of memory (which is, as mentioned, probably smaller than you think it is due to using sizeof rather than strlen). Then you go on to use temp as if it's a valid C string, with strchr. This will likely access memory that doesn't belong to you, which invokes undefined behavior.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • Actually xyz is of type char *, that was a typo (my bad). I have changes sizeoff to strlen in place now. but still I have the same problem, BTW after malloc I do have strcpy(temp, something); If I do debugging on this "temp" has the string I am expecting. Now the code block looks like below char *temp, *xyz; temp = (char *)malloc(strlen(somestring)); strcpy(temp, something); // value in something is copied to temp xyz = (char *) malloc(strlen(temp)); xyz= strrchar(temp, "_"); // temp does not contain "_", but need to check for error validation – sam Aug 20 '12 at 13:57
  • if(temp !=NULL) free(temp); if(something != NULL) free(something); this is what I have at the end, where I am seeing th crash – sam Aug 20 '12 at 14:02
0

I figured it out, correct me if I am wrong, xyz is just pointing to the location where it has the "_" in the string temp. As soon as I free up the temp, that location got freed up. but xyz is still pointing to that location, which is already freed. That is the reason I had crash on freeing xyz.

sam
  • 197
  • 2
  • 17
  • 1
    yes that would lead to problems. When you use `strchr` you only need a `char *` pointer to reference parts of the string, by pointing to memoru _inside_ the already-allocated string. This pointer, `xyz` in your case, **doesn't** need to have memory allocated for it, and doesn't need to be free'd. You'll free the original string - **only** if it's dynamically allocated with `malloc` - at some later point, assuming that you correctly kept a reference to it. – pb2q Aug 20 '12 at 18:50