4

My main() crashes below when add(4) is called.

As I understand int* add, it should return a pointer to integer. Then, I should be able in main to say:

int * a = add(3);

to return a pointer to int.

Please explain what I'm doing wrong.

#include <cstdlib>
#include <iostream>

using namespace std;

int* add (int a) {
   int * c, d;
   d = a + 1;
   *c = d;
   cout << "c = " << c << endl; 
   return c;
}

int main(int argc, char *argv[])
{
    int a = 4;

    int * c;

    c = add(4); 

    system("PAUSE");
    return EXIT_SUCCESS;
}
8
  • +1 - @Rubber boots is correct Commented Aug 18, 2010 at 14:38
  • 4
    No he's not. Reading comprehension, people. *c = d does not make c point to d! Commented Aug 18, 2010 at 14:38
  • 1
    Worse than that, you're writing to, and returning, an uninitialised pointer. Commented Aug 18, 2010 at 14:38
  • @all - oops I saw that and corrected the comment that when your complaints came. What do now? Should I strike out the modification? How to do that? Commented Aug 18, 2010 at 14:40
  • 1
    This is unreal, I figured when I clicked this it would be a scramble between 10 people to get the right answer in, and instead I find myself downvoting a half-dozen wrong answers Commented Aug 18, 2010 at 14:42

4 Answers 4

8

The problem is that you have declared an int* but not given it anything to point to. What you need to do is initialize it with a memory location (error checknig omitted)

int* c = new int();
...
*c = d;  // Now works

Later on though you'll need to make sure to free this memory since it's an allocated resource.

A better solution though is to use references. Pointers have several nasty attributes including unitialized values, NULL, need to free, etc ... Most of which aren't present on references. Here is an example of how to use references in this scenario.

void add (int a, int& c) {
   int d;
   d = a + 1;
   c = d;
   cout << "c = " << c << endl; 
}

int c;
add(4, c);
Sign up to request clarification or add additional context in comments.

2 Comments

In C++, using malloc is almost certainly the wrong thing to do.
@Mike, agreed. Only read the problem code and assumed it was a C question. Updated
5

In

 *c = d;

the pointer c is not initialized, so your program runs into undefined behavior. You could do something like the following instead:

void add( int what, int* toWhat )
{
    (*toWhat) += what;
}

and call it like this:

int initialValue = ...;
add( 4, &initialValue );

Comments

3

You never allocate any memory to the pointer c. Pointers must refer to valid memory, and you must allocate that memory yourself with a call to new, e.g. write

int* c = new int();

within the add function. Now c points to a valid block of memory that is large enough to hold an int. When you are done with that memory, call delete c to deallocate it and release it back to the system.

2 Comments

The question is tagged C++, so using malloc would be a very bad idea.
Well, not a bad idea (malloc is perfectly legal in C++) but not the best idea. Will fix, though.
1

You get an error because c is an uninitialized pointer, so it is undefined behaviour.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.