Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 16th, 2005, 9:46 AM   #1
FarAway
Newbie
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 FarAway is on a distinguished road
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.... =)
FarAway is offline   Reply With Quote
Old Feb 16th, 2005, 10:09 AM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
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);
the (char *) tells the program that whatever space malloc returns is going to be used by a char, and sizeof(char) * 5 tells malloc how many bytes to allocate in memory. In your case you might just want to allocate an arbitrary amount, examply 255 bytes. So you won't run out.
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 space

hope 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
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Feb 16th, 2005, 10:27 AM   #3
FarAway
Newbie
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 FarAway is on a distinguished road
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.. =)
FarAway is offline   Reply With Quote
Old Feb 16th, 2005, 10:39 AM   #4
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
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));
since this is dynamically allocated you can access it as
array[0] = foo;
array[1] = bar;
I'm assuming that you're not making up the 50*50 and do have limits. if you don't than i'd just allocate a large amount per line, and read in per line. determine the size, and realocate a new bit of memory for the size of the string you just read in.
Good luck, hope you catch my drift.

Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Feb 16th, 2005, 10:50 AM   #5
FarAway
Newbie
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 FarAway is on a distinguished road
Thanks Dizzutch...

FarAway is offline   Reply With Quote
Old Feb 16th, 2005, 11:08 AM   #6
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
np, hope it helps.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:13 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC