User forums > Using Code::Blocks

Need help displaying data in assembly language

<< < (3/3)

Ray Seyfarth:

--- Quote from: oBFusCATed on December 07, 2011, 05:36:00 pm ---
--- Quote from: Ray Seyfarth on December 07, 2011, 04:55:57 pm ---Even in C/C++ it can be handy to give names to areas of memory.  This is more critical if there is more than 1 such area.  I can imagine that there might be cases when you might not have a local or global variable giving convenient access to an array of data.  The data could be accessible through a moderate amount of indirection and giving convenient access would be nice.  It would also be nice to allow naming these arrays or variables in case someone needs to track multiple variables.  It should be a minor matter to add names to the new capability if done at the time it is created.

--- End quote ---
I don't understand how this is related to C/C++.
Still waiting for the c/c++ and gdb commands, btw.

--- End quote ---

Here is a sample C++ program:

#include <iostream>

using namespace std;

class Data
{
    public:
        double *x;
        double *y;
        int n;
};

main()
{
    Data data;
    int i;

    data.n = 10;

    data.x = new double[10];
    data.y = new double[10];

    for ( i = 0;  i < data.n; i++ ) {
        cin  >>  data.x  >>  data.y;
    }

    return 0;
}

Here's a fragment from a gdb session:

(gdb) b main
Breakpoint 1 at 0x4006fd: file cpptest.cpp, line 18.
(gdb) r
Starting program: /home/seyfarth/cpptest/a.out

Breakpoint 1, main () at cpptest.cpp:18
18          data.n = 10;
(gdb) n
20          data.x = new double[10];
(gdb)
21          data.y = new double[10];
(gdb)
23          for ( i = 0;  i < data.n; i++ ) {
(gdb) n
24              cin  >>  data.x  >>  data.y;
(gdb) n
1 5
23          for ( i = 0;  i < data.n; i++ ) {
(gdb) p/x data.x
$1 = 0x602010
(gdb) p/x data.y
$2 = 0x602030
(gdb) x/4fg 0x602010
0x602010:       1       0
0x602020:       0       1.6304166312761136e-322
(gdb) x/4fg 0x602030
0x602030:       5       0
0x602040:       0       4.001931731314097e-322
(gdb)

In this program the data is doubles so the examine command needs "fg" after the number of items to dump.  For general use there needs to be a way to specify the number of items, the format and the size.

This program is very simple, but there are 2 arrays.  It would also be possible to use "x/4fg data.x".  As things get more complicated the expression to get to the address could be confusing.  With 2 arrays it would be nice to allow a label for the arrays to clarify the intent.

Thanks

Ray

zabzonk:
Possibly you meant this, but maybe you are not aware that:

     data.x = new double(10);

does not dynamically allocate an array of 10 elements - it allocates a single double and initialises it to the value 10.

Ray Seyfarth:

--- Quote from: Neil Butterworth on December 07, 2011, 08:55:17 pm ---Possibly you meant this, but maybe you are not aware that:

     data.x = new double(10);

does not dynamically allocate an array of 10 elements - it allocates a single double and initialises it to the value 10.

--- End quote ---

Hi Neil!

Thanks for the correction.  I wrote this quickly to show how a more sophisticated memory dump could be useful and should have used [].  I have modified the original post in case this gets used.

Ray

Navigation

[0] Message Index

[*] Previous page

Go to full version