Hi gcssolve,
> how would you free the memory originally assigned to the pointer?
Keep a copy of the original pointer, so later, you can free it.
David Maisonave (Axter)
Cheers!
Main Topics
Browse All TopicsIf you move the char-pointer as done in the code below how would you free the memory originally assigned to the pointer? The call to the free-function makes the program crash.
regards,
Thorbjørn
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fhin;
char *LinePtr;
char line[1024];
while (1) {
if (!(fhin = fopen("/tmp/testfile", "r"))) {
printf("error");
exit(0);
}
while (fgets(line, sizeof(line), fhin) != NULL) {
printf("got line %s\n", line);
LinePtr = (char *) calloc(sizeof(line), sizeof(char));
strcpy(LinePtr, line);
*LinePtr++;
free(LinePtr);
}
fclose(fhin);
sleep(5);
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
*LinePtr++;
is hard to use, since (i do not know the right one) it could be:
*(LinePtr++);
which would change the address making LinePtr maybe invalid,
or
(*LinePtr)++;
which would just make the first char of LinePtr increase the value by 1.
The biggest problem is see is that you use sizeof(line).
I would use something like:
#define LINELENGTH 1024
...
char line[LINELENGTH];
...
fgets(line, LINELENGTH, fhin)
...
LinePtr = (char *) calloc(LINELENGTH, sizeof(char));
...
>>nrip... test it before you to say it... LinePtr will be 0x0 after free, so *LinePtr++; will segfault!
LinePtr will not be 0x0 (NULL) because free was called. How will free change the value of LinePtr when it is passed by value.
Going back to the original question: You can't do it. You will have to save a original copy of the pointer.
Or you will have to implement a smarter free() which can find out to which allocated block does the pointer belong too and then free that block!!!
Business Accounts
Answer for Membership
by: grg99Posted on 2007-01-02 at 09:14:33ID: 18229059
why do you have a LinePtr++ ?? You don't need it,and it makes it impossible to free the pointer.
btw you probably wanted to malloc the length of the string, not the size of the array. use strlen()+1 instead of sizeof()