473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

using new in functions, pointers passed still NULL after called?!

Hello,

So I think the answer to this question is going to be rather simple.

Basically I have a pointer to a class, and I want to have a function
to call to initialize it.

After this function is called, the pointer is still NULL.

Here is a simplified version, but basically the same:

void main(){
MyClass *ptr = NULL;
createMyClass(ptr);
}

createMyClass(MyClass *ptrToClass){
ptrToClass= new MyClass();
}

Any reason why ptr would still be NULL after createMyClass is called?

Thanks,
_ivan
Jul 19 '05 #1
4 4509
Hi Ivan,

"_ivan" <ik***@nyu.edu> wrote in message
news:f4**************************@posting.google.c om...
| void main(){
| MyClass *ptr = NULL;
| createMyClass(ptr);
| }
|
| createMyClass(MyClass *ptrToClass){
| ptrToClass= new MyClass();
| }
|
| Any reason why ptr would still be NULL after createMyClass is called?

By default in C/C++, function parameters *copies* of values
that are passed.

In the code above, changing the value of 'ptrToClass' will
not affect 'ptr' in main.

A way to achieve what you expect is to pass the parameter
by reference. What you need to do is change the function's
signature as follows:
createMyClass( MyClass*& ptrToClass )

This will behave as you expect...
hth
--
http://www.post1.com/~ivec <> Ivan Vecerina
http://www.brainbench.com <> Brainbench MVP for C++
Jul 19 '05 #2

_ivan <ik***@nyu.edu> wrote in message
news:f4**************************@posting.google.c om...
Hello,

So I think the answer to this question is going to be rather simple.

Basically I have a pointer to a class, and I want to have a function
to call to initialize it.
A class type object should be initialized with a
constructor, not by calling a function after the fact.

After this function is called, the pointer is still NULL.

Here is a simplified version, but basically the same:

void main(){
int main() {
MyClass *ptr = NULL;
createMyClass(ptr);
}

createMyClass(MyClass *ptrToClass){
ptrToClass= new MyClass();
}

Any reason why ptr would still be NULL after createMyClass is called?
Yes. Look up 'pass by value' vs. 'pass by reference'.

#include <iostream>

class MyClass {};

void createMyClass(MyClass *&ptrToClass)
{
ptrToClass = new MyClass;
}

int main()
{
MyClass *ptr = 0;
std::cout << "Before: " << ptr << '\n';
createMyClass(ptr);
std::cout << "After : " << ptr << '\n';
delete ptr;
return 0;
}

But why not just create the object directly in main(),
and don't fool with 'new'.?

int main()
{
MyClass mc;
return 0;
}

Or if you must use 'new' for whatever reason, why not
just call it directly instead of making a separate
function?

int main()
{
MyClass *ptr = new MyClass;
/* etc */
delete ptr;
return 0;
}

-Mike
_ivan

Jul 19 '05 #3

"Ivan Vecerina" <iv**@myrealbox.com> wrote in message news:3f********@news.swissonline.ch...
By default in C/C++, function parameters *copies* of values
that are passed.

With the exception of the apparent (but not in reality) passing of arrays.
Jul 19 '05 #4
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<y1**************@newsread3.news.pas.earthlin k.net>...
_ivan <ik***@nyu.edu> wrote in message
news:f4**************************@posting.google.c om...
Hello,

So I think the answer to this question is going to be rather simple.

Basically I have a pointer to a class, and I want to have a function
to call to initialize it.


A class type object should be initialized with a
constructor, not by calling a function after the fact.

After this function is called, the pointer is still NULL.

Here is a simplified version, but basically the same:

void main(){


int main() {
MyClass *ptr = NULL;
createMyClass(ptr);
}

createMyClass(MyClass *ptrToClass){
ptrToClass= new MyClass();
}

Any reason why ptr would still be NULL after createMyClass is called?


Yes. Look up 'pass by value' vs. 'pass by reference'.

#include <iostream>

class MyClass {};

void createMyClass(MyClass *&ptrToClass)
{
ptrToClass = new MyClass;
}

int main()
{
MyClass *ptr = 0;
std::cout << "Before: " << ptr << '\n';
createMyClass(ptr);
std::cout << "After : " << ptr << '\n';
delete ptr;
return 0;
}

But why not just create the object directly in main(),
and don't fool with 'new'.?

int main()
{
MyClass mc;
return 0;
}

Or if you must use 'new' for whatever reason, why not
just call it directly instead of making a separate
function?

int main()
{
MyClass *ptr = new MyClass;
/* etc */
delete ptr;
return 0;
}

-Mike
_ivan

You can also use the pointer to pointer like this
#include <iostream>
class MyClass
{
};

void createMyClass(MyClass **ptrToClass)
{
*ptrToClass= new MyClass();
std::cout<<"Value in Function:"<<*ptrToClass<<std::endl;
}
int main()
{
MyClass *ptr = NULL;
createMyClass(&ptr);
std::cout<<"Value in main:"<<ptr<<std::endl;
return 0;
}
in this way change reflects in the actual pointer when you return from the function.
Jul 19 '05 #5

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

Similar topics

7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
27
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux): ================================================================== read_raw_data.c:51: warning:...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
7
by: srinivasaraokoneru | last post by:
Can structures be passed to the functions by value?
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.