Basic Pointer Question...

S

shan_rish

Hi CLCers,
I coded a function to allocate memory and i am passing a pointer to the
function. The code is compiling but throws error and closes while
executing. The program is as below:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
void mem_fun(int *i);
mem_fun(p);
printf("%d\n",*p);
getch();
return 0;
}

void mem_fun(int *i)
{

printf("%u\n",i);
i=malloc(sizeof(int));
*i=10;
printf("%d\n",*i);
}

Please help in understanding the problem. Advanced thanks.
Cheers
Shan
 
B

bwaichu

Hi CLCers,
I coded a function to allocate memory and i am passing a pointer to the
function. The code is compiling but throws error and closes while
executing. The program is as below:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
void mem_fun(int *i);
mem_fun(p);
printf("%d\n",*p);
getch();
return 0;
}

void mem_fun(int *i)
{

printf("%u\n",i);
i=malloc(sizeof(int));
*i=10;
printf("%d\n",*i);
}

Please help in understanding the problem. Advanced thanks.
Cheers
Shan

You are not using pointers correctly. Here's a good website with an
easy to understand explanation of pointers:

http://cslibrary.stanford.edu/

I removed your first printf from the mem_fun() since I had no idea what
you were trying to do.

Here's your code corrected:

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

void mem_fun(int **);

int
main() {

int *p;
mem_fun(&p);
(void)printf("%d\n",*p);
exit(0);
}

void
mem_fun(int **i)
{

*i = malloc(sizeof(int));
if(*i == NULL)
return;
**i = 10;
(void)printf("%d\n",**i);
return;
}
 
M

Michael Mair

Hi CLCers,
I coded a function to allocate memory and i am passing a pointer to the
function. The code is compiling but throws error and closes while
executing. The program is as below:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
void mem_fun(int *i);

Do not hide your function declarations within other functions.
mem_fun(p);

Let us recapitulate: C passes arguments by value.
You have given no value to p, so making a copy of p for mem_fun
invokes undefined behaviour. From now on, anything can happen,
including your harddisk being formatted.
If you want to change something using a function, you have
to pass its _address_, not the object itself (which will only
copied but not changed).
Note: The address is also passed by value, but this value is
sufficient to do something.
printf("%d\n",*p);
getch();

This function is not defined in standard C.
return 0;
}

void mem_fun(int *i)
{

printf("%u\n",i);

The format specifier for pointers is %p.
If pointers and integers have different sizes, the above may
go wrong.
i=malloc(sizeof(int));

Better:
i = malloc(sizeof *i);
This works even if you change the type to i.

You forgot to check whether malloc() succeeded.
*i=10;
printf("%d\n",*i);

As you work on a copy of a pointer value which expires at the
end of the function, you have produced a memory leak.
}

Please help in understanding the problem. Advanced thanks.

Better:

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

void mem_fun (int **i);

int main (void)
{
int *p = 0;
mem_fun(&p);
if (p)
printf("%p : %d\n", p, *p);
free(p);
getch();
return 0;
}

void mem_fun (int **i)
{
*i = malloc(sizeof **i);
if (*i)
**i = 10;
else {
fprintf(stderr, "mem alloc trouble\n");
exit(EXIT_FAILURE); /* Replace by _real_ error
** handling whenever possible */
}
}

As you see, I check _twice_ whether malloc() succeeded to be
on the safe side. This is because mem_fun() does not directly
tell me whether it was successful. You may consider returning
*i/int * to signal success or failure.
In addition, as malloc() is hidden within a function, free()
can be easily forgotten.
It is usually better to malloc() on the same "level" as you free()
or to provide companion functions to be called from the same
level performing malloc() and free().


Cheers
Michael
 
S

shan_rish

Hi Guys,
Thanks for your reply. Now i understand what the problem is. When
coding with pointers, i stumble a lot, even though i know that values
are passed to C functions. Thanks again for your time for clearing my
doubt.
Cheers
Shan
 
K

Krishanu Debnath

Michael Mair wrote:

Better:

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

void mem_fun (int **i);

int main (void)
{
int *p = 0;
mem_fun(&p);
if (p)
printf("%p : %d\n", p, *p);

typo? printf("%p : %d\n",(void *) p, *p);

Krishanu
 
K

kernelxu

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

void mem_fun (int **i);

int main (void)
{
int *p = 0;
hi,Michael
maybe "int *p = (void *)0; " is better than "int *p = 0; ".
and, I always use NULL to initilize a pointer. do you think it's a good
style?
Is the macro "NULL" a standard C's definition?
any word would be appreciated.
mem_fun(&p);
if (p)
printf("%p : %d\n", p, *p);
free(p);
getch();
return 0;



}
 
P

Peter Pichler

maybe "int *p = (void *)0; " is better than "int *p = 0; ".
and, I always use NULL to initilize a pointer. do you think it's a good
style?
Is the macro "NULL" a standard C's definition?
any word would be appreciated.

Look up Q 5.4 in the FAQ. I would also recommend to read the whole
section 5, which deals with NULL pointers. The FAQ can be found at
http://www.eskimo.com/~scs/C-faq/top.html. It used to be anyway; I
couldn't connect to that site today :(

Peter
 
R

ranjeet.gupta

Here's your code corrected:

I dont think so !!!
#include <stdio.h>
#include <stdlib.h>

void mem_fun(int **);

int
main() {

int *p;
mem_fun(&p);
(void)printf("%d\n",*p);
exit(0);
}

void
mem_fun(int **i)
{

*i = malloc(sizeof(int));
if(*i == NULL)
return;
**i = 10;
(void)printf("%d\n",**i);
return;
what you are returning and why ? Check your declaration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top