![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
how to use malloc() and free()
Hi all,
I am a beginner to C. I got this problem about malloc() and free(). How can i read a line from a file, put it into an array by using malloc, do something to it, free the array, and then read next line? e.g. sample.txt: hello how are you nice to meet you bye bye read and put "hello how are you" into an array by using malloc, print it out, and then free the memory, then read "nice to meet you", and so on... Thanks.... =) |
|
|
|
|
|
#2 |
|
Professional Programmer
|
whenever you create something (except for a primitive type, int, char, float, double) you need to allocate space for it in RAM. malloc takes as argument the size of what you're going to allocate, and returns a pointer that needs to be typecast to what you allocated for.
ex: char *word; //we're gonna store a 5 letter word word = (char *)malloc(sizeof(char) * 5); free(word) will just tell the memory that the space allocated for word can be reused. So after you call free(word) you won't be able to access the word variable again. reading from the file can be done using fopen, fscanf, and fclose. There are many examples of that online, but i'll give you a quick example FILE *fp; //file pointer
char *line = (char *)malloc(255); a char is 1 byte so for a char you can just allocate 255 bytes
fp = fopen("file.txt", "r"); open file for reading
fscanf(fp, "%s\n", &word); read a line from the file untill a newline character comes along
fclose(fp); //close the file
printf("%s\n", word); //print the word read
free(word); //free the spacehope this helps, there's a lof ot examples on google if you're stuck, and feel free to post your code here if you have trouble. good luck Dizz |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
Hi Dizzutch,
Thank you for replying... Actually i am writing codes to read a line from a file, then use fork() and execv() to create a new process, waitpid for the process to die, then read next line. e.g. sample.txt /usr/bin/w -s /bin/ls -al so i need to do char *line[] = {"/usr/bin/w", "-s", 0};
execv("/usr/bin/w", line);there are up to 50 parameters need to be passed, and each 50 characters. so how can i read each line and put the parameters into a allocated array, execute it, then free the array? Thank you.. =) |
|
|
|
|
|
#4 |
|
Professional Programmer
|
ah, i thought you had no idea how to use malloc and free..
![]() you can allocate 50*50 bytes, and just access it as an array char *array = (char *)malloc(sizeof(char) * (50*50)); array[0] = foo; array[1] = bar; Good luck, hope you catch my drift. Dizz |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
Thanks Dizzutch...
![]() |
|
|
|
|
|
#6 |
|
Professional Programmer
|
np, hope it helps.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|