1,105,578 Community Members

Free Malloc

Member Avatar
efronefron
Light Poster
36 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

The problem is I use malloc in the last function(toString()).
I just wanna know if there is anyway to free it? or that I dont have to?
Thanks

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

/* Function prototypes */
void process(char input);
void update(char words[][51], int position);
char * toString(char character);

/* the global variables */
char words[100][51];	//the array of the words inside the text
int lastWordPos = 0;	//position of the last word in array words

/* Main Function */
int main()
{
	char character = getchar();	//get a character from stdin
	char tempWord[51] = "";		//temporary array for storing a word
	char tempDigit[51] = "";		//temporary array for storing a number
	
	while(character != '0')		//as long as the character from stdin is not 0, keep looping
	{
		//if the character is an alphabet
		if(isalpha(character))
		{
			while( isalpha(character) )
			{
				strcat(tempWord, toString(character));		//get the whole word
				character = getchar(); 
			}
			strcpy(words[lastWordPos], tempWord);			//add the word to the word list
			printf("%s", tempWord);						//print the word
			strcpy(tempWord, "");						//reset tempWord, so that it can be used again at the next loops, if needed
			lastWordPos++;								//increment the position of the word list
		}
		
		//if the character is a number
		if(isdigit(character))
		{
			while( isdigit(character) )
			{
				strcat(tempDigit, toString(character));		//get the whole number
				character = getchar(); 
			}
			int position = lastWordPos - atoi(tempDigit);	//get the position of the word that will replace the number
			printf("%s", words[position]);				//print the word
			strcpy(tempDigit, "");						//reset tempDigit, so that it can be used again at the next loops, if needed
			update(words, position);						//update the list of words
		}
		
		//if the character is a punctuation of a white space, print it
		if(ispunct(character) || isspace(character))	printf("%c", character);
		
		//get next character from stdin
		character = getchar();
	}
	
	return 0;
}
/* update : take an array of characters and a position of the word that wants to be put at front of the list
 * Input: a char array and position of the word
 * Output: the word at the position of the array is put on front, returns void
 */
void update(char words[][51], int position)
{
	int i;
	char temp[51];
	for(i=position; i<lastWordPos-1; i++)		//will keep swapping to move the word from position to the front of the array
	{
		strcpy(temp, words[i]);
		strcpy(words[i], words[i+1]);
		strcpy(words[i+1], temp);
	}
}
/* toString: take a character and make it into a type string( array of chars)
 * Input: take a character
 * Output: an pointer to an array of only 1 character long, that had been declared globally before
 */
char * toString(char character)
{ 
	char *x =	malloc(2 * sizeof(*x));
	x[0] = character;	//put a character inside the first index
	return x;			//return the address of the array of only one character
}
Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

Any memory that you allocate should be also freed and since your returning a pointer to the allocated memory, you can use that to free it.

Also, why all the comments its really distracting.

Member Avatar
efronefron
Light Poster
36 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Any memory that you allocate should be also freed and since your returning a pointer to the allocated memory, you can use that to free it.

Also, why all the comments its really distracting.

sorry its an assignment from my university so I have to write all that comments. I hate it too so please bear with me :/

btw, I dont think that will work in this case. I dont know how to explain. but can you give me an example?

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

Also, why all the comments its really distracting.

Because good programmers comment their code so others can follow it.

sorry its an assignment from my university so I have to write all that comments. I hate it too so please bear with me :/

Don't apologize. Keep it up. Comments are critical if you want to make programming a profession.

Member Avatar
navedalam
Newbie Poster
21 posts since Jun 2010
Reputation Points: -6 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

1. Comment are good.Keep it up.
2. I agree any memory that you allocate should be freed.
3. You can always use the pointer that you are returning to free the memory.
4. Instead of calling malloc several time toString is called, allocate it in main once
and send it pointer. That would be more efficient.

Member Avatar
efronefron
Light Poster
36 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

3. You can always use the pointer that you are returning to free the memory.

Can you give me an example of how I can do that?

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

My point on the comments refers to commenting something like this

char character = getchar(); //get a character from stdin

getchar() is a function from the standard library. Its well documented, so I consider commenting its functionality redundant. That said, if your Prof wants comments then give him/her comments.

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

And your example

#include <stdio.h>
#include <stdlib.h>

char * toString(char character)
{ 
	char *x =	malloc(2 * sizeof(char));
	x[0] = character;
	x[1] = '\0';	
	return x;			
}

int main(int argc, char**argv)
{
	int ca;
	char *ans = NULL;

	fputs("Enter a character->", stdout);
	ca = fgetc(stdin);

	ans = toString(ca);
	
	fprintf(stdout, "%s\n", ans);

	free(ans);
	ans = NULL;
	return 0;
}
Member Avatar
navedalam
Newbie Poster
21 posts since Jun 2010
Reputation Points: -6 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
-1
 

i totally agree with gerard4143...
@gerard4143-----thanks for the example

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

My point on the comments refers to commenting something like this

char character = getchar(); //get a character from stdin

getchar() is a function from the standard library. Its well documented, so I consider commenting its functionality redundant. That said, if your Prof wants comments then give him/her comments.

That may have been your point, but you included

/* Function prototypes */
void process(char input);
void update(char words[][51], int position);
char * toString(char character);

/* the global variables */
char words[100][51];	//the array of the words inside the text
int lastWordPos = 0;	//position of the last word in array words

char tempWord[51] = "";	//temporary array for storing a word
char tempDigit[51] = "";//temporary array for storing a number
	
while(character != '0')	//as long as the character from stdin is not 0, keep looping
strcpy(words[lastWordPos], tempWord);	//add the word to the word list
strcpy(tempWord, "");			//reset tempWord, so that it can be used again at the next loops, if needed
lastWordPos++;				//increment the position of the word list

in your remark. If you didn't mean these, you should have been more explicit. As a veteran programmer, I understood your remark as "don't use comments". How will noobs interpret it?

Question Answered as of 3 Years Ago by gerard4143, WaltP and navedalam
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: