passing an uninitialize pointer

J

joe

Hi all! I just have quick, possibly stupid question....
is it possible to do the following:

int func(){
int *pointer;

foo(pointer);
}

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int));
}

do I just have the syntax wrong or is this illegal?

Thanks in advance!
Joe
 
A

Artie Gold

joe said:
Hi all! I just have quick, possibly stupid question....
is it possible to do the following:

int func(){
int *pointer;

foo(pointer);
Since arguments in C are passed by *value*, the variable `pointer'
remains uninitialized at this point.
}

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int));
Even worse, all the above line does is introduce a memory leak.
}

do I just have the syntax wrong or is this illegal?
The syntax is fine. You don't even invoke undefined behavior.
Unfortunately, outside of the memory leak, it doesn't do *anything*.

A function cannot side effect a parameter (pass-by-value) but it can
side effect what a parameter *points to*.

In this case something like:

void foo(int **pointer) {
*pointer = malloc(10 * sizeof **pointer);
}

int main(void) {
int *pointer;
foo(&pointer);
/* pointer now contains the value returned by
malloc() in foo() */
...
}

HTH,
--ag
 
A

Alex Fraser

Artie Gold said:
Since arguments in C are passed by *value*, the variable `pointer'
remains uninitialized at this point.
Even worse, all the above line does is introduce a memory leak.
The syntax is fine. You don't even invoke undefined behavior.

So passing an uninitialised pointer (which must mean accessing it in order
to make a copy) doesn't invoke undefined behaviour?

Alex
 
M

Martin Ambuhl

joe said:
Hi all! I just have quick, possibly stupid question....
is it possible to do the following:

int func(){
int *pointer;

foo(pointer);
}

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int));
}

do I just have the syntax wrong or is this illegal?

This is covered fully in the FAQ. The fact that you cast the return
from malloc as well as asking this question demonstrates that you have
neither followed the newsgroup before posting not checked the FAQ. That
is a usenet sin.

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

#define MAGICNUMBER 10

int *foo(int **pointer)
{
*pointer = malloc(MAGICNUMBER * sizeof **pointer);
return *pointer;
}

int main(void)
{
int *pointer = 0;
printf("before calling foo, pointer == %p\n", (void *) pointer);
if (!foo(&pointer)) {
fprintf(stderr, "the malloc call failed\n");
exit(EXIT_FAILURE);
}
printf("after calling foo, pointer == %p\n", (void *) pointer);
free(pointer);
return 0;
}
 
R

Ravi Uday

joe said:
Hi all! I just have quick, possibly stupid question....
is it possible to do the following:

int func(){
int *pointer;

foo(pointer);
}

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int));
}

do I just have the syntax wrong or is this illegal?

Thanks in advance!
Joe

Try this -

int func(){
int *pointer;

pointer = foo(pointer);
/* If pointer == NULL .. print a error gracefully exit */

}

int *foo(int *pointer){
pointer = malloc(10 * sizeof *pointer );
if ( pointer == NULL )
return NULL;
else
return pointer;
}

Upon finishing usage of pointer don't forget to free ( free(pointer)).

HTH,
- Ravi
 
D

Darrell Grainger

Hi all! I just have quick, possibly stupid question....
is it possible to do the following:

int func(){
int *pointer;

foo(pointer);
}

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int));
}

do I just have the syntax wrong or is this illegal?

This is completely valid code but I'm guessing it does not do what you
want it to do. Just to keep things a little easier to discuss, here is
your code with different variables:

#include <stdlib.h>

void foo(int *);

int main(void)
{
int *p;
foo(p);
return 0;
}

void foo(int *q)
{
q = malloc(10*sizeof int);
}

First, I #include <stdlib.h> when using malloc. Second, I do not return
anything from foo so it is defined as returning void. Third, I do not cast
the result of malloc, which hides the fact you forgot to #include
<stdlib.h>.

Now, here is an explanation of what the code does:

The first line of main will create a variable called p. This variable will
exist somewhere. The contents of this variable will be some random value.
The second line of main we pass this random value to the function foo.
When we reach the function foo it creates a variable called q and assigns
q with the random value p holds. On the first line of foo we change the
value of q to the memory location returned by malloc. The variable q (not
p) now holds a valid memory location (assuming malloc was successful). I
then return to main. The variable q is now out of scope and disappears.
The memory it points to is lost.

Important thing to note is that at no time was the variable p assigned any
value. You are probably assuming that by altering q, a COPY of p, you are
some how altering p. Not so.

What you need to do is pass in the address of p (&p) and then have foo
accept (int **q). Any change to *q will be a change to p. This is because
q is a pointer to p thus *q is p.
 
R

Richard Bos

Try this -

It works, but it is unnecessarily complicated:
int func(){
int *pointer;

pointer = foo(pointer);
/* If pointer == NULL .. print a error gracefully exit */

}

int *foo(int *pointer){
pointer = malloc(10 * sizeof *pointer );

Why pass this pointer in at all, if the first thing you do with it in
the function is to overwrite it? You could just as easily have a local
temporary pointer object.
if ( pointer == NULL )
return NULL;
else
return pointer;

Erm... what does this if statement achieve that a simple

return pointer;

doesn't?

Richard
 
M

Michael Mair

Hi Alex,

So passing an uninitialised pointer (which must mean accessing it in order
to make a copy) doesn't invoke undefined behaviour?

Nope. You copy the "content" of the pointer, supposedly an address,
to another pointer object but you do not have to read let alone change
the content of the "address".
So, you essentially pass crap to the foo() function of the OP but
this crap is never evaluated; hence, no UB.

Unfortunately, your original crappy pointer does not become initialised,
so you invoke UB when trying to use it.


HTH
Michael
 
A

Alex Fraser

Michael Mair said:
So passing an uninitialised pointer (which must mean accessing it in
order to make a copy) doesn't invoke undefined behaviour?

Nope. You copy the "content" of the pointer, [...]

Right. My point was that if this assignment invokes undefined behaviour
(which, AFAIK, it does - correct me if I'm wrong):

void func(void) {
int i;
int j = i;
}

Then I don't see why this function call, which conceptually has an assigment
like above, wouldn't:

void foo(int j) { /* nothing */ }

void func(void) {
int i;
foo(i);
}

So, does this call to foo() invoke undefined behaviour? If so, doesn't the
call to foo() in the OP's code also invoke undefined behaviour? And if I'm
wrong please tell me why!

Alex
 
G

Gordon Burditt

So passing an uninitialised pointer (which must mean accessing it in order
Nope. You copy the "content" of the pointer, supposedly an address,
to another pointer object but you do not have to read let alone change
the content of the "address".

Using the content of an uninitialized pointer invokes undefined
behavior. Evaluating a floating-point variable containing a trapping
NaN causes undefined behavior. Evaluating a pointer containing a
trapping NaP (Not A Pointer) also causes undefined behavior. For
example:

int main(void)
{
int *p;
p; /* Undefined behavior */
return 0;
}
So, you essentially pass crap to the foo() function of the OP but
this crap is never evaluated; hence, no UB.

The crap *IS* evaluated (passing the pointer to a function). The
crap is not dereferenced, but since it was already evaluated, it's
too late to avoid undefined behavior.

Gordon L. Burditt
 
M

Michael Mair

Hiho,

Using the content of an uninitialized pointer invokes undefined
behavior. Evaluating a floating-point variable containing a trapping
NaN causes undefined behavior. Evaluating a pointer containing a
trapping NaP (Not A Pointer) also causes undefined behavior. For
example:

int main(void)
{
int *p;
p; /* Undefined behavior */
return 0;
}




The crap *IS* evaluated (passing the pointer to a function). The
crap is not dereferenced, but since it was already evaluated, it's
too late to avoid undefined behavior.

Ooops, I solemnly abdicate all evil:

I somehow considered only the stuff about trap representations (6.2.6.1)
and lvalues (6.3.2.1), deducing the wrong thing, but in 6.5.2.2 it
clearly says that arguments are evaluated and their values are assigned
to the corresponding parameter. Shame on me.

Sorry for the misinformation
Michael
 
J

Jack Klein

This is completely valid code but I'm guessing it does not do what you

No, invalid code. Invokes UB by passing the value of an uninitialized
object (pointer) to a function.
 
M

Malcolm

joe said:
int func(){
int *pointer;

foo(pointer);
}

int foo(int *pointer){
pointer = (int*)malloc(10 * sizeof(int));
}

do I just have the syntax wrong or is this illegal?
Others have pointed out that this is technically undefined behaviour, though
on most common platforms it won't cause a crash. It will only crash if
pointers have "trap representations" which could be triggered when you load
garbage (the unitialised pointer) into a pointer register to pass it to
foo(). Don't worry too much about this at your stage.

However as Richard Bos pointed out, what you are doing is pointless. There
is no point passing a parameter in just to overwrite it in the first line.
I suspect you are making this mistake

int func()
{
int *pointer;

foo(pointer);

/* pointer now points to allocated memory, right?
Wrong. C parameters are pass by value. The value of pointer is
copied and passed to foo(). When foo() modifies the parameter, the
variable in func() retains its same old garbage value. */
}
 
O

Old Wolf

Artie Gold said:
Since arguments in C are passed by *value*, the variable `pointer'
remains uninitialized at this point.
Even worse, all the above line does is introduce a memory leak.
The syntax is fine. You don't even invoke undefined behavior.

Actually it is undefined behaviour to access the value of an
uninitialized variable (in this case, 'pointer').

Also it is UB to call malloc without including said:
In this case something like:

void foo(int **pointer) {
*pointer = malloc(10 * sizeof **pointer);
}

int main(void) {
int *pointer;
foo(&pointer);
/* pointer now contains the value returned by
malloc() in foo() */
...
}

I'm surprised that nobody has suggested the simpler:

#include <stdlib.h>

int *foo(void)
{
return malloc(10 * sizeof(int));
}
/* ... */
int *pointer = foo();
 
R

Ravi Uday

Jack Klein said:
Does not.


Uninitialized pointer.


Accessing value of uninitialized object. Undefined behavior.


Ok so..initialize it to NULL..

int *pointer = NULL;

[also as bros pointed out the use of checking of return value inside the
'foo' function is useless]

- Ravi
 
P

Peter Nilsson

Old Wolf said:
... it is UB to call malloc without including <stdlib.h>.

No, it isn't.

#include <stdio.h>

void *malloc(size_t);

int main(void)
{
int *p = malloc(sizeof *p);
if (p)
{
*p = 42;
printf("%d\n", *p);
}
return 0;
}
 
O

Orhan Kavrakoglu

Uninitialized pointer.


Accessing value of uninitialized object. Undefined behavior.

How about this?

int *pointer1;
foo(&pointer1);

void foo(int **pointer)
{
*pointer = malloc(...);
}

I would say that this does not invoke UB (never mind the memory leak)
because the uninitialized pointer1 is never dereferenced. Am I
correct?
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top