Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#Actualslicer4ever

Posted 14 February 2012 - 04:50 PM

I hope the collision course I gave on pointers at least cleared some stuff in his head Posted Image

i'm sorry, but he was doing nothing wrong in his initial code when it comes to pointers. and to be fair, he seems to have a better understanding then your "course"

for example:
int* pointer; // creates an 8-byte pointer to an integer, pointer's value is 0 (Null-pointer)
///////////This is not true, first of all, pointers generally are 4-bytes(although it isn't impossible for the architecture your working on to use 8-byte's for a pointer, generally they are only 4 bytes.)
//////////Secondly, not defining a value, does [B]NOT[/B] mean it defaults as a null pointer, it is simply undefined(do not rely on uninitialized pointers to equal 0, this is extremely bad practice.)

int value = *pointer; // ERROR: you tried to access the contents of a null-pointer, this is illegal
//again, not invalid, but is undefined behavior(generally seg-fault level, but it is not illegal by compilation to access a null pointer.)

pointer = new int; // dynamically (at runtime) assigns 4 bytes of memory at the place which is pointed by pointer, pointer is now valid
//pointer is not "now" valid, the assumption is that you were successful in creating the memory, but does not gurantee the returned data is valid(i.e: out of memory errors)

int value_2 = *pointer; // VALID: pointer actually points to somewhere, so value_2 should be either 0 or a random number, this depends on the compiler
//Yes, undefined behavior.

delete pointer; // tells the operating system to free the place pointed by pointer
//Assuming that the pointer was successfully created.

int value_3 = *pointer; // VALID: pointer still points to somewhere in the memory, and most probably you'll get back the old value pointed by pointer, since nothing has overwritten it. BUT this cannot be guaranteed (so DO NOT do this)
//Yes, undefined behavior.

pointer = 0; // now pointer is a null-pointer again, but this doesn't free up any space, as opposed to delete
//Yipee, finally, the pointer actually points to 0.

int value_4 = *pointer; //ERROR: you tried to access a null pointer again.
//No, not an Error, just undefiend attempt at reading w/e is written at the first address in ram.(this could be theoretically valid in some embedded systems.)

// for arrays you need to use this:
pointer = new int[32]; // allocate 32 integers (4 bytes per int) to the place pointed by pointer
//pointer will actually point to the first element of the array meaning this will be valid, and give a value:
int value_5 = *pointer; // VALID: gives back pointer[0]

delete [] pointer; //this frees up the space pointed by pointer, but pointer will still be valid, so you need to set it to 0
//pointer is pointing to undefined data after deletion.
pointer = 0; // now pointer is a null-pointer again
// to check wether pointer is valid you can use a simple if
if(pointer) // if pointer is 0 (logical false) it will be invalid (null-pointer) else it will be valid
//again, some embeded systems can use 0 as a logical address, as well, null does not always = 0, in 16/8 bit days, the processor did some near/far stuff with pointers, so do not assume null = 0
{
  std::cout << "Pointer is valid.\n";
}
else
{
  std::cout << "Pointer is INvalid.\n";
}

generally, my nitpick is with your first line, claiming that an uninitialized pointer is always 0 is wrong, it is UNDEFINED, never trust data that is uninitiated, and for suggesting that it well become 0 tells me your understanding of pointers/data structures is flawed.

well it does work, glUnmapBuffer doesn't throw any errors if you add checking after it. It reproduces the same behaviour on Linux Mint 12.1 64 bit HD 5670 Catalyst 12.1


as brother bob said, check the return value.

as per specifications, it should not work in the same way as OP describes.  I can not tell you why it does, but it shouldn't.

#4slicer4ever

Posted 14 February 2012 - 04:47 PM

I hope the collision course I gave on pointers at least cleared some stuff in his head Posted Image

i'm sorry, but he was doing nothing wrong in his initial code when it comes to pointers. and to be fair, he seems to have a better understanding then your "course"

for example:
int* pointer; // creates an 8-byte pointer to an integer, pointer's value is 0 (Null-pointer)
///////////This is not true, first of all, pointers generally are 4-bytes(although it isn't impossible for the architecture your working on to use 8-byte's for a pointer, generally they are only 4 bytes.)
//////////Secondly, not defining a value, does [B]NOT[/B] mean it defaults as a null pointer, it is simply undefined(do not rely on uninitialized pointers to equal 0, this is extremely bad practice.)

int value = *pointer; // ERROR: you tried to access the contents of a null-pointer, this is illegal
//again, not invalid, but is undefined behavior(generally seg-fault level, but it is not illegal by compilation to access a null pointer.)

pointer = new int; // dynamically (at runtime) assigns 4 bytes of memory at the place which is pointed by pointer, pointer is now valid
//pointer is not "now" valid, the assumption is that you were successful in creating the memory, but does not gurantee the returned data is valid(i.e: out of memory errors)

int value_2 = *pointer; // VALID: pointer actually points to somewhere, so value_2 should be either 0 or a random number, this depends on the compiler
//Yes, undefined behavior.

delete pointer; // tells the operating system to free the place pointed by pointer
//Assuming that the pointer was successfully created.

int value_3 = *pointer; // VALID: pointer still points to somewhere in the memory, and most probably you'll get back the old value pointed by pointer, since nothing has overwritten it. BUT this cannot be guaranteed (so DO NOT do this)
//Yes, undefined behavior.

pointer = 0; // now pointer is a null-pointer again, but this doesn't free up any space, as opposed to delete
//Yipee, finally, the pointer actually points to 0.

int value_4 = *pointer; //ERROR: you tried to access a null pointer again.
//No, not an Error, just undefiend attempt at reading w/e is written at the first address in ram.(this could be theoretically valid in some embedded systems.)

// for arrays you need to use this:
pointer = new int[32]; // allocate 32 integers (4 bytes per int) to the place pointed by pointer
//pointer will actually point to the first element of the array meaning this will be valid, and give a value:
int value_5 = *pointer; // VALID: gives back pointer[0]

delete [] pointer; //this frees up the space pointed by pointer, but pointer will still be valid, so you need to set it to 0
//pointer is pointing to undefined data after deletion.
pointer = 0; // now pointer is a null-pointer again
// to check wether pointer is valid you can use a simple if
if(pointer) // if pointer is 0 (logical false) it will be invalid (null-pointer) else it will be valid
//again, some embeded systems can use 0 as a logical address, as well, null does not always = 0, in 16/8 bit days, the processor did some near/far stuff with pointers, so do not assume null = 0
{
  std::cout << "Pointer is valid.\n";
}
else
{
  std::cout << "Pointer is INvalid.\n";
}

generally, my nitpick is with your first line, claiming that an uninitialized pointer is always 0 is wrong, it is UNDEFINED, never trust data that is uninitiated, and for suggesting that it well become 0 tells me your understanding of pointers/data structures is flawed.

#3slicer4ever

Posted 14 February 2012 - 04:46 PM

I hope the collision course I gave on pointers at least cleared some stuff in his head Posted Image

i'm sorry, but he was doing nothing wrong in his initial code when it comes to pointers. and to be fair, he seems to have a better understanding then your "course"

for example:
int* pointer; // creates an 8-byte pointer to an integer, pointer's value is 0 (Null-pointer)
///////////This is not true, first of all, pointers generally are 4-bytes(although it isn't impossible for the architecture your working on to use 8-byte's for a pointer, generally they are only 4 bytes.)
//////////Secondly, not defining a value, does [B]NOT[/B] mean it defaults as a null pointer, it is simply undefined(do not rely on uninitialized pointers to equal 0, this is extremely bad practice.)

int value = *pointer; // ERROR: you tried to access the contents of a null-pointer, this is illegal
//again, not invalid, but is undefined behavior(generally seg-fault level, but it is not illegal by compilation to access a null pointer.)

pointer = new int; // dynamically (at runtime) assigns 4 bytes of memory at the place which is pointed by pointer, pointer is now valid
//pointer is not "now" valid, the assumption is that you were successful in creating the memory, but does not gurantee the returned data is valid(i.e: out of memory errors)

int value_2 = *pointer; // VALID: pointer actually points to somewhere, so value_2 should be either 0 or a random number, this depends on the compiler
//Yes, undefined behavior.

delete pointer; // tells the operating system to free the place pointed by pointer
//Assuming that the pointer was successfully created.

int value_3 = *pointer; // VALID: pointer still points to somewhere in the memory, and most probably you'll get back the old value pointed by pointer, since nothing has overwritten it. BUT this cannot be guaranteed (so DO NOT do this)
//Yes, undefined behavior.

pointer = 0; // now pointer is a null-pointer again, but this doesn't free up any space, as opposed to delete
//Yipee, finally, the pointer actually points to 0.

int value_4 = *pointer; //ERROR: you tried to access a null pointer again.
//No, not an Error, just undefiend attempt at reading w/e is written at the first address in ram.(this could be theoretically valid in some embedded systems.)

// for arrays you need to use this:
pointer = new int[32]; // allocate 32 integers (4 bytes per int) to the place pointed by pointer
//pointer will actually point to the first element of the array meaning this will be valid, and give a value:
buffer(which for your purpose is generalized as an int)
int value_5 = *pointer; // VALID: gives back pointer[0]

delete [] pointer; //this frees up the space pointed by pointer, but pointer will still be valid, so you need to set it to 0
//pointer is pointing to undefined data after deletion.
pointer = 0; // now pointer is a null-pointer again
// to check wether pointer is valid you can use a simple if
if(pointer) // if pointer is 0 (logical false) it will be invalid (null-pointer) else it will be valid
//again, some embeded systems can use 0 as a logical address, as well, null does not always = 0, in 16/8 bit days, the processor did some near/far stuff with pointers, so do not assume null = 0
{
  std::cout << "Pointer is valid.\n";
}
else
{
  std::cout << "Pointer is INvalid.\n";
}

generally, my nitpick is with your first line, claiming that an uninitialized pointer is always 0 is wrong, it is UNDEFINED, never trust data that is uninitiated, and for suggesting that it well become 0 tells me your understanding of pointers/data structures is flawed.

#2slicer4ever

Posted 14 February 2012 - 04:45 PM

I hope the collision course I gave on pointers at least cleared some stuff in his head Posted Image

i'm sorry, but he was doing nothing wrong in his initial code when it comes to pointers. and to be fair, he seems to have a better understanding then your "course"

for example:
int* pointer; // creates an 8-byte pointer to an integer, pointer's value is 0 (Null-pointer)
///////////This is not true, first of all, pointers generally are 4-bytes(although it isn't impossible for the architecture your working on to use 8-byte's for a pointer, generally they are only 4 bytes.)
//////////Secondly, not defining a value, does [B]NOT[/B] mean it defaults as a null pointer, it is simply undefined(do not rely on uninitialized pointers to equal 0, this is extremely bad practice.)

int value = *pointer; // ERROR: you tried to access the contents of a null-pointer, this is illegal
//again, not invalid, but is undefined behavior(generally seg-fault level, but it is not illegal by compilation to access a null pointer.)

pointer = new int; // dynamically (at runtime) assigns 4 bytes of memory at the place which is pointed by pointer, pointer is now valid
//pointer is not "now" valid, the assumption is that you were successful in creating the memory, but does not gurantee the returned data is valid(i.e: out of memory errors)

int value_2 = *pointer; // VALID: pointer actually points to somewhere, so value_2 should be either 0 or a random number, this depends on the compiler
//Yes, undefined behavior.

delete pointer; // tells the operating system to free the place pointed by pointer
//Assuming that the pointer was successfully created.

int value_3 = *pointer; // VALID: pointer still points to somewhere in the memory, and most probably you'll get back the old value pointed by pointer, since nothing has overwritten it. BUT this cannot be guaranteed (so DO NOT do this)
//Yes, undefined behavior.

pointer = 0; // now pointer is a null-pointer again, but this doesn't free up any space, as opposed to delete
//Yipee, finally, the pointer actually points to 0.

int value_4 = *pointer; //ERROR: you tried to access a null pointer again.
//No, not an Error, just undefiend attempt at reading w/e is written at the first address in ram.(this could be theoretically valid in some embedded systems.)

// for arrays you need to use this:
pointer = new int[32]; // allocate 32 integers (4 bytes per int) to the place pointed by pointer
//pointer will actually point to the first element of the array meaning this will be valid, and give a value:
//incorrect, in way: pointer points to the memory block of the allocated memory, which so happens to be the first value of an 32*sizeof(int) byte sized buffer(which for your purpose is generalized as an int)
int value_5 = *pointer; // VALID: gives back pointer[0]

delete [] pointer; //this frees up the space pointed by pointer, but pointer will still be valid, so you need to set it to 0
//pointer is pointing to undefined data after deletion.
pointer = 0; // now pointer is a null-pointer again
// to check wether pointer is valid you can use a simple if
if(pointer) // if pointer is 0 (logical false) it will be invalid (null-pointer) else it will be valid
//again, some embeded systems can use 0 as a logical address, as well, null does not always = 0, in 16/8 bit days, the processor did some near/far stuff with pointers, so do not assume null = 0
{
  std::cout << "Pointer is valid.\n";
}
else
{
  std::cout << "Pointer is INvalid.\n";
}

generally, my nitpick is with your first line, claiming that an uninitialized pointer is always 0 is wrong, it is UNDEFINED, never trust data that is uninitiated, and for suggesting that it well become 0 tells me your understanding of pointers/data structures is flawed.

#1slicer4ever

Posted 14 February 2012 - 04:42 PM

I hope the collision course I gave on pointers at least cleared some stuff in his head Posted Image

i'm sorry, but he was doing nothing wrong in his initial code when it comes to pointers. and to be fair, he seems to have a better understanding then your "course"

for example:
int* pointer; // creates an 8-byte pointer to an integer, pointer's value is 0 (Null-pointer)
///////////This is not true, first of all, pointers generally are 4-bytes(although it isn't impossible for the architecture your working on to use 8-byte's for a pointer, generally they are only 4 bytes.)
//////////Secondly, not defining a value, does [B]NOT[/B] mean it defaults as a null pointer, it is simply undefined(do not rely on uninitialized pointers to equal 0, this is extremely bad practice.)

int value = *pointer; // ERROR: you tried to access the contents of a null-pointer, this is illegal
//again, not invalid, but is undefined behavior(generally seg-fault level, but it is not illegal by compilation to access a null pointer.)

pointer = new int; // dynamically (at runtime) assigns 4 bytes of memory at the place which is pointed by pointer, pointer is now valid
//pointer is not "now" valid, the assumption is that you were successful in creating the memory, but does not gurantee the returned data is valid(i.e: out of memory errors)

int value_2 = *pointer; // VALID: pointer actually points to somewhere, so value_2 should be either 0 or a random number, this depends on the compiler
//Yes, undefined behavior.

delete pointer; // tells the operating system to free the place pointed by pointer
//Assuming that the pointer was successfully created.

int value_3 = *pointer; // VALID: pointer still points to somewhere in the memory, and most probably you'll get back the old value pointed by pointer, since nothing has overwritten it. BUT this cannot be guaranteed (so DO NOT do this)
//Yes, undefined behavior.

pointer = 0; // now pointer is a null-pointer again, but this doesn't free up any space, as opposed to delete
//Yipee, finally, the pointer actually points to null.

int value_4 = *pointer; //ERROR: you tried to access a null pointer again.
//No, not an Error, just undefiend attempt at reading w/e is written at the first address in ram.(this could be theoretically valid in some embedded systems.)

// for arrays you need to use this:
pointer = new int[32]; // allocate 32 integers (4 bytes per int) to the place pointed by pointer
//pointer will actually point to the first element of the array meaning this will be valid, and give a value:
//incorrect, in way: pointer points to the memory block of the allocated memory, which so happens to be the first value of an 32*sizeof(int) byte sized buffer(which for your purpose is generalized as an int)
int value_5 = *pointer; // VALID: gives back pointer[0]

delete [] pointer; //this frees up the space pointed by pointer, but pointer will still be valid, so you need to set it to 0
//pointer is pointing to undefined data after deletion.
pointer = 0; // now pointer is a null-pointer again
// to check wether pointer is valid you can use a simple if
if(pointer) // if pointer is 0 (logical false) it will be invalid (null-pointer) else it will be valid
//again, some embeded systems can use 0 as a logical address, as well, null does not always = 0, in 16/8 bit days, the processor did some near/far stuff with pointers, so do not assume null = 0
{
  std::cout << "Pointer is valid.\n";
}
else
{
  std::cout << "Pointer is INvalid.\n";
}

generally, my nitpick is with your first line, claiming that an uninitialized pointer is always 0 is wrong, it is UNDEFINED, never trust data that is uninitiated, and for suggesting that it well become 0 tells me your understanding of pointers/data structures is flawed.

PARTNERS