Debug memory !

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Mathieu

    #1

    Debug memory !

    Hi !

    I have a program with almost 100 sources.

    The program coredump so i want debug the memory and i try memwatch but i
    don't get the result i want.

    The program crash so memwatch don't create the log's file. Also, memwatch
    just tell you how many memory allocate et free.

    If someone know some tips or program to help me, i will greatly appreciate.

    In my program, i have some declaration like this : *ptr = "abcdef" and
    before any malloc the program try to change de string "abcdef" to "bbcdef".
    But with cc compiler this command is ok. But with gcc compiler i got a
    coredump. But isn't the original coredump i want to debug. And i must use
    gcc for use memwatch.

    So it's very difficult to debug ... any help please !

    P.S : Excuse me for my english, it's not my first language !

    Thank !


  • CBFalconer

    #2
    Re: Debug memory !

    Eric Mathieu wrote:[color=blue]
    >[/color]
    .... snip ...[color=blue]
    >
    > In my program, i have some declaration like this : *ptr = "abcdef"
    > and before any malloc the program try to change de string "abcdef"
    > to "bbcdef". But with cc compiler this command is ok. But with gcc
    > compiler i got a coredump. But isn't the original coredump i want
    > to debug. And i must use gcc for use memwatch.
    >
    > So it's very difficult to debug ... any help please ![/color]

    That declaration make ptr point to an unmodifiable string. You
    would be better off declaring it as "const char *ptr =
    "abcdef";". Then you will be warned about misuse if you have a
    suitable set of options set for gcc, such as "-W -Wall -ansi -
    pedantic". If you also add -Wwrite-strings you will also be
    warned about the poor declaration.

    If you truly want an initialized but modifiable string there, you
    could declare:

    static char foo[] = "abcdef";
    char *ptr = & foo[0];

    and foos size can be discovered by either:

    1 + strlen(foo) /* before any modification */
    or
    sizeof foo /* at the declaration scope */

    --
    Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home .att.net> USE worldnet address!

    Comment

    • Darrell Grainger

      #3
      Re: Debug memory !

      On Sun, 20 Jun 2004, Eric Mathieu wrote:
      [color=blue]
      > Hi !
      >
      > I have a program with almost 100 sources.
      >
      > The program coredump so i want debug the memory and i try memwatch but i
      > don't get the result i want.
      >
      > The program crash so memwatch don't create the log's file. Also, memwatch
      > just tell you how many memory allocate et free.
      >
      > If someone know some tips or program to help me, i will greatly appreciate.[/color]

      Not really a question about the C programming language. A general trick
      for debugging a crash is to insert printf statements. Run the code and
      look at the output. From the printf statements you should be able to
      narrow down where the crash is occurring. Keep adding an dremove printf
      statements until you narrow it down to a small block of code.

      Anything better than that will depend on the tools available for your
      implementation. That is definitely off-topic for this newsgroup.
      [color=blue]
      > In my program, i have some declaration like this : *ptr = "abcdef" and
      > before any malloc the program try to change de string "abcdef" to "bbcdef".
      > But with cc compiler this command is ok. But with gcc compiler i got a
      > coredump. But isn't the original coredump i want to debug. And i must use
      > gcc for use memwatch.[/color]

      Here is the bug. Just because one compiler (cc) lets you do something does
      not mean it is correct. Modifying a string literal has undefined
      behaviour. Don't do that. Try:

      char ptr[] = "abcdef";

      This is an array of char that has been initialized with the data "abcdef".
      This is something that you can modify, always.
      [color=blue]
      > So it's very difficult to debug ... any help please !
      >
      > P.S : Excuse me for my english, it's not my first language !
      >
      > Thank !
      >
      >
      >[/color]

      --
      Send e-mail to: darrell at cs dot toronto dot edu
      Don't send e-mail to vice.president@ whitehouse.gov

      Comment

      Working...