Re: A pointer as a porameter of the function
- From: "santosh" <santosh.k83@xxxxxxxxx>
- Date: 15 Nov 2006 06:33:47 -0800
svata wrote:
So I rewrote it:
More specific comments.
/* function to check malloc */
int malloc_check(char *in){
Again make the parameter const qualified, as the code in the function
need not modify it.
if (in == NULL ) {
printf("Failed to allocate memory on line %d", __LINE__);
This will always print the line number of the line the above printf()
is on, _not_, as you probably wanted, the line number of the failing
malloc() call.
return 1;
}
else {
return 0;
}
}
This whole function is a very poor design choice. It would be better,
atleast in small projects, to wrap malloc() and it's associated
debugging and error-handling code within a single function, say,
my_malloc().
void read_name( char *p_buf, const int size) {
char *p_nl;
int ch;
int malloc_check(char *in);
Although legal, don't place declarations within functions. Place them
outside of any function and before that function is called in the
source file. Generally, it's good practise to declare functions and
other objects right after the #include for header files and macro
definitions. In larger and multi-module projects, it would be better to
encapsulate them into a common header file and include that.
printf("Enter the description: ");This is not necessary.
fflush(stdout);
/* if there is some input */
if (fgets(p_buf, (size -1), stdin) != NULL){
/* malloc memory for p_nl ( one char ) */
p_nl = malloc(1); /* we need it to get rid of new line */
ch = malloc_check(p_nl); /* check malloc status */
if ( ch != 1 ){/* ie malloc returned success */
if (( p_nl = strchr(p_buf, '\n')) != NULL ){
*p_nl = '\0'; /* get rid of new line */
This will cause a memory leak of 1 byte due to the unnecessary
allocation previously.
/* the only problem I have here, of at least I'm aware of, is that I
can't figure out how to correctly handle EOF in the context of thsi
function.
*/
and then in the main()
This is why you should cut and paste your exact and complete code.
Because you've only pasted a incomplete portion of main(), we will not
be able to compile it here, without encountering possibly spurious
errors.
char *p_buf;
char *p_input;
void read_name(char *, const int);
int malloc_check(char *in);
Again place the above function declarations outside of any functions
and preferably at the top of the source file. Also the declaration of
malloc_check above conflicts with it's definition previously.
int ch;
p_buf = malloc(10); /* malloc memory for p_buf */
ch = malloc_check(p_buf); /* check if it succeded */
if ( ch != 1 ) {/* if yes, do... */
read_name(p_buf, 10);
p_input = malloc(10);/* malloc memory for p_input */
if ( ch != 1 ) {/* if the case of success... */
strcpy(p_input, p_buf);/* copy content of p_buf to p_item */
free(p_buf);/* free memory*/
}
printf("%s", p_input);
}
All this is horribly convolvuted and wrong. I suggest that you work
your way up from simpler programs than this one. Also your
understanding of pointers and dynamic memory are very shaky. Please go
through a good textbook.
Hope, this code gets better response from you :)
You're implementing before carefully thinking out your program on pen
and paper and before understanding many of the fundamentals of C. As
such it would be better to scrap this program and start with a simpler
one. Use static arrays to begin with, progressing to dynamic memory
when you've got all other issues ironed out.
Don't be discouraged by my comments. Most newbies will have some
trouble early on. Practise and re-reading will usually improve matters
dramatically.
.
- Follow-Ups:
- Re: A pointer as a porameter of the function
- From: svata
- Re: A pointer as a porameter of the function
- References:
- A pointer as a porameter of the function
- From: svata
- Re: A pointer as a porameter of the function
- From: santosh
- Re: A pointer as a porameter of the function
- From: svata
- A pointer as a porameter of the function
- Prev by Date: Re: Please Help ----------Free Downloadable Ebooks for C & C++ Language needed
- Next by Date: Re: regarding << and >> operators
- Previous by thread: Re: A pointer as a porameter of the function
- Next by thread: Re: A pointer as a porameter of the function
- Index(es):
Relevant Pages
|