Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Question

Name: Anonymous 2011-12-02 13:18

Consider the two snipits used to make a struct


//T1
struct b *create_b(...)
{
  struct b *a = malloc(sizeof(struct b));
  ...
  return a;
}

//T2
struct b *create_b(...)
{
  struct b a;
  ...
  return &a;
}


would T2 produce memory errors later on in a program if used a lot? How would it know to when to cleanup a?

Name: Anonymous 2011-12-02 13:20

T2 is undefined behaviour. Never return the address of a non-persistant (static) local variable.

Name: >>2 2011-12-02 13:22

To be more precise: in T2, a doesn't exist anymore after the end of the function and the returned pointer is thus invalid.

Name: Anonymous 2011-12-02 13:23

Use of persistent local variables considered fucking evil.

Name: Anonymous 2011-12-02 13:32

>>4
Why is static evil?

Name: Anonymous 2011-12-02 14:36

>>5
Clarity, threading.

Name: Anonymous 2011-12-02 17:56

>>5
all you're allowed to use is if, for, while and functions.
it keeps the code easy to understand and ensures you're cheaply replaceable.

Name: Anonymous 2011-12-02 18:12

>>7
cretin detected

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List