The code successfully compiles it but I can't understand why, for certain values of number, the program crashes and for other values it doesn't. Could someone explain the behavior of adding a long int with a char* that the compiler uses?
#include <iostream>
int main()
{
long int number=255;
std::cout<< "Value 1 : " << std::flush << ("" + number) << std::flush << std::endl;
number=15155;
std::cout<< "Value 2 : " << std::flush << ("" + number) << std::flush << std::endl;
return 0;
}
Test results:
Value 1 : >
Value 2 : Segmentation fault
Note: I'm not looking for a solution on how to add a string with a number.
In C++, "" is a const char[1] array, which decays into a const char* pointer to the first element of the array (in this case, the string literal's '\0' nul terminator).
Adding an integer to a pointer performs pointer arithmetic, which will advance the memory address in the pointer by the specified number of elements of the type the pointer is declared as (in this case, char).
So, in your example, ... << ("" + number) << ... is equivalent to ... << &""[number] << ..., or more generically:
const char *ptr = &""[0];
ptr = reinterpret_cast<const char*>(
reinterpret_cast<const uintptr_t>(ptr)
+ (number * sizeof(char))
);
... << ptr << ...
Which means you are going out of bounds of the array when number is any value other than 0, thus your code has undefined behavior and anything could happen when operator<< tries to dereference the invalid pointer you give it.
Unlike in many scripting languages, ("" + number) is not the correct way to convert an integer to a string in C++. You need to use an explicit conversion function instead, such as std::to_string(), eg:
#include <iostream>
#include <string>
int main()
{
long int number = 255;
std::cout << "Value 1 : " << std::flush << std::to_string(number) << std::flush << std::endl;
number = 15155;
std::cout << "Value 2 : " << std::flush << std::to_string(number) << std::flush << std::endl;
return 0;
}
Or, you can simply let std::ostream::operator<< handle that conversion for you, eg:
#include <iostream>
int main()
{
long int number = 255;
std::cout<< "Value 1 : " << std::flush << number << std::flush << std::endl;
number = 15155;
std::cout<< "Value 2 : " << std::flush << number << std::flush << std::endl;
return 0;
}
Pointer arithmetic is the culprit.
A const char* is accepted by operator<<, but will not point to a valid memory address in your example.
If you switch on -Wall, you will see a compiler warning about that:
main.cpp: In function 'int main()':
main.cpp:6:59: warning: array subscript 255 is outside array bounds of 'const char [1]' [-Warray-bounds]
6 | std::cout<< "Value 1 : " << std::flush << ("" + number) << std::flush << std::endl;
| ^
main.cpp:8:59: warning: array subscript 15155 is outside array bounds of 'const char [1]' [-Warray-bounds]
8 | std::cout<< "Value 2 : " << std::flush << ("" + number) << std::flush << std::endl;
| ^
Value 1 : q
Live Demo
Related
The code successfully compiles it but I can't understand why, for certain values of number, the program crashes and for other values it doesn't. Could someone explain the behavior of adding a long int with a char* that the compiler uses?
#include <iostream>
int main()
{
long int number=255;
std::cout<< "Value 1 : " << std::flush << ("" + number) << std::flush << std::endl;
number=15155;
std::cout<< "Value 2 : " << std::flush << ("" + number) << std::flush << std::endl;
return 0;
}
Test results:
Value 1 : >
Value 2 : Segmentation fault
Note: I'm not looking for a solution on how to add a string with a number.
In C++, "" is a const char[1] array, which decays into a const char* pointer to the first element of the array (in this case, the string literal's '\0' nul terminator).
Adding an integer to a pointer performs pointer arithmetic, which will advance the memory address in the pointer by the specified number of elements of the type the pointer is declared as (in this case, char).
So, in your example, ... << ("" + number) << ... is equivalent to ... << &""[number] << ..., or more generically:
const char *ptr = &""[0];
ptr = reinterpret_cast<const char*>(
reinterpret_cast<const uintptr_t>(ptr)
+ (number * sizeof(char))
);
... << ptr << ...
Which means you are going out of bounds of the array when number is any value other than 0, thus your code has undefined behavior and anything could happen when operator<< tries to dereference the invalid pointer you give it.
Unlike in many scripting languages, ("" + number) is not the correct way to convert an integer to a string in C++. You need to use an explicit conversion function instead, such as std::to_string(), eg:
#include <iostream>
#include <string>
int main()
{
long int number = 255;
std::cout << "Value 1 : " << std::flush << std::to_string(number) << std::flush << std::endl;
number = 15155;
std::cout << "Value 2 : " << std::flush << std::to_string(number) << std::flush << std::endl;
return 0;
}
Or, you can simply let std::ostream::operator<< handle that conversion for you, eg:
#include <iostream>
int main()
{
long int number = 255;
std::cout<< "Value 1 : " << std::flush << number << std::flush << std::endl;
number = 15155;
std::cout<< "Value 2 : " << std::flush << number << std::flush << std::endl;
return 0;
}
Pointer arithmetic is the culprit.
A const char* is accepted by operator<<, but will not point to a valid memory address in your example.
If you switch on -Wall, you will see a compiler warning about that:
main.cpp: In function 'int main()':
main.cpp:6:59: warning: array subscript 255 is outside array bounds of 'const char [1]' [-Warray-bounds]
6 | std::cout<< "Value 1 : " << std::flush << ("" + number) << std::flush << std::endl;
| ^
main.cpp:8:59: warning: array subscript 15155 is outside array bounds of 'const char [1]' [-Warray-bounds]
8 | std::cout<< "Value 2 : " << std::flush << ("" + number) << std::flush << std::endl;
| ^
Value 1 : q
Live Demo
I want to read a chunk of data from file into stringstream, which later will be used to parse the data (using getline, >>, etc). After reading the bytes, I set the buffer of the stringstream, but I cant make it to set the p pointer.
I tested the code on some online services, such as onlinegdb.com and cppreference.com and it works. However, on microsoft, I get an error - the pointers get out of order.
Here's the code, I replaced the file-read with a char array.
#include <sstream>
#include <iostream>
int main()
{
char* a = new char [30];
for (int i=0;i<30;i++)
a[i]='-';
std::stringstream os;
std::cout << "g " << os.tellg() << " p " << os.tellp() << std::endl;
os.rdbuf()->pubsetbuf(a,30);
os.seekp(7);
std::cout << "g " << os.tellg() << " p " << os.tellp() << std::endl;
}
the output I get when it works
g 0 p 0
g 0 p 7
the output I get on visual studio 2015
g 0 p 0
g -1 p -1
any ides?
thanks
std::sstream::setbuf may do nothing:
If s is a null pointer and n is zero, this function has no effect.
Otherwise, the effect is implementation-defined: some implementations do nothing, while some implementations clear the std::string member currently used as the buffer and begin using the user-supplied character array of size n, whose first element is pointed to by s, as the buffer and the input/output character sequence.
You are better off using the std::stringstream constructor to set the data or call str():
#include <sstream>
#include <iostream>
int main()
{
std::string str( 30, '-' );
std::stringstream os;
std::cout << "g " << os.tellg() << " p " << os.tellp() << std::endl;
os.str( str );
os.seekp(7);
std::cout << "g " << os.tellg() << " p " << os.tellp() << std::endl;
}
This is a console application in Visual studio and so I wanted to see what happened if I called a cout in a cout. And it works kinda but it removes a character which is kinda weird. So it removes the amount of characters from the string in the cout in the main. So it removes as much characters to the value of the return of the doPrint() function.
Example:
if the return value is 1 it will output "AAAAABLLLLLLLLLL"
if the return value is 2 it will output "AAAAALLLLLLLLLL"
#include "stdafx.h"
#include <iostream>
int doPrint()
{
std::cout << "AAAAA" << std::endl;
return 1;
}
int main()
{
std::cout << "BBLLLLLLLLLL" + doPrint() << std::endl;
int x;
std::cin >> x;
return 0;
}
It isn't that big of a deal but I would like to know why this happens.
Thanks already.
P.S: I know I should do << instead of +
Well, basically what happens is pointer arithmetic and specified evaluation order of function calls.
"BBLLLLLLLLLL" + doPrint()
So
"BBLLLLLLLLLL" + 1
yields
BLLLLLLLLLL
and
"BBLLLLLLLLLL" + 2
yields
LLLLLLLLLL
with std::cout.
It applies function pointer arithmetic with the character array literal, and "looses" characters, as doPrint() yields something bigger than 0.
The operator precedence of + is higher than <<, hence doPrint() is called first and prints AAAAA. So your staement
std::cout << "BBLLLLLLLLLL" + doPrint() << std::endl;
breaks down to
call doPrint()
1.1. call std::cout << "AAAAA" << std::endl;
call "BBLLLLLLLLLL" + 1 from the result value of doPrint()
call std::ostream& operator<<(std::otream&, const char*)
call std::endl
I have defined some functions and I print their address like this:
#include<iostream>
#include <string>
using std::cout;
std::string func()
{
return "hello world\n";
}
int func2(int n)
{
if (n==0)
{
cout << func2 << std::endl;
return 1;
}
cout << func2 << std::endl;
return n + func2(n - 1);
}
//================================================
int main()
{
int (*fun)(int) = func2;
cout << fun;
cout << std::endl << func2(3);
}
When I print the function's name (address) they all print 1 on my compiler (Mingw gcc 4.8 ).
Is it OK or it should differ?
It doesn't exist an overload of operator<< for std::ostream that takes a function pointer. Thus the operator<<(std::ostream&, bool) overload is prefered. The address of a function is always evaluated to true when converted to bool. Thus, 1 is printed.
Alternatevely, if a function pointer is not larger than the size of a data pointer, you could cast your function pointer to a void* via reinterpret_cast and evoke the operator<<(std::ostream&, void*) overload and thus get the actual address of the function printed.
int (*fun)(int) = func2;
std::cout << reinterpret_cast<void*>(fun) << std::endl;
Live Demo
However, as correctly Neil and M.M mentioned in the comments there's no standard conversion from a function pointer to a data pointer, and this could evoke undefined behaviour.
Alternatively, and in my humble opinion properly, you could format your function pointer as a char array buffer and convert its address to a string in the following way:
unsigned char *p = reinterpret_cast<unsigned char*>(&func2);
std::stringstream ss;
ss << std::hex << std::setfill('0');
for(int i(sizeof(func2) - 1); i >= 0; --i) ss << std::setw(2)
<< static_cast<unsigned int>(p[i]);
std::cout << ss.str() << std::endl;
Live Demo
You're not printing the address because it's now converted to a boolean value.
But you can do e.g. this:
std::cout << reinterpret_cast<unsigned long long int *>(func2) << std::endl;
Now you'll get the actual address.
Hi search for a way to dump the memory layout of a class/structure/datatype with clang.
I have a simple application based on this tutorial.
I also added this function
bool VisitFieldDecl(FieldDecl *F)
{
F->dump();
std::cerr << F->getQualifiedNameAsString() << " " << F->getBitWidthValue(*Context) << " " << std::endl;
F->dump() ;
std::cerr << "-----------------------------------------" << std::endl;
return true;
}
Unfortunately getBitWidthValue also returns zero for my types.
I need the complete memory-layout recursively for each class and all nested types. Including sizes/offsets.
Maybe the AST is the wrong place, and i need a other hook to start?
One way would be to use the "AST Record Layout" of a given const clang::CXXRecordDecl* decl in llvm/clang-3.4:
const clang::ASTRecordLayout &typeLayout(decl->getASTContext().getASTRecordLayout(decl));
std::cout << "record '" << decl->getQualifiedNameAsString() << "' with " << typeLayout.getSize().getQuantity() << "bytes\n";
for(clang::RecordDecl::field_iterator fit = decl->field_begin(); fit != decl->field_end(); fit++) {
const clang::QualType qualType = fit->getType().getLocalUnqualifiedType().getCanonicalType();
size_t fieldOffset = typeLayout.getFieldOffset(fit->getFieldIndex());
std::cout << "member '" << qualType.getAsString() << "' with " << fieldOffset/8 << " bytes offset\n";
}
no warranties: Code copied together, not tested as typed here -- but should work... (tm)
Example:
struct EXAMPLE
{
char a;
int b;
long c;
long long d;
float e;
double f;
};
Output:
record 'EXAMPLE' with 40 bytes
member 'char' with 0 bytes offset
member 'int' with 4 bytes offset
member 'long' with 8 bytes offset
member 'long long' with 16 bytes offset
member 'float' with 24 bytes offset
member 'double' with 32 bytes offset
for more see:
https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html
https://clang.llvm.org/doxygen/classclang_1_1ASTRecordLayout.html