Cant figure out runtime error issue

Im trying to use the time.h struct tm *pointer so I can access the values of my computer's local time and date. I have a rather large program that I'm writing, and I would prefer to not have to redeclare this thing in every function I wish to use it in. What is it that I am doing wrong?

Example of my attempt:

Header file

1
2
3
4
5
6
7
8
9
10
//Header file: attempt.h

class attempt {
   time_t rawtime;
   struct tm *value;

public:
   attempt();
   void prntFunc();
};


Implementation file

1
2
3
4
5
6
7
//Function: void attempt:prntFunc();
{
   time(&rawtime);
   this->value = gmtime(&rawtime);
   date = (value->tm_yday) + 1;
   cout << date << endl;
}


main file
1
2
3
4
5
6
7
//main function: int main()
{
   attempt A;
   A.prntFunc();
   system("pause");
   return 0;
}


Assume that all headers are included and nothing else is missing.

Thanks for your help.
¿what's the problem?
When I run debug, in visual studio 2010, it locks up and gives me an error. When I tried debugging the error using cout statements, they showed me that it worked up until around this area:

1
2
   time(&rawtime);
   this->value = gmtime(&rawtime);
Last edited on
You should initialize pointer in you constructor, delete it in destructor and define assigment operator and copy constructor.

Basicly: you are rewriting random memory area now. Which is bad.
Last edited on
> When I tried debugging the error using cout statements
Use a debugger next time. Perform a backtrace.
By the way, `std::cout' is buffered.
Guys: he is not using unallocated memory. gmtime() returns a pointer to a static object. He does not have to manage the memory himself.

OP: You are not doing anything wrong in this code you have posted. It compiles and runs fine here after I fill in the gaps.

This is working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <ctime>
#include <iostream>
using namespace std;

class attempt {
   time_t rawtime;
   struct tm *value;

public:
    attempt() {}
   void prntFunc();
};

void attempt::prntFunc()
{
   time(&rawtime);
   this->value = gmtime(&rawtime);
   auto date = (value->tm_yday) + 1;
   cout << date << endl;
}

int main()
{
   attempt A;
   A.prntFunc();
   cin.get();
   return 0;
}


The problem must be elsewhere in your program. We will not be able to solve without seeing more code.
Topic archived. No new replies allowed.