free() allocated memory twice..

O

ormax3

... is it an undefined behaviour?? You would expect this code to this
code ti give a segmentation fault but it doesnt.. Why??

Sample Code:
---------------
typedef struct {
char name[10];
int x;
} Employee;

int main()
{
// allocate and fill e1
Employee **e1 = (Employee**) malloc( 5 * sizeof(Employee*) );
for(i=0 ; i<5 ; ++i) {
e1 = (Employee*) malloc( sizeof(Employee) );
fillEmployee(e1);
}

// make shallow copy
Employee **e2 = (Employee**) malloc( 5 * sizeof(Employee*) );
for(i=0 ; i<5 ; ++i) {
e2 = e1;
}

// free the copy
for(i=0 ; i<5 ; ++i) {
free(e2);
}
free(e2);
e2 = NULL;

// free the original one
for(i=0 ; i<5 ; ++i) {
free(e1);
}
free(e1);
e1 = NULL;

return 0;
}


Thanks
 
K

Kai-Uwe Bux

(e-mail address removed) wrote:

[about: free() allocated memory twice...]
.. is it an undefined behaviour??
Yes.

You would expect this code to this
code ti give a segmentation fault but it doesnt.. Why??

Because the behavior is _undefined_. That means, all your expectations can
be off.

Sample Code:
---------------
typedef struct {
char name[10];
int x;
} Employee;

int main()
{
// allocate and fill e1
Employee **e1 = (Employee**) malloc( 5 * sizeof(Employee*) );
for(i=0 ; i<5 ; ++i) {
e1 = (Employee*) malloc( sizeof(Employee) );
fillEmployee(e1);
}

// make shallow copy
Employee **e2 = (Employee**) malloc( 5 * sizeof(Employee*) );
for(i=0 ; i<5 ; ++i) {
e2 = e1;
}

// free the copy
for(i=0 ; i<5 ; ++i) {
free(e2);
}
free(e2);
e2 = NULL;

// free the original one
for(i=0 ; i<5 ; ++i) {
free(e1);
}
free(e1);
e1 = NULL;

return 0;
}


BTW: asside from exhibiting undefined behavior, the code is not idiomatic.
You may want to consider the transition to std::string, std::vector, value
semantics, and if needed, new and delete.


Best

Kai-Uwe Bux
 
I

Ian Collins

... is it an undefined behaviour?? You would expect this code to this
code ti give a segmentation fault but it doesnt.. Why??

Sample Code:
---------------
typedef struct {
char name[10];
int x;
} Employee;

int main()
{
// allocate and fill e1
Employee **e1 = (Employee**) malloc( 5 * sizeof(Employee*) );
for(i=0 ; i<5 ; ++i) {
e1 = (Employee*) malloc( sizeof(Employee) );
fillEmployee(e1);
}

Why are are you using malloc? Wouldn't a std::vector of Employee* be
more appropriate?
 
J

Jorgen Grahn

(e-mail address removed) wrote: .... ....
Why are are you using malloc? Wouldn't a std::vector of Employee* be
more appropriate?

Topic drift, but I cannot help myself:

Where do all these people come from? I would expect *some* inflow of C
programmers to C++, but it's so bad that I'm mildly /surprised/
whenever a new poster here uses std::string or std::vector -- things
which you learn to use in the first few chapters of any decent C++
book, things which have been standard for over a decade.

I can understand how someone doesn't have access to modern
information, or has no time to learn before some deadline
.... but they are so many, and they make life so hard for
themselves!

/Jorgen
 
O

ormax

(e-mail address removed) wrote:

[about: free() allocated memory twice...]
.. is it an undefined behaviour??
Yes.

You would expect this code to this
code ti give a segmentation fault but it doesnt.. Why??

Because the behavior is _undefined_. That means, all your expectations can
be off.


Sample Code:
---------------
typedef struct {
 char name[10];
 int x;
} Employee;
int main()
{
 // allocate and fill e1
 Employee **e1 = (Employee**) malloc( 5 * sizeof(Employee*) );
 for(i=0 ; i<5 ; ++i) {
  e1 = (Employee*) malloc( sizeof(Employee) );
  fillEmployee(e1);
 }

 // make shallow copy
 Employee **e2 = (Employee**) malloc( 5 * sizeof(Employee*) );
 for(i=0 ; i<5 ; ++i) {
  e2 = e1;
 }

 // free the copy
 for(i=0 ; i<5 ; ++i) {
  free(e2);
 }
 free(e2);
 e2 = NULL;

 // free the original one
 for(i=0 ; i<5 ; ++i) {
  free(e1);
 }
 free(e1);
 e1 = NULL;

 return 0;
}

BTW: asside from exhibiting undefined behavior, the code is not idiomatic..
You may want to consider the transition to std::string, std::vector, value
semantics, and if needed, new and delete.

Best

Kai-Uwe Bux


Thanks. Should I expect the same behavior if we used new and delete?
Also can we really make a similar shallow copying using std::vector's?
 
D

David Connet

Topic drift, but I cannot help myself:

Where do all these people come from? I would expect *some* inflow of C
programmers to C++, but it's so bad that I'm mildly /surprised/
whenever a new poster here uses std::string or std::vector -- things
which you learn to use in the first few chapters of any decent C++
book, things which have been standard for over a decade.

I can understand how someone doesn't have access to modern
information, or has no time to learn before some deadline
... but they are so many, and they make life so hard for
themselves!

I always look at the calendar - Feb? Well that's fairly close to the start
of a semester... (string and vector must come in the next part of the
class)

Dave Connet
 
D

Default User

Jorgen said:
Where do all these people come from? I would expect some inflow of C
programmers to C++, but it's so bad that I'm mildly surprised
whenever a new poster here uses std::string or std::vector -- things
which you learn to use in the first few chapters of any decent C++
book, things which have been standard for over a decade.

Unfortunately, far too many instructors still use outdated techniques
and reference materials.




Brian
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top