3

I wrote the below program to set a value (here it's 3) to some location at memory that is pointed by a pointer named p using a function named f() and print it in the main:

#include <iostream>
using namespace std;

void f(float* q)
{
        q=new float;
        *q=3;
}

int main()
{
    float *p= nullptr;
    f(p);
    cout<<*p;
    return 0;
}

But when I want to compile it, I receive this compile time error :

ap1019@sharifvm:~$ g++ myt.cpp
myt.cpp: In function âint main()â:
myt.cpp:12:11: error: ânullptrâ was not declared in this scope
  float *p=nullptr;
           ^
ap1019@sharifvm:~$

What's wrong?

TheGoodUser
  • 1,188
  • 4
  • 26
  • 52

3 Answers3

2

It seems that pointer literal nullptr is not supported by your compiler. You may use null pointer constant instead. For example

float *p = 0;

But in any case your program is wrong. It has a memory leak because you store the address of the allocated memory in a local variable of function f that will be destroyed after exiting the function.

The program could look the following way

#include <iostream>
using namespace std;

void f( float **q)
{
        *q = new float;
        **q = 3;
}

int main()
{
    float *p = 0;

    f( &p );

    cout << *p;

    delete p;

    return 0;
}

Or you could use reference to the pointer. For example

#include <iostream>
using namespace std;

void f( float * &q)
{
        q = new float;
        *q = 3;
}

int main()
{
    float *p = 0;

    f( p );

    cout << *p;

    delete p;

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Isn't `&q` equal with `&p` in my program? Is `q` equal with `p` or it is just another pointer that points to same address that `p` points to? In the other word, when I pass `p` to `f()` what is the type and data that I pass? Sorry, I'm a little confused :) I want to say : I defined `p` in `main`, and refer it to my function, so why should I be careful about the `q`? – TheGoodUser Apr 24 '15 at 08:43
  • I really appreciate you if add a brief interpretation about your two programs please. Thanks :) – TheGoodUser Apr 24 '15 at 08:47
  • 1
    @TheGoodUser If you are speaking about your original code then function parameters are local variables of the functions. It looks like you have a statement float *q = p; That is q has a copy of the value of p. Any changes of q do not influence on the original variable p. Variable p will keep its initial value. It is q that will be changed and after exiting the function will be destroyed. – Vlad from Moscow Apr 24 '15 at 08:48
  • `q` has a copy of the value of `p` or it points to the same address that `p` is point to? – TheGoodUser Apr 24 '15 at 08:51
  • 1
    @TheGoodUser It has a copy of the value of p that is the same address of memory. However if you assign to q a new value for example by means of operator new the value stored in p will not be changed. – Vlad from Moscow Apr 24 '15 at 08:52
  • Thanks, I got it. :) So in the line `cout<<*p;` I want to print nothing in output, right? In this case will I receive an error or it must terminate normally? (On my computer I have `Segmentation fault (core dumped)` error in runtime, while in `www.cpp.sh` it run normally and print a space char (as null,I guess) in output ) Is the response OS dependent? – TheGoodUser Apr 24 '15 at 08:59
  • As I understand, my original program is equal with a program that defined `f()` without any inputs and used `q=new float;` with `float *q=new float;` in that function. right? – TheGoodUser Apr 24 '15 at 09:09
  • 1
    @TheGoodUser it looks like float *q = p; *q = new float. The p itself is not changed. – Vlad from Moscow Apr 24 '15 at 09:28
1

nullptr is only supported from gcc-4.6 or later.

You can easily workaround that with a const void *nullptr=(void*)0;, but to avoid later problems with a gcc upgrade, I suggest to

  • upgrade your gcc (4.6 is quite old)
  • or don't use it.

It is only syntactic sugar, you don't need that.

peterh
  • 11,875
  • 18
  • 85
  • 108
-1

The word null is not reserved by the C++ standard.

Use NULL instead.

Waqar ul islam
  • 418
  • 4
  • 17
  • 1
    er... OP used `nullptr` which is a reserved word in c++11, look at the references in other answers. About using NULL, see for example: http://stackoverflow.com/questions/3577580/using-null-in-c – jsantander Apr 24 '15 at 08:31