Debug memory !

E

Eric Mathieu

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 !
 
C

CBFalconer

Eric said:
.... snip ...

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 !

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 */
 
D

Darrell Grainger

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.

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.
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top