Good use of pointers?

J

Johs32

I am a bit uncertain that this is a good use of pointers:

int *ip[10];
ip[0] = malloc(sizeof(int));
*ip[0] = 22;

Is there any problems with this declaration and would it survive a function
that returns, if I make the function return a pointer to an int??
 
A

Antonio Contreras

Johs32 said:
I am a bit uncertain that this is a good use of pointers:

int *ip[10];
ip[0] = malloc(sizeof(int));
*ip[0] = 22;

Is there any problems with this declaration?

No

and would it survive a function
that returns, if I make the function return a pointer to an int??

If you enclose the three lines in a function body, the array will cease
to exist at the moment the function exits. If you return some of the
values of the array you may capture them in the caller, but the other 9
pointers will be lost. If you want to retain all ten pointer you should
malloc space for the array and return the address of the array as an
int**. (There are other possibilities, but this is the simplest IMO).
 
A

Andrey Tarasevich

Johs32 said:
I am a bit uncertain that this is a good use of pointers:

int *ip[10];
ip[0] = malloc(sizeof(int));
*ip[0] = 22;

It is impossible to decide whether this is a "good" or "bad" use from such an
abstract example, without context. There's nothing immediately illegal in this
code, that's all that can be said.
Is there any problems with this declaration

Declaration? The very first line is a declaration, the rest are not. The
declaration looks fine. Whether there are any problems with the whole thing
depends on what it is you are trying to do.
and would it survive a function
that returns, if I make the function return a pointer to an int??

Could you, please, clarify, _what_ exactly should survive? You declare an object
of type 'int*[10]', which cannot serve as a return value for a function that
"returns a pointer to an int" - the types are different. If you are planning to
return 'ip[0]' then everything will... hm... "survive", if I understand your
"survive" correctly.
 
S

Sirius Black

Antonio said:
Johs32 said:
I am a bit uncertain that this is a good use of pointers:

int *ip[10];
ip[0] = malloc(sizeof(int));
*ip[0] = 22;

Is there any problems with this declaration?


No

Except that you are not checking the ptr returned by malloc. You should
check if malloc succeeded or not and then only use the memory allocated.
 
W

weaselboy1976

So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(int));
*ip[0] = 22;
return ip[0];
}

The value 22 would "survive". But this is really bad form. You should
always do malloc and free within the same scope, not in different
functions. Also, it's a good idea to always make your functions return
an int that represents an error code. So something like this would be
much better:

int functionOne()
{
int i;
int *ip[10];
for (i=0; i<10; i++)
{
*ip = NULL;
}
ip[0] = malloc(sizeof(int));
if (ip[0] == (int)0)
return 1; /*error, allocation not done*/
if (functionTwo(ip[0])
return 2; /*error is functionTwo*/
if (*ip[0] != NULL)
{
free(ip[0];
*ip[0] = NULL;
}
return 0;
}

int functionTwo(int *ip)
{
*ip = 22;
return 0;
}
 
C

Clark S. Cox III

So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(int));
*ip[0] = 22;
return ip[0];
}

The value 22 would "survive". But this is really bad form. You should
always do malloc and free within the same scope, not in different
functions.

"Always" is a bit strong there. I'd bet that more often than not, the
malloc and free should be in different functions; otherwise, you've
done nothing but emulate automatic variables.
Also, it's a good idea to always make your functions return
an int that represents an error code.

Again, "Always" is a bit strong.
 
P

pete

Clark said:
So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(int));
*ip[0] = 22;
return ip[0];
}

The value 22 would "survive". But this is really bad form. You should
always do malloc and free within the same scope, not in different
functions.

"Always" is a bit strong there. I'd bet that more often than not, the
malloc and free should be in different functions; otherwise, you've
done nothing but emulate automatic variables.

It is a bit strong.
Linked lists are simplest to free with a list freeing function,
rather than from where the nodes were allocated.
 
R

Richard G. Riley

So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(int));
*ip[0] = 22;
return ip[0];
}

The value 22 would "survive". But this is really bad form. You should
always do malloc and free within the same scope, not in different
functions. Also, it's a good idea to always make your functions return
an int that represents an error code. So something like this would be
much better:

This is not true. Sorry.

In any system of any size, memory allocations are frequently handled
in an optimised system memory handler. In addition the creation of an
object in the system does not immediatley mean that good prorgamming
practices insist that that same function also destroys it. A good example
might be a message based window system : the code with creates the
window/dialog is never (almost) responsible for deleting it. A system
dealing with Business Objects based on a database. Lots of things.

malloc is not immitiating an automatic stack variable and it not
intended to. That said, if using malloc to create a temporary buffer,
of course it makes sense to clean up in the same scope as it was
created if it is no further needed
 
S

stathis gotsis

weaselboy1976 said:
So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(int));
*ip[0] = 22;
return ip[0];
}

The value 22 would "survive". But this is really bad form. You should
always do malloc and free within the same scope, not in different
functions. Also, it's a good idea to always make your functions return
an int that represents an error code. So something like this would be
much better:

int functionOne()
{
int i;
int *ip[10];
for (i=0; i<10; i++)
{
*ip = NULL;


ip = NULL;
}
ip[0] = malloc(sizeof(int));
if (ip[0] == (int)0)
return 1; /*error, allocation not done*/
if (functionTwo(ip[0])

Syntax error.
return 2; /*error is functionTwo*/
if (*ip[0] != NULL)

ip[0] != NULL
{
free(ip[0];

Syntax error.
*ip[0] = NULL;
}
return 0;
}

int functionTwo(int *ip)
{
*ip = 22;
return 0;
}
 
A

Andrey Tarasevich

weaselboy1976 said:
...
The value 22 would "survive". But this is really bad form. You should
always do malloc and free within the same scope, not in different
functions.
...

No. Absolutely not. This is one of those "fake" rules that sound "true" but in
fact make no sense at all.

There are several reason to use freestore in the program (as opposed to using
static and automatic storage) and one of the most important ones is that
freestore allows the user to extend the lifetime of the object beyond the bounds
dictated by the scoping rules.
 

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
474,175
Messages
2,570,946
Members
47,497
Latest member
PilarLumpk

Latest Threads

Top