C pointers understanding - Stack Overflow

I have ran into an issue that has made me realise I don't know as much about pointers as I thought

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
  • 1 If you are calling 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:59
  • 3 In test.c you do not include stdio.h. Using malloc without including that header should cause a warning in your compiler for implicitely declaring a function. Then, the compiler assumes malloc would return an int which is not correct. – Gerhardh Commented Jan 31 at 11:00
  • 3 There is no point initializing tmp to *dst in getDataAlt and getData when the code assigns a new value to tmp immediately afterwards. – Ian Abbott Commented Jan 31 at 11:10
  • 2 @Gerhardh I think you meant stdlib.h for malloc, but it does also need stdio.h for sprintf. – Ian Abbott Commented Jan 31 at 11:16
  • 1 What happens when the project is compiled like recommended here: What compiler options are recommended for beginners learning C? – Lundin Commented Jan 31 at 11:25
 |  Show 19 more comments

1 Answer 1

Reset to default 2

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745267166a4619526.html

相关推荐

  • C pointers understanding - Stack Overflow

    I have ran into an issue that has made me realise I don't know as much about pointers as I thought

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信