Please see how gdb fails to print the global variable dictionary in the code below:
m#m-X555LJ ~ $ cat 148.cc
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <utility>
#include <algorithm>
#include <sstream>
using namespace std;
using letters = array<int, 26>;
letters emptylet = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
vector<pair<string, letters>> dictionary;
void backtrace(string &orig, vector<string> &phrase, vector<string> &acc, letters &curr, letters &goal, decltype(dictionary)::iterator it) {
if(it == dictionary.end())
return;
for(int i = 0; i < it->second.size(); i++)
curr[i] += it->second[i];
acc.push_back(it->first);
bool all_same = true;
for(int i = 0; i < curr.size(); i++)
if(curr[i] > goal[i])
goto clean;
else if(curr[i] != goal[i])
all_same = false;
if(all_same) {
if(acc != phrase) {
cout << orig << " =";
for(string word: acc)
cout << ' ' << word;
cout << '\n';
}
} else
backtrace(orig, phrase, acc, curr, goal, it+1);
clean:
acc.pop_back();
for(int i = 0; i < it->second.size(); i++)
curr[i] -= it->second[i];
backtrace(orig, phrase, acc, curr, goal, it+1);
}
int main()
{
while((cin>>ws).peek()!='#') {
string word;
cin >> word;
letters let = emptylet;
for(char c: word)
let[c-'A']++;
dictionary.push_back(make_pair(word, let));
}
while((cin>>ws).peek()!='#') {
string s;
getline(cin, s);
string orig = s;
stringstream ss(s+'\n');
vector<string> phrase;
while(cin>>s){
phrase.push_back(s);
}
sort(phrase.begin(), phrase.end());
letters let = emptylet;
for(string wrd: phrase)
for(char c: wrd)
let[c-'A']++;
vector<string> empt;
backtrace(orig, phrase, empt, emptylet, let, dictionary.begin());
}
}
m#m-X555LJ ~ $ g++ -g -std=c++11 -o 148 148.cc
m#m-X555LJ ~ $ cat 148.in
ABC
AND
DEF
DXZ
K
KX
LJSRT
LT
PT
PTYYWQ
Y
YWJSRQ
ZD
ZZXY
#
ZZXY ABC DEF
SXZYTWQP KLJ YRTD
ZZXY YWJSRQ PTYYWQ ZZXY
#
m#m-X555LJ ~ $ gdb ./148
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./148...done.
(gdb) break 52
Breakpoint 1 at 0x401d0f: file 148.cc, line 52.
(gdb) r < 148.in
Starting program: /home/m/148 < 148.in
Breakpoint 1, main () at 148.cc:52
52 while((cin>>ws).peek()!='#') {
(gdb) p dictionary
No symbol "dictionary" in current context.
(gdb) p di
dictionary[abi:cxx11] dir_data dirfd dirstream.h div divmod_1.c
difftime dirent dirfd.c disallow_malloc_check div.c divrem.c
difftime.c dirent.h dirname distinguish_extX div_t
digits_dots.c dirent64 dirname.c distinguish_extX.isra.0 divide
(gdb) p dictionary[abi:cxx11]
No symbol "dictionary" in current context.
(gdb) quit
A debugging session is active.
Inferior 1 [process 3815] will be killed.
Quit anyway? (y or n) y
m#m-X555LJ ~ $ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
m#m-X555LJ ~ $
I do not understand this. Why is this the case? Is there something obvious about gdb I do not know?
To my best understanding, p dictionary should print the contents of the std::vector global variable called dictionary.
And this is not the first time something like that happens to me. I remember gdb not seeing functions, variables, etc. This time I decided to record the case.
You can see the problem when you use p ditabtab (as you did) -- the symbol produced by gcc is called "dictionary[abi:cxx11]", which you can print by entering p 'dicttab -- note the quotes around the name to get gdb to not try to interpret the [..] as an expression.
Related
This question already has an answer here:
Ambiguous overload when writing an enum with an enum-base, but only with clang
(1 answer)
Closed 7 years ago.
Quite simple. Why does this happen? It works fine will all other integer types.
enum : unsigned short {
ushort_min = static_cast<unsigned short>(0u),
ushort_max = static_cast<unsigned short>(~0u)
};
enum : short {
short_min = short(ushort_max << (sizeof(short) * char_bit - 1)),
short_max = short(ushort_max >> 1)
};
std::cout << short_min; // BOOM
The sample code compiles without errors with gcc. The answer here is, probably, "upgrade your compiler":
$ cat t.C
#include <iostream>
enum : unsigned short {
ushort_min = static_cast<unsigned short>(0u),
ushort_max = static_cast<unsigned short>(~0u)
};
enum : short {
short_min = short(ushort_max << (sizeof(short) * 8 - 1)),
short_max = short(ushort_max >> 1)
};
int main()
{
std::cout << short_min; // BOOM
}
$ g++ -std=c++14 -c -o t.o t.C
$ g++ --version
g++ (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
int main()
{
typedef unsigned char a4[4];
a4 p1;
a4& p2 = p1;
p2[1]=1;
cout<<sizeof(p2);
return p2[1];
}
Compile, start gdb and put breakpoint on return. If you type p sizeof(p2), gdb will print 8 instead of 4 which will be printed if you start the program. If you write in gdb p sizeof(*p2), the output is 4 (the size of array). I think this is because gdb treats p2 as pointer(reference is implemented behind the scene as pointer).
Tested with compilers GCC 4.8.2 and Clang 4.3 on GDB 7.7 linux arch., ubuntu 13.10,
Is this correct or a bug in gdb?
Here's a modified version of your program. I've changed the array size from 4 to 17 to ensure that its size is distinguishable from anything else. I've also changed the type and variable names to make the code easier to follow, and added #include <iostream> so it actually compiles. I've also removed some unnecessary stuff.
#include <iostream>
int main()
{
typedef unsigned char char17[17];
char17 arr17;
char17& arr17_ref = arr17;
std::cout << "sizeof(arr17) = "
<< sizeof arr17
<< ", sizeof(arr17_ref) = "
<< sizeof(arr17_ref)
<< "\n";
return 0;
}
When I compile and run it on my system, the output is 17.
When I run it under gdb, I get 8 (the size of a pointer on my system):
$ gdb ./c
GNU gdb (GDB) 7.5-ubuntu
[snip]
Reading symbols from /home/kst/c...done.
(gdb) b 12
Breakpoint 1 at 0x40097e: file c.cpp, line 12.
(gdb) r
Starting program: /home/kst/c
sizeof(arr17) = 17, sizeof(arr17_ref) = 17
Breakpoint 1, main () at c.cpp:12
12 return 0;
(gdb) p sizeof(arr17)
$1 = 17
(gdb) p sizeof(arr17_ref)
$2 = 8
(gdb) c
Continuing.
[Inferior 1 (process 23420) exited normally]
(gdb) q
$
Yes, this is a bug in gdb. gdb is supposed to evaluate expressions as they'd be evaluated in a running program; in this case, it fails to do so.
(I'm using gcc 4.7.2 and gdb 7.5 on Linux Mint 14.)
UPDATE :
The OP submitted a bug report: https://sourceware.org/bugzilla/show_bug.cgi?id=16675
and it's been fixed. The patch was approved and committed 2014-04-14. I still see the bug in gdb 7.7.1, but it's fixed in 7.11.1.
I am trying to debug a C++ program but I face the below issue when trying to print the contents of the array.
I have declared a array of chars as below in the c++ program
using namespace std;
int main() {
string s;
cin >> s;
char d[s.size()];
int i=0;
int j=0;
while(j<s.size()) {
if(s[j] != '+') {
d[i++] = s[j];
}
j++;
}
}
I use the below commands to compile the above code and run gdb
g++ -g main.cpp -o main
gdb main
However when I debug and hit the below command in GDB I get the below error.
Command entered in GDB : p *d#10
No symbol "d" in current context.
Can you please let me know what I can do so that all the contents of the array is printed?
Until you run the program, there are no variables to print. For your program, you should set a breakpoint at the last line of main, and run the program. When the program stops at the breakpoint, you can then print d.
(gdb) list 15
10 while(j<s.size()) {
11 if(s[j] != '+') {
12 d[i++] = s[j];
13 }
14 j++;
15 }
16 }
(gdb) break 15
Breakpoint 1 at 0x80488b9: file x.cc, line 15.
(gdb) run
Starting program: /home/jxh/StackOverflow/gdb/a.out
1234567890
Breakpoint 1, main () at x.cc:15
15 }
(gdb) p *d#10
$2 = "1234567890"
(gdb)
Make sure to turn off compiler optimizations. This link gives a little more detail.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm making a simulation for severals queues.
I'm supposed to input number of queues in the beginning and then simulate all thoose.
Output for each queue each "round" is meantime, total number of served and size of each queue.
My program is crashing and dosen't respond.
It writes out the first queue och then crashes...
Help!
I think my calculations is wrong to but i don't know since it's all crashing.
Here it is:
#include <iostream>
#include <cstdlib>
#include <list>
#include <ctime>
#include<conio.h>
#include <time.h>
#include <stdlib.h>
#include<dos.h>
#include<windows.h>
using namespace std;
class Customer
{
public:
int servicet;
int served;
Customer()
{
servicet= rand()%150+30;
}
int getServicetime()
{
return servicet;
}
int getServed()
{
return served;
}
int decreaseServeTime()
{
servicet --;
}
};
int totServed=0;
int queues=0;
int inLine=0;
int totTime=0;
int smallestQueue=0;
int temp=0;
int ran=0;
double mean=0;
int served=0;
int serviceTime=0;
int help=0;
int sim=0;
int n=0;
using namespace std;
int main()
{
cout<<"Number of Cashiers?: "<<endl;
cin >> queues;
cout <<"How long simulation?: "<<endl;
cin >> sim;
list<Customer> *cashiers[queues];
list<Customer> *cust;
for(int i=0; i<=queues; i++)
{
cust = new list<Customer>;
cashiers[i] = cust;
}
srand(time(0));
while(n<sim)
{
Sleep(2000);
ran= rand()%4;
smallestQueue = cashiers[0] ->size();
for(int j=0; j<ran; j++)
{
for(int k=0; k<queues; k++)
{
temp = cashiers[k]->size();
if(temp<=smallestQueue)
{
smallestQueue = temp;
help=k;
}
}
Customer C;
cashiers[help]->push_back(C);
inLine++;
}
for(int i=0; i<queues; i++)
{
if(serviceTime>0)
{
serviceTime = cashiers[i]->front().getServicetime();
cashiers[i]->front().decreaseServeTime();
}
else if(serviceTime==0)
{
cashiers[i]->pop_front();
served++;
}
}
totTime++;
int cash=1;
for(int i=0; i<queues; i++)
{
if(inLine!=0)
{
cout <<"Kassa: "<<cash<<endl;
inLine = cashiers[i]->size();
mean = (totTime/inLine);
totServed +=served;
cash++;
}
cout <<inLine<<" "<<mean<<" "<<totServed<<endl;
}
n++;
}
system("pause");
}
I recommend that you use a program such as Application Verifier to find the issue that is causing the crash:
http://www.microsoft.com/en-us/download/details.aspx?id=20028
It's important that you learn how to debug your software and understand what is going on. Please run your code in a debugger (Visual Studio, Eclipse) and take a look at where it stops. If you used Application Verifier, then it will likely stop where the issue originates. Take a look at the variables and see whether they make sense. Look at whether you're accessing memory locations you shouldn't.
To use Application Verifier with Visual Studio, install it then find the appVerifier.exe in the System32 folder in C:\Windows. Then open the file and point it to your executable. Enable what you think are the proper checks. Then run it in visual Studio.
A good place to start is with a debugger (e.g. gdb). First we compile with debugging enabled (g++ -ggdb) and try running in the debugger,
$ g++ hi.cpp -ggdb
$ gdb ./a.out
GNU gdb (GDB) 7.5-ubuntu
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/ben/a.out...done.
(gdb) run
Starting program: /home/ben/a.out
Number of Cashiers?:
5
How long simulation?:
5
Kassa: 1
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401827 in std::_List_const_iterator<Customer>::operator++ (
this=0x7fffffffdd10) at /usr/include/c++/4.7/bits/stl_list.h:236
236 _M_node = _M_node->_M_next;
(gdb) backtrace
#0 0x0000000000401827 in std::_List_const_iterator<Customer>::operator++ (
this=0x7fffffffdd10) at /usr/include/c++/4.7/bits/stl_list.h:236
#1 0x0000000000401665 in std::__distance<std::_List_const_iterator<Customer> >
(__first=..., __last=...)
at /usr/include/c++/4.7/bits/stl_iterator_base_funcs.h:82
#2 0x0000000000401492 in std::distance<std::_List_const_iterator<Customer> > (
__first=..., __last=...)
at /usr/include/c++/4.7/bits/stl_iterator_base_funcs.h:118
#3 0x000000000040135b in std::list<Customer, std::allocator<Customer> >::size
(this=0x604010) at /usr/include/c++/4.7/bits/stl_list.h:855
#4 0x0000000000401122 in main () at hi.cpp:125
Here we see that the program crashed with a segmentation fault in a
function of the std::list. After programming for a while, you will
gain an intuition that this is probably due to your program trampling
on some memory it shouldn't be. Having identified roughly the nature
of the problem, we will now switch to valgrind, a tool for tracking
down specifically this sort of issue.
$ valgrind ./a.out
==13751== Memcheck, a memory error detector
==13751== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==13751== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==13751== Command: ./a.out
==13751==
Number of Cashiers?:
5
How long simulation?:
5
Kassa: 1
==13751== Invalid read of size 8
==13751== at 0x401422: std::list<Customer, std::allocator<Customer> >::begin() const (stl_list.h:749)
==13751== by 0x40134F: std::list<Customer, std::allocator<Customer> >::size() const (stl_list.h:855)
==13751== by 0x401121: main (hi.cpp:125)
==13751== Address 0x5a06040 is 0 bytes inside a block of size 16 free'd
==13751== at 0x4C2A44B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13751== by 0x4018E7: __gnu_cxx::new_allocator<std::_List_node<Customer> >::deallocate(std::_List_node<Customer>*, unsigned long) (new_allocator.h:100)
==13751== by 0x4017D9: std::_List_base<Customer, std::allocator<Customer> >::_M_put_node(std::_List_node<Customer>*) (stl_list.h:339)
==13751== by 0x4015C0: std::list<Customer, std::allocator<Customer> >::_M_erase(std::_List_iterator<Customer>) (stl_list.h:1549)
==13751== by 0x4013E9: std::list<Customer, std::allocator<Customer> >::pop_front() (stl_list.h:983)
==13751== by 0x40108B: main (hi.cpp:113)
==13751==
==13751==
==13751== Process terminating with default action of signal 8 (SIGFPE)
==13751== Integer divide by zero at address 0x402CCCE98
==13751== at 0x40113C: main (hi.cpp:126)
Here we see valgrind telling us that your program attempted a read
operation on unallocated memory. In particular, this seems to be
happening as a result of a pop_front operation. Looking at the
source, you indeed attempt to pop from cashiers[i] without first
checking it's size.
We can add an appropriate check,
...
else if(serviceTime==0)
{
if (!cashiers[i]->empty()) {
cashiers[i]->pop_front();
served++;
}
}
...
The actual cause of the crash, however, is a division by zero in computing the
mean, as noted at the end of valgrind's output. This is due to the fact that the case where no Customers are inLine is not handled when computing mean.
I am facing problem While using watch in gdb . I am trying to keep a
watch on variable m in my code . But for some reason i am getting the following message
no symbol m in current context. I have kept a break point at line 7 so that scope of m is known .
steps performed by me :-
1>g++ -g a.cpp
2>gdb a.out
3>(gdb)break 7
4>(gdb)watch m
Below is my program :-
# include<iostream>
# include<stdio.h>
using namespace std;
int main()
{
int m=10;
char *abc = (char *)"ritesh";
cout << abc << endl ;
m=11;
m=13;
abc=NULL;
cout << *abc <<endl;
return 0;
}
I have also seen How can I use "watch" GDB? But it did not help me much . Can someone explain this problem i am facing.Below are information related to my GNU
ritesh#ubuntu:~$ gdb a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/ritesh/a.out...done.
When you load your program into a debugger it is not being run yet. However, you try to watch a symbol, which will start to "live" in a function -- main() function -- and will "disappear" when you return from the function.
For example, in this code
void func() {
int b = 1;
++b;
cout << b << endl;
}
int main() {
int a = 1;
func();
cout << a << endl;
}
you can't set a watch of the value of a before start executing a program, and a watch on the value of b until the execution enters func().