Solved

# Trying to find out time_t size and i have 4 bytes?

Posted on 2011-10-28
380 Views
Hi there;

I am trying to find a time_t struct size and what I have got is 4 bytes. Isn't it 8 bytes?

Here you go, for the code;

Kind regards.
``````# include <stdio.h>
# include <iostream>
# include <time.h>
using namespace std;
size_t getPtrSize( char *ptr )
{
return sizeof( ptr );
}

int main()
{
time_t lt;

lt = time(NULL);
cout << "Date: " << ctime(&lt) << endl;

cout << "\nThe length of " << ctime(&lt) << " is: "
<< sizeof ctime(&lt)
<< "\nThe size of the pointer is "
<< getPtrSize( ctime(&lt) ) << endl;

return 0;
}
``````
0
Question by:jazzIIIlove

LVL 31

Accepted Solution

Try using

cout << sizeof(time_t);

Address of pointers is always the same irrespective of the data type
0

LVL 12

Author Comment

Ok, working.

But why 4, not 8 with my code? I mean, could you explain that, before I conclude this question?

Regards.
0

LVL 31

Assisted Solution

Sizes are dependent upon your architecture.

Sizes of integers vary.  Sizes of pointer even vary.  If you run your code on anther machine, you may see a different value.

See this
http://www.cplusplus.com/doc/tutorial/pointers/
and search for "size in memory"
0

LVL 39

Assisted Solution

Just to clarify what farzanj has already said...

>> But why 4, not 8 with my code?
Because on your architecture a pointer is 4 bytes (32 bits).

>> Isn't it 8 bytes?
No. From the C standard: The range and precision of times representable in clock_t and time_t are implementation-defined.
0

## Featured Post

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why tâ€¦
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallâ€¦
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.