473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why does this cause access error reading null pointer?

Microsoft Visual C++ .net 2003. I was just curious what numeric_limits for
std::string would give me so wrote this:

#include <iostream>
#include <string>
#include <limits>
int main()
{
std::cout << std::numeric_limits<std::string>::max() << '\n';
}

When I run it I get an access violation reading location 0x00000000
I couldn't quite figure out why however. Any thoughts?
Jul 20 '07 #1
2 1821
On Jul 20, 12:51 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
Microsoft Visual C++ .net 2003. I was just curious what
numeric_limits for std::string would give me so wrote this:

std::cout << std::numeric_limits<std::string>::max() << '\n';

When I run it I get an access violation reading location 0x00000000
I couldn't quite figure out why however. Any thoughts?
Well, the standard just says that using max()
on a type without numeric limits, is "not meaningful".

I don't know if this is licence to exit the program though.

BTW you can check if numeric_limits is defined for a
type, by reading the bool std::numeric_limits<T>::is_specialized.

Also, it is defined that non-fundamental standard types
(such as std::string) do not have specializations of
numerc_limits, so this property should always be
false for std::string.

Jul 20 '07 #2
Jim Langston wrote:
Microsoft Visual C++ .net 2003. I was just curious what numeric_limits
for std::string would give me so wrote this:

#include <iostream>
#include <string>
#include <limits>
int main()
{
std::cout << std::numeric_limits<std::string>::max() << '\n';
}

When I run it I get an access violation reading location 0x00000000
I couldn't quite figure out why however. Any thoughts?
The default implementation of numeric_limits in your library may contain
code like this:
// default implementation
template < typename T >
struct numeric_limits {

static
T max ( void ) {
return ( T(0) );
}

// ...

};

In this case, the string is being constructed from the null pointer, which
yields UB.
Best

Kai-Uwe Bux
Jul 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
7
by: Daniel Rudy | last post by:
Hello, I have a peice of code that I'm making an attempt to code. The problem is that I need to return an arbitrary number of char strings. int function(char *fname, int *dcount, char *data)...
33
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
11
by: sgadag | last post by:
Even if "a" is NULL in the assignment below, this assignment does not cause any AV: SOME_PTR * someVar = (SOME_PTR *) a->b; But something like this will cause an AV because "someVar" is...
4
by: arnuld | last post by:
1.) we usually say /return 0/ indicates success e.g #include <iostream> int main() { int i=3; std::cout << i << std::endl; return 0;
42
by: Sheldon | last post by:
Hi, This program works when tested with gdb ( see results 1) but when used in a larger program where 12 becomes 1500 there exists a problem when freeing the memory ( see results 2). Can anyone...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.