Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memory leak on shallow copy 1

Status
Not open for further replies.

svar

Programmer
Aug 12, 2001
349
GR

Define a structure like this:
typedef struct {char *s;
int k;}mystruct;

main(){
mystruct str1,str2;
// fill str1...

...
str2=str1;

Now if one changes a character in the string s
in str1, this is also changed in str2, but does this really leak memory? I do not see any memory problems unless there is a delete, which cannot really be done, since s
is effectively an array, or am I missing something?
Also, it looks to me like the declared dimension of s should be specified somewhere, s will be using a contiguous space on
the stack and the compiler cannot reserve ALL space for s,
or am I wrong?
 
> since s is effectively an array, or am I missing something?
s isn't an array, and yes you're missing quite a lot.

First off, you can't use s with impunity as some infinitely long array which the compiler looks after for you.

It's up to you to make sure that it is set to point to some allocated memory.

Let me say that again, if you have any pointers, it's your responsibility to make sure they point somewhere. The compiler does nothing automatic for you.


Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char *s;
    int k;
} mystruct;

int main ( ) {
    mystruct str1,str2;

    str1.s = malloc(10);
    strcpy( str1.s, "hello" );
    str1.k = 22;
    printf( "%s %d\n", str1.s, str1.k );

    // This only copies the pointer
    // changes to str2 will affect str1 and vice-versa
    str2 = str1;
    strcpy( str2.s, "HELLO" );
    printf( "%s %d\n", str1.s, str1.k );
    printf( "%s %d\n", str2.s, str2.k );

    // now the killer blow
    // since they're both the same pointer, freeing one
    // also frees the other
    // str2.s pointer now points nowhere valid - a particularly hard
    // bug to find in a large program
    free( str1.s );
    return 0;
}

If on the other hand, you made s a proper array to start with, none of that would be a problem.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char s[10];
    int k;
} mystruct;

int main ( ) {
    mystruct str1,str2;

    strcpy( str1.s, "hello" );
    str1.k = 22;
    printf( "%s %d\n", str1.s, str1.k );

    str2 = str1;
    strcpy( str2.s, "HELLO" );
    printf( "%s %d\n", str1.s, str1.k );
    printf( "%s %d\n", str2.s, str2.k );

    return 0;
}

> Also, it looks to me like the declared dimension of s should be specified somewhere
Yes, in a call to malloc for example.

> s will be using a contiguous space
No - the compiler does nothing in terms of allocating memory to pointers. Like I said, its all up to you.

--
 
Yes, but the big question is is there any other way to
have a memory leak except a delete(free)?
I guess not. Unless there is a delete, things are ok
-which of course is no way to write a program
 
Well a memory leak is defined as losing your last pointer to a block of memory (since you can no longer free it if you don't have a pointer to it).

So whilst
Code:
char *p = malloc( 10 );
char *q = p;
// do lots of stuff
p = NULL;  // without q, this would be a leak
is not a memory leak (you still have a copy of the pointer in q), the fact that q is an alias creates a new set of problems.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top