1

I have this basic program:

int initfunc(int *array, int len) {
  int i;
  for(i=1; i <= len; i++) {
    array[i] = i;
  }
 return 0;
}

int main(int argc, char* argv[]){

  int*  myarray=0;
  initfunc(myarray,10);
}

First i'm trying to figure out what the command in GDB to find what memory address main is stored in.

And also my error is at line 4 (array[i] = i), i'm trying to figure out what I need to do to get it to run. My professor wrote this program so I get that using these pointers probably isn't a good way to code this basic program. I just need some insight since i'm not too great with pointers.

wade aston
  • 105
  • 3
  • 14
  • If you compile with the debug symbols on, gdb will show it out of the box... – Eugene Sh. Apr 07 '16 at 15:53
  • @EugeneSh. , You're right buddy. If you're saying GDB tells me out of the box what the error is I get that since I know it's at line 4. The pointer is why it is throwing it off is my assumption, I just wanted clarity why it is doing so. If you're telling me however out of the box that the GDB tells me the memory address of the function that is false, because it tells me the memory address of the function that returns the error which is not what I need I need the memory address for another specific function. – wade aston Apr 07 '16 at 15:59
  • 1
    You want to look at the value of `array` (where it points to), not the addresses of the functions. – mch Apr 07 '16 at 16:00
  • 1
    2 things: 1) You haven't allocated any memory for `myarray`, so your attempt to write to it is UB. 2) Once you allocate memory, C pointers are 0-indexed; your `for` loop should be `i = 0; i < len; i++` – R_Kapp Apr 07 '16 at 16:01
  • @mch, Well I just had to find out a specific memory allocation for function main for the assignment and I wasn't sure how. I set a breakpoint at it and it returns the address so I guess that's how to do it? I wasn't sure if there was a more direct command. – wade aston Apr 07 '16 at 16:03
  • @R_Kapp, Thanks dude let me look into that. – wade aston Apr 07 '16 at 16:03
  • `x/ main` will show address of the `main`, can be used for any other function – re_things Nov 24 '16 at 19:31

2 Answers2

0

before compile you must use -g for save symbol link to your execute file.

if you use gdb a.out you get error :

Reading symbols from TEMP...(no debugging symbols found)...done.

but if you use g++ -g test.cpp and now gdb show you :

Reading symbols from a.out...done.

And now you can use command-gdb

And see this : How to use GDB to find what function a memory address corresponds to

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
0

I figured it out, thanks for the insight. I just used breakpoints in GDB to figure out memory allocation for functions. I failed to mention I used -g in my makefile, so that part was already done. Also the memory allocation for the array wasn't present and that fixed the issue! Cheers

wade aston
  • 105
  • 3
  • 14