Alright, the issue I'm having is figuring out whether or not I'm causing memory leaks. I'm considering three situations which might or might not be the same:

1) If I declare a variable in a block of code as a character array, I'm making a pointer to it. But when the block ends, that pointer is lost. Am I to assume the memory associated with its data is also freed?

2) On the other hand, if I declare a pointer to a char and fill it with a string will the situation be the same?

3) And finally, well I can't explain the last situation in any way that code can't. I'm posting code snippets to these three situations. Can someone tell me if any of them cause memory leaks? Thanks.

1)

{
    char my_string[20];
    // Do something with this string, fill it with some stuff
}

I'm quite sure that one doesn't cause memory leaks. So is it different than situation 2 and if so does situation 2 cause leaks?

2)

{
    char* my_string = "I presume this is dynamically allocated.";
    // Once again, use the string for various purposes
}

And finally, this is the one that's really worrying me but I realized it isn't so different from situation 2.

The snippet is from the code I'm currently working on. Some of the initializations are cut out because they are involved in unrelated processes. The code actually works so no need to try to correct it, I just need to know about memory leaks (although if anyone has better ideas on how to do this that's fine too).

3)

char* user_name = NULL;

// Here there is the possibility of user input assigning a string to user_name
// Also size_user_name is initialized and given the correct value if user_name is given

{
    // Default name for images is screen_# if nothing else is given
    if (user_name == NULL) {
        user_name = "screen";
        size_user_name = 6;
    }

    // Calculate the number of digits in the time stamp, log 0 = -inf
    if (time_stamp >= 10)
        size_time = (int) log10(time_stamp) + 1;
    else
        size_time = 1;
    // Set aside enough space to write the output name (+ 1 for terminator)
    char* output_name = malloc(size_user_name + size_time + 2);

    // Generate the output name
    sprintf(output_name, "%s_%d.ppm", user_name, time(NULL) - init_time);
}

// Is the output name free at this point or do I need to free it?

Thanks in advance

The golden rule is thusly: If you call malloc, calloc, or realloc, you must call free on the pointer returned or risk a memory leak.

> 1) If I declare a variable in a block of code as a character array, I'm making a pointer to it.
You are making an array, not a pointer. Only when the array name is used in an expression will it evaluate to a pointer, but even then it is not a pointer to dynamic memory. There is no leak.

> 2) On the other hand, if I declare a pointer to a char and fill it with a string will the situation be the same?
String literals are not dynamic either. There is no leak.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.