free causes core-dump

L

loudking

Hi, all

Here is part of my code.

========================================
void *record;

/* treat record */

if (record) {
fprintf(stderr, "free record start\n");
fprintf(stderr, "char= %s\n", (char*)record);
fprintf(stderr, "hex= %x\n", (u_int)record);
free(record);
fprintf(stderr, "free record end\n");
}
========================================

The output is

========================================
free record start
char= @Ýa X¨-
hex= 92d73e8
*** glibc detected *** double free or corruption (!prev): 0x092d73e8
***
zsh: 18957 abort (core dumped) ./zizzy system/manager
==========================================

Could anybody tell me what is the reason for core-dump?

I already checked if record is empty ....
 
J

Joachim Schmitz

loudking said:
Hi, all

Here is part of my code.

========================================
void *record;

/* treat record */

if (record) {
fprintf(stderr, "free record start\n");
fprintf(stderr, "char= %s\n", (char*)record);
fprintf(stderr, "hex= %x\n", (u_int)record);
free(record); record=NULL;

fprintf(stderr, "free record end\n");
}
========================================

The output is

========================================
free record start
char= @Ýa X¨-
hex= 92d73e8
*** glibc detected *** double free or corruption (!prev): 0x092d73e8
***
zsh: 18957 abort (core dumped) ./zizzy system/manager
==========================================

Could anybody tell me what is the reason for core-dump?

I already checked if record is empty ....
But you didn't set it to NULL after the first free, did you?

Bye, Jojo
 
J

James Kuyper

loudking said:
Hi, all

Here is part of my code.

========================================
void *record;

/* treat record */

if (record) {
fprintf(stderr, "free record start\n");
fprintf(stderr, "char= %s\n", (char*)record);
fprintf(stderr, "hex= %x\n", (u_int)record);
free(record);
fprintf(stderr, "free record end\n");
}
========================================

The output is

========================================
free record start
char= @Ýa X¨-
hex= 92d73e8
*** glibc detected *** double free or corruption (!prev): 0x092d73e8
***
zsh: 18957 abort (core dumped) ./zizzy system/manager
==========================================

Could anybody tell me what is the reason for core-dump?

As usual, when you post only the part of your program that you think
contains the error, you're generally wrong.
You shouldn't call free() unless you've first called malloc() or
calloc() or realloc(), but I don't see that call in your code. The error
message implies that there was a previous call free() for the same
pointer value, but I don't see that previous call to free(). We need at
least a larger code sample to diagnose this problem. A complete program
of minimum size that reliably demonstrates the problem would be even better.

From past experience, I know that when you have problems like this with
dynamically allocated memory, those problems are often caused by a part
of the code that has nothing to do with the part of the code where the
symptoms first become fatal. Look for buffer overruns, attempts to write
to or read from memory that has already been deallocated, and other
similar possibilities.
 
K

Keith Thompson

loudking said:
Here is part of my code.

========================================
void *record;

/* treat record */

if (record) {
fprintf(stderr, "free record start\n");
fprintf(stderr, "char= %s\n", (char*)record);
fprintf(stderr, "hex= %x\n", (u_int)record);
free(record);
fprintf(stderr, "free record end\n");
}
========================================
[...]

See question 7.21 in the comp.lang.c FAQ, <http://c-faq.com/>.

Also, your method of displaying the address of your pointer is
incorrect. The "char=" line converts the pointer to char* and tells
fprintf() to assume that the resulting pointer points to a string, and
to display the value of that string (this doesn't display the
address). Judging by the output you got, record probably points to
some kind of binary data, not a string.

The hex version converts the void* value to u_int and prints it using
"%x". (Presumably u_int is unsigned int; why not just use "unsigned
int" so your readers don't have to guess?) But conversion of a
pointer to an integer type, though it's allowed doesn't necessarily
give you a meaningful result.

Fortunately, fprintf() provides a format for just this purpose: "%p".

To print a pointer value (other than a function pointer):

fprintf(stderr, "record=%p\n", record);

"%p" requires a void* pointer value. In this case, record is already
of that type. If it weren't, you'd need to cast it:

int x;
fprintf(stderr, "&x = %p\n", (void*)&x);
 
K

Keith Thompson

Keith Thompson said:
loudking said:
Here is part of my code.

========================================
void *record;

/* treat record */

if (record) {
fprintf(stderr, "free record start\n");
fprintf(stderr, "char= %s\n", (char*)record);
fprintf(stderr, "hex= %x\n", (u_int)record);
free(record);
fprintf(stderr, "free record end\n");
}
========================================
[...]

See question 7.21 in the comp.lang.c FAQ, <http://c-faq.com/>.

Also, your method of displaying the address of your pointer is
incorrect.
[...]

Correction: I should have written "the *value* of your pointer", not
its address.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top