0

// The distance between two towns is 380km. A car and a lorry started from the two towns at the same time. At what speed drove the two vehicles, if the speed of the car is with 5km\ faster than the speed of the lorry and we know that they met after 4 hours? Use Dynamic Memory Allocation and Pointer in calculating and Displaying the needed info.

// The distance between two towns is 380km. A car and a lorry started from the two towns at the same time. At what speed drove the two vehicles, if the speed of the car is with 5km\ faster than the speed of the lorry and we know that they met after 4 hours? Use Dynamic Memory Allocation and Pointer in calculating and Displaying the needed info.

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>

int main ()
{
    int t=4;
    int d=380;
    int dt=20;
    int dd;
    int * ptr;
    int * ptr2;
    ptr  = (int*) malloc (500*sizeof(int));
    ptr2 = (int*) malloc (500*sizeof(int));
    dd=d-dt;
    ptr = dd/8;
    ptr2 = ptr+5;
    cout << " The Speed of the Car is: " << ptr << endl;
    cout << " The Speed of the Lorry is: " << ptr2 << endl;
    return(0);
}

How can i make this run? How to use dynamic memory allocation in all variables? Thanks.

Hulk
  • 6,399
  • 1
  • 30
  • 52
  • 4
    This statement: `ptr = dd/8;` is wrong. You're assigning the result of an integer division to a pointer variable, and adding insult to injury, leaking memory in the process. You're likewise leaking the memory previously allocated to `ptr2` on the next line. – WhozCraig Aug 30 '13 at 07:25
  • You probably meant to use `*ptr` and `*ptr2` (assuming you wanted to modify the values they point to). – SingerOfTheFall Aug 30 '13 at 07:30
  • I'm new in C++ can you help me? – StackingErrors Aug 30 '13 at 07:37
  • 1
    1. Fix your compilation error (i told you what it was in the prior comment). 2. Don't use `malloc()` in a C++ program; use `new`. 3. Don't leak memory; use `delete`, or in this case, `delete[]`. 4. Be prepared to run this through a debugger; its not going to work as-written even with those changes. The pointer usage in this needs significantly better understanding. – WhozCraig Aug 30 '13 at 07:47
  • 1
    @StackingErrors: You should get a [decent book](http://stackoverflow.com/questions/388242) and ignore whoever is teaching you to write code like this. – Mike Seymour Aug 30 '13 at 07:48
  • 1
    That is the most pointless use of dynamic allocation I've seen in a long time. Is there any chance you can get a different teacher? – molbdnilo Aug 30 '13 at 08:00

2 Answers2

1

To get the value a pointer ptr points to you must dereference it using operator *ptr

Thus, replace

ptr = dd/8
ptr2 = ptr+5;

with

*ptr = dd/8
*ptr2 = *ptr+5;

And this:

cout << " The Speed of the Car is: " << ptr << endl;
cout << " The Speed of the Lorry is: " << ptr2 << endl;

with

cout << " The Speed of the Car is: " << *ptr << endl;
cout << " The Speed of the Lorry is: " << *ptr2 << endl

;

cpp
  • 3,743
  • 3
  • 24
  • 38
0

As others in the comments have already pointed out, you need to write

*ptr = dd/8;
*ptr2 = ptr+5;

In addition to that, you also need to free any memory you got by calling malloc, but as you are trying to learn C++ and not C, I recommend using new and delete instead.

You'll also find (if your code example is complete) that you are missing std namespace specifier for cout and endl

Hulk
  • 6,399
  • 1
  • 30
  • 52