free() a char* once it's been cycled through.

If I _strdup() a char*, and they start cycling through it's pointers, how do I free() it at the end?

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
char *orig = _strdup(my.GetData("creatureLoads")); // Pulling data from a MySQL table.

while (*orig)
   {
      switch (*orig)
         {
            case '[':
               howMany = 0;
               break;
            case 'c':
               *orig++;
               creatureID = atoi(orig);
               break;
            case 'r':
               *orig++;
               roomID = atoi(orig);
               break;
            case 'x':
               *orig++;
               howMany = atoi(orig);
               break;
         }
	*orig++;
   }
   
   free(orig); // Here is where the program crashes. 


Program crashes when I try to free() orig. I'm assuming since it was _strdup()'d into memory, it needs to be free()'d. If I don't cycle thru it's pointers, it free's fine.

Thanks in advance for the advise.
That's because you have incremented the pointer so you can't expect free to be able to understand what to do with this random pointer you have given it.

Just save a copy of the pointer to free later if you need it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
char *orig = _strdup(my.GetData("creatureLoads")); // Pulling data from a MySQL table.
char *orig_ptr = NULL;

orig_ptr = &*orig;

while (*orig)
   {
      switch (*orig)
         {
            case '[':
               howMany = 0;
               break;
            case 'c':
               *orig++;
               creatureID = atoi(orig);
               break;
            case 'r':
               *orig++;
               roomID = atoi(orig);
               break;
            case 'x':
               *orig++;
               howMany = atoi(orig);
               break;
         }
	*orig++;
   }

orig = &*orig_ptr;
free(orig);


This above code works, is it correctly free()ing what I _strdup()'d? I think it is.
It looks correct, but you can throw away those stacked &* since that just dereferences the pointer then takes its address (effectively doing nothing). You can also just free(orig_ptr) at the end instead of assigning it to a different variable first.
Yes, thank you. That's much more efficient. Tested successfully.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
char *orig = _strdup(my.GetData("creatureLoads")); // Pulling data from a MySQL table.
char *orig_ptr = orig;

while (*orig)
   {
      switch (*orig)
         {
            case '[':
               howMany = 0;
               break;
            case 'c':
               *orig++;
               creatureID = atoi(orig);
               break;
            case 'r':
               *orig++;
               roomID = atoi(orig);
               break;
            case 'x':
               *orig++;
               howMany = atoi(orig);
               break;
         }
	*orig++;
   }

free(orig_ptr);
orig_ptr = NULL;
orig = NULL;
Topic archived. No new replies allowed.