We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,725 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Free Malloc

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
}
4
Contributors
9
Replies
17 Hours
Discussion Span
2 Years Ago
Last Updated
10
Views
Question
Answered
efronefron
Light Poster
35 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 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?

efronefron
Light Poster
35 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

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.

navedalam
Newbie Poster
21 posts since Jun 2010
Reputation Points: 4
Solved Threads: 3
Skill Endorsements: 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?

efronefron
Light Poster
35 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 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;
}
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0

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

navedalam
Newbie Poster
21 posts since Jun 2010
Reputation Points: 4
Solved Threads: 3
Skill Endorsements: 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?

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36
Question Answered as of 2 Years Ago by gerard4143, WaltP and navedalam

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0998 seconds using 2.75MB