admin管理员组文章数量:1418709
I have ran into an issue that has made me realise I don't know as much about pointers as I thought I did.
test.h
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
void getDataAlt(char** dst);
#endif // TEST_H_INCLUDED
test.c
#include "test.h"
void getDataAlt(char** dst) {
char* tmp = *dst;
tmp = malloc(sizeof(char) * 50);
sprintf(tmp, "This is a test.\r\n");
*dst = tmp;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "test.h"
#define enableOther
void getData(char** dst);
int main()
{
char* anOther;
getData(&anOther);
printf("Hello %s\n", anOther);
free(anOther);
#ifdef enableOther
char * anOtherAlt;
getDataAlt(&anOtherAlt);
printf("Hello Alt %s\n", anOtherAlt);
free(anOtherAlt);
#endif
return 0;
}
void getData(char** dst) {
char* tmp = *dst;
tmp = malloc(sizeof(char) * 50);
sprintf(tmp, "This is a test.\r\n");
*dst = tmp;
}
This is my code. I am trying to let the function do the allocating etc. and modify the char*
that is passed to it.
The local getData
works without issue, the one inside the seperate .h /.c file works up till the point of the free(getDataAlt)
and it spits out a "double free" error, and I can't for the life of me figure it out.
Running code blocks 20.03 on windows 11, MingW compiler version 8.1.0
New Screenshots:
I have ran into an issue that has made me realise I don't know as much about pointers as I thought I did.
test.h
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
void getDataAlt(char** dst);
#endif // TEST_H_INCLUDED
test.c
#include "test.h"
void getDataAlt(char** dst) {
char* tmp = *dst;
tmp = malloc(sizeof(char) * 50);
sprintf(tmp, "This is a test.\r\n");
*dst = tmp;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "test.h"
#define enableOther
void getData(char** dst);
int main()
{
char* anOther;
getData(&anOther);
printf("Hello %s\n", anOther);
free(anOther);
#ifdef enableOther
char * anOtherAlt;
getDataAlt(&anOtherAlt);
printf("Hello Alt %s\n", anOtherAlt);
free(anOtherAlt);
#endif
return 0;
}
void getData(char** dst) {
char* tmp = *dst;
tmp = malloc(sizeof(char) * 50);
sprintf(tmp, "This is a test.\r\n");
*dst = tmp;
}
This is my code. I am trying to let the function do the allocating etc. and modify the char*
that is passed to it.
The local getData
works without issue, the one inside the seperate .h /.c file works up till the point of the free(getDataAlt)
and it spits out a "double free" error, and I can't for the life of me figure it out.
Running code blocks 20.03 on windows 11, MingW compiler version 8.1.0
New Screenshots:
Share Improve this question edited Jan 31 at 14:56 John Bollinger 183k11 gold badges96 silver badges189 bronze badges asked Jan 31 at 10:43 BagOfTricksBagOfTricks 134 bronze badges 24 | Show 19 more comments1 Answer
Reset to default 2There's a bug in the leak checker you're using.
If I run this code (with the leak checker) under valgrind I get the following output:
==19372== Memcheck, a memory error detector
==19372== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19372== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==19372== Command: ./main
==19372==
Hello This is a test.
Hello Alt This is a test.
WARNING:: (main.c:20) Double free detected
/*========= SUMMARY =========*/
Total allocations 1
Total Free 2
Total Memory allocated 50 bytes
Total Memory freed 50 bytes
Memory Leaked 0 bytes
==19372==
==19372== HEAP SUMMARY:
==19372== in use at exit: 0 bytes in 0 blocks
==19372== total heap usage: 2 allocs, 2 frees, 100 bytes allocated
==19372==
==19372== All heap blocks were freed -- no leaks are possible
==19372==
==19372== For lists of detected and suppressed errors, rerun with: -s
==19372== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
It's clear both from looking at the code and the valgrind output that there's no double-free going on. So the leak checker is buggy.
本文标签: C pointers understandingStack Overflow
版权声明:本文标题:C pointers understanding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745267166a2650671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
free(getDataAlt);
it is no surprise it crashes --getDataAlt
is a function pointer, not a pointer to heap allocated memory. However, the code you show doesn't have that problem and should work fine. Your issue may be that you're not compiling what you think you are. Maybe you have an old test.o you're getting and not recompiling test.c – Chris Dodd Commented Jan 31 at 10:59stdio.h
. Usingmalloc
without including that header should cause a warning in your compiler for implicitely declaring a function. Then, the compiler assumesmalloc
would return anint
which is not correct. – Gerhardh Commented Jan 31 at 11:00tmp
to*dst
ingetDataAlt
andgetData
when the code assigns a new value totmp
immediately afterwards. – Ian Abbott Commented Jan 31 at 11:10stdlib.h
formalloc
, but it does also needstdio.h
forsprintf
. – Ian Abbott Commented Jan 31 at 11:16