Solved

size_t

Posted on 2013-11-13
4
291 Views
Last Modified: 2013-11-22
Hi

I am trying to see what the implementation definition  of size_t is in my project. I tried searching by typedef.*size_t and also using.*size_t but I found no results. Could size_t be defined by default somewhere? I expected to see it in a (C++) header file and I was expecting it to be programmer defined.

Am i incorrect in assuming it is defined for the project by the programmer? And if so, where else should I be looking for its definition?

thanks
0
Comment
Question by:LuckyLucks
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
it depends on your compiler whether it is a built-in type like int or double or if it was a typedef.

at windows platform the vc compiler defines it in a c++ header like

#ifndef _SIZE_T_DEFINED
#ifdef  _WIN64
typedef unsigned __int64    size_t;
#else
typedef _W64 unsigned int   size_t;
#endif
#define _SIZE_T_DEFINED
#endif

Open in new window



by use of sizeof you would find out whether it is 32-bit or 64-bit. newer compilers always should use 64-bit. signed or unsigned you may find out by assigning -1 and 0 to size_t variables and compare them.

Sara
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>I am trying to see what the implementation definition  of size_t is in my project

What about

std::cout << "The size of 'size_t' is " << sizeof(size_t) << std::endl;

Open in new window


or

printf("The size of 'size_t' is %d\n", sizeof(size_t));

Open in new window


respectively?
0
 
LVL 7

Expert Comment

by:Murugesan Nagarajan
Comment Utility
It depends upon the search command being used to find size_t
I have used the following command in my Linux machine:
/usr/bin/find /usr/include/ -type f -name "*.h" -exec /bin/egrep -l "typedef.*unsigned long.*size_t" {} \;

Open in new window

/usr/include/asm-x86_64/posix_types.h

/usr/bin/find /usr/include/ -type f -name "*.h" -exec /bin/egrep "typedef.*unsigned long.*size_t" {} \;

Open in new window

typedef unsigned long   __kernel_size_t;

/usr/bin/find /usr/include/ -type f -name "*.h" -exec /bin/egrep -l "typedef.*__kernel_size_t.*size_t" {} \;

Open in new window

/usr/include/linux/types.h

/usr/bin/find /usr/include/ -type f -name "*.h" -exec /bin/egrep "typedef.*__kernel_size_t.*size_t" {} \;

Open in new window

typedef __kernel_size_t         size_t;


My machine details:
/bin/cat /etc/redhat-release

Open in new window

CentOS release 5.5 (Final)

/bin/uname -sm

Open in new window

Linux x86_64
0
 
LVL 7

Expert Comment

by:Murugesan Nagarajan
Comment Utility
Knowing the type of size_t using gdb

$ /bin/cat  know_Type_size_t.c

Open in new window



#include <stdio.h>
int main( )
{
        size_t checkThisType = 0;
        printf( "%d\n", checkThisType);
        return 0;
}

Open in new window



Compilation:
/usr/bin/gcc -g know_Type_size_t.c -o a.out

Open in new window

With or without "-o a.out" above, binary name created is a.out



/usr/bin/gdb -q a.out
(gdb) break main
Breakpoint 1 at 0x4004a0: file know_Type_size_t.c, line 4.
(gdb) run
Starting program: a.out
Breakpoint 1, main () at know_Type_size_t.c:4
4               size_t checkThisType = 0;
(gdb) ptype checkThisType
type = long unsigned int

Open in new window





Using dbx, use the following instead of "break main"
stop in main

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now