Can't figure this out

R

ritesh

Hi,

I'm unable to figure out why initializing a pointer dosen't work this
way -

void func(int * x)
{
x = (int*)(malloc(sizeof(int*)));
*x = 2;
}

int main(void)
{
int * a = 0;
func(a);
printf(" value of a = %d" , *a); /* getting SEGV fault here */
}

buf works this way -

void func(int ** x)
{
*x = (int*)(malloc(sizeof(int*)));
**x = 2;
}

int main(void)
{
int * a = 0;
func(&a);
printf(" value of a = %d" , **a); /* Prints 2 */
}


Could someone please explain this in detail.

Shouldn't the first code example work fine
1. I'm passing a pointer to a function
2. which intializes it
3. and assigns a value to the location pointed to by this pointer
4. Running this in a debugger shows that in main() 'a' hasn't been
intialized even after the call to func()

The second example works fine.

Thanks,
Ritesh
 
C

ciju

int * a = 0;
func(a);

Here u r passing a value of '0' to the func().

void func(int * x)
{
x = (int*)(malloc(sizeof(int*)));
*x = 2;
}

Now x will be having a value '0'. ( Usual call by value mechanism ).
Then u r allocating memory to x. Now x will contain an address returned
by malloc. So the value of the location pointed by 'a' won't be
updated.
Again in main u r trying to print '*a' , which is *(0)

The second example is correct.
 
S

suresh

ritesh said:
Hi,

I'm unable to figure out why initializing a pointer dosen't work this
way -

void func(int * x)
{
x = (int*)(malloc(sizeof(int*)));
*x = 2;
}

int main(void)
{
int * a = 0;
func(a);
printf(" value of a = %d" , *a); /* getting SEGV fault here */
}

This does not work for the same reason why the swap function below does
not work.

void swap(int x, int y)
{
int t = x;
x = y;
y = t;
}


Technically speaking there is no pass-by-reference in C but only
pass-by-value. Only the value of the expression is passed to the
function as an argument (as apposed to passing the actual object's
reference)

The modifications you do to the argument inside the function only
affects the local variable that initially had the passed function
argument's value.

The above explanation holds good even for pointer arguments.
 
K

Keith Thompson

ritesh said:
I'm unable to figure out why initializing a pointer dosen't work this
way -

void func(int * x)
{
x = (int*)(malloc(sizeof(int*)));
*x = 2;
}

int main(void)
{
int * a = 0;
func(a);
printf(" value of a = %d" , *a); /* getting SEGV fault here */
}

buf works this way -
[snip]

Could someone please explain this in detail.

Shouldn't the first code example work fine
1. I'm passing a pointer to a function
2. which intializes it
3. and assigns a value to the location pointed to by this pointer
4. Running this in a debugger shows that in main() 'a' hasn't been
intialized even after the call to func()

You need #include directives for <stdio.h> (for printf) and <stdlib.h>
(for malloc).

Don't cast the result of malloc; it can hide errors (such as failing
to #include <stdlib.h>). malloc() returns void*, which is implicitly
converted to the target type.

x need to point to an int, but you allocate enough space to hold an
int*. A good idiom to avoid this kind of problem is:

x = malloc(sizeof *x);

You pass an int* value, a, to func. Inside func, the value is copied
to x, which is local to func (that's how parameter passing works in
general, for any type). You then write over that value by assigning a
new value to it. When you leave func, you lose the only pointer to
the memory you allocated, creating a memory leak.
 
P

pemo

ritesh said:
Hi,

I'm unable to figure out why initializing a pointer dosen't work this
way -

void func(int * x)
{
x = (int*)(malloc(sizeof(int*)));
*x = 2;
}

int main(void)
{
int * a = 0;
func(a);
printf(" value of a = %d" , *a); /* getting SEGV fault here */
}

buf works this way -

void func(int ** x)
{
*x = (int*)(malloc(sizeof(int*)));
**x = 2;
}

int main(void)
{
int * a = 0;
func(&a);
printf(" value of a = %d" , **a); /* Prints 2 */
}


Could someone please explain this in detail.

Shouldn't the first code example work fine
1. I'm passing a pointer to a function
2. which intializes it
3. and assigns a value to the location pointed to by this pointer
4. Running this in a debugger shows that in main() 'a' hasn't been
intialized even after the call to func()

The second example works fine.

Thanks,
Ritesh

Here is what I believe you want - albeit with some diagnostic /clutter/

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

void func(int ** x)
{
puts("\nIn func()");

printf("\tx's *value* is %p, *x's value is %p\n", (void *)x, (void
*)*x);

puts("\tmalloc()");

*x = malloc(sizeof(int)); // replaced sizeof(int *)

printf("\tx's *value* is %p, *x's value is %p\n", (void *)x, (void
*)*x);

printf("\t**x's *value* is %d\n", **x);

**x = 2;

printf("\t**x's *value* is %d\n", **x);

puts("Returning from func()\n");
}


int main(void)
{
int * a = NULL;

printf("main()\n\ta's address is %p, its value is %p\n", (void *)&a,
(void *)a);

func(&a);

printf("main()\n\ta's address is %p, its value is %p\n", (void *)&a,
(void *)a);

printf("\tcontents of *a is %d\n" , *a);

free(a);

return 0;
}
 
K

Kenneth Brody

ritesh said:
Hi,

I'm unable to figure out why initializing a pointer dosen't work this
way - [... calling func(int *) ...]
int * a = 0;
func(a); [...]
buf works this way - [... calling func(int **) ...]
int * a = 0;
func(&a); [...]
Could someone please explain this in detail.

Simple:

C passes parameters by value.

Check your books to see how this differs from "pass by reference".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

Default User

ritesh said:
Hi,

I'm unable to figure out why initializing a pointer dosen't work this
way -

void func(int * x)
{
x = (int*)(malloc(sizeof(int*)));
*x = 2;
}

int main(void)
{
int * a = 0;
func(a);
printf(" value of a = %d" , *a); /* getting SEGV fault here */
}

buf works this way -

void func(int ** x)
{
*x = (int*)(malloc(sizeof(int*)));
**x = 2;
}

http://c-faq.com/ptrs/passptrinit.html




Brian
 

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

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top