473,748 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Good use of pointers?

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

int *ip[10];
ip[0] = malloc(sizeof(i nt));
*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??
Feb 23 '06 #1
9 1186
Johs32 wrote:
I am a bit uncertain that this is a good use of pointers:

int *ip[10];
ip[0] = malloc(sizeof(i nt));
*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).

Feb 23 '06 #2
Johs32 wrote:
I am a bit uncertain that this is a good use of pointers:

int *ip[10];
ip[0] = malloc(sizeof(i nt));
*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.

--
Best regards,
Andrey Tarasevich
Feb 23 '06 #3
Antonio Contreras wrote:
Johs32 wrote:
I am a bit uncertain that this is a good use of pointers:

int *ip[10];
ip[0] = malloc(sizeof(i nt));
*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.

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).

Feb 24 '06 #4
So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(i nt));
*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[i] = NULL;
}
ip[0] = malloc(sizeof(i nt));
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;
}

Feb 24 '06 #5
On 2006-02-24 09:12:45 -0500, "weaselboy1 976" <we***********@ yahoo.com> said:
So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(i nt));
*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.

--
Clark S. Cox, III
cl*******@gmail .com

Feb 24 '06 #6
Clark S. Cox III wrote:

On 2006-02-24 09:12:45 -0500, "weaselboy1 976" <we***********@ yahoo.com> said:
So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(i nt));
*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.

--
pete
Feb 24 '06 #7
On 2006-02-24, weaselboy1976 <we***********@ yahoo.com> wrote:
So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(i nt));
*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
Feb 24 '06 #8
"weaselboy1 976" <we***********@ yahoo.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .
So, if you are looking to do something like:

int* someFunction()
{
int *ip[10];
ip[0] = malloc(sizeof(i nt));
*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[i] = NULL;
ip[i] = NULL;
}
ip[0] = malloc(sizeof(i nt));
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;
}

Feb 24 '06 #9
weaselboy1976 wrote:
...
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.

--
Best regards,
Andrey Tarasevich
Feb 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
3470
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
72
5884
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
14
4645
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further explanations, so I wonder is there any good using typedef ? and I also know that when a data type being typedefed become an abstract data type, so what exactly is an abstract data type, is it any good ? -- Posted via http://dbforums.com
43
2659
by: Sensei | last post by:
Hi! I'm thinking about a good programming style, pros and cons of some topics. Of course, this has nothing to do with indentation... Students are now java-dependent (too bad) and I need some serious motivations for many issues... I hope you can help me :) I begin with the two major for now, others will come for sure! - function variables: they're used to java, so no pointers. Personally I'd use always pointers, but why could be better...
17
1893
by: I_got_questions? | last post by:
I just started c programming. I want to migrate to c++. I know a little bit about class/inheritance. I am asking for good books to read. Elementary will be good. Thanks for any comments
6
1494
by: JoeC | last post by:
I understand the basics of pointers, they point to memory locations. I would like to know resources for learning all about poters. I am having some problems erasing elements of pointers from a vector. I wold like to know where I can get some in depth information on how to use pointers in various situations. It seems that all most books have to say about pointers is that they point to memory locations. Some even say they are...
206
8357
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
7
1502
by: Markus Pitha | last post by:
Hello, I still have massive problems with handling with pointers when I use them through methods or much more complicated constructs. Do you have any good and helpful links which describe these issues carefully? Thanks, Markus
75
3404
by: Amkcoder | last post by:
http://amkcoder.fileave.com/L_BitWise.zip http://amkcoder.fileave.com/L_ptr2.zip
0
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.