using compiler option to flag a dangling pointer in code

The code below compiles, despite having a bug in function g(), where a pointer to the instance is deleted and then a method is invoked.

Does ```g++ -std=c++20``` have a compiler option that can be used to flag a compile time error ? Any help will be appreciated.

#include <iostream>

class X {
      int x;
public:
      X ();
      auto print () -> void const;
};

X::X () : x{0} {
      ;
}

auto X::print () -> void const {
      std::cout << x << std::endl;
}


auto f (X *p) -> void {
      delete p;
}

auto g () -> void {
      X *q = new X;
      f (q);
      q->print();
}


auto main () -> int {
      g ();
      return 0;
}
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/inouthack
πŸ“…︎ Jan 01 2022
🚨︎ report
Is it safe to create references to zero-sized types from aligned dangling pointers?

And if it is, can you have multiple mut references at the same time?

fn main() {
    let ptr = std::mem::align_of::<()>() as *mut ();
    let ref1 = unsafe { &mut *ptr };
    let ref2 = unsafe { &mut *ptr };
    
    println!("{:?}, {:?}", ref1, ref2);
}
πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/LechintanTudor
πŸ“…︎ Dec 18 2021
🚨︎ report
How to handle dangling pointers in a game engine?

I have a struct called "Objects" that looks something like this:

typedef struct Object{
    float[3] position;
    float[3] rotation;
    Mesh* mesh;
    Material* material;
    ...
} Object;

Each object can have a pointer to a mesh, but a mesh can be shared between two different objects:

Mesh* mesh1;
Object* obj1
Object* obj2;
obj1->mesh = mesh1;
obj2->mesh = mesh1;

The problem arise when I free the mesh, the mesh pointers in each object is now pointing to an invalid memory location and I can't know because the pointer isn't NULL. Is there any simple way to solve this?

I'm not sure but I think the solution in c++ is weak pointers or smart pointers? I could also store each reference to the mesh in it's structure for cleanup but that would be cumbersome.

πŸ‘︎ 40
πŸ’¬︎
πŸ‘€︎ u/noiseuli
πŸ“…︎ Jun 17 2021
🚨︎ report
Keep your dangling pointer in your pants ffs
πŸ‘︎ 414
πŸ’¬︎
πŸ‘€︎ u/sushantvashisht
πŸ“…︎ Feb 12 2021
🚨︎ report
[Recording, Mixing, Mastering] Dangling Pointer soundcloud.com/balintkiss…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/balintkissguitar
πŸ“…︎ May 22 2021
🚨︎ report
Is existing a way to fool Rust's compiler and make a dangling pointer? reddit.com/r/rust/comment…
πŸ‘︎ 7
πŸ’¬︎
πŸ‘€︎ u/FinJoTheGreat
πŸ“…︎ Mar 12 2021
🚨︎ report
Keep your dangling pointer in your pants ffs
πŸ‘︎ 4
πŸ’¬︎
πŸ‘€︎ u/givemeagooduns_un
πŸ“…︎ Feb 13 2021
🚨︎ report
Dangling Pointer

Hello community , I'm actually working on the rust cookbook and I didn't understand why there is a dangling pointer in this case.

fn dangle() -> &String { // dangle returns a reference to a String

let s = String::from("hello"); // s is a new String

&s // we return a reference to the String, s

} // Here, s goes out of scope, and is dropped. Its memory goes away. // Danger!

It said that " when the function is finished we deallocated the string ,but we tried to return a reference to it"

In my opinion ,we are first returning a reference to the string ,then it is deallocated because it's the end of the scope.

πŸ‘︎ 4
πŸ’¬︎
πŸ‘€︎ u/rayanaay
πŸ“…︎ Sep 25 2020
🚨︎ report
dangling pointer - why am i getting all the values to be zero?

Hi,

I am trying to read garbage values of memory, character by character.

For that i am using dangling pointers, e.g. pointers that have not been initialized, and therefore point to random places in the memory.

If I try to "inspect" the stack memory:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 200

int main ()
{
	char* ptr;
	
	/* ptr = malloc(SIZE); */
	
	for (int n = 0; n < SIZE; n++)
	{
		if (*(ptr + n) != 0)
		printf("The value ptr[%d] is %d\n", n, *(ptr + n) );
	}
	
	printf("this is a test\n");
}

I get:

The value ptr[156] is -1
The value ptr[157] is 127
The value ptr[160] is 47
The value ptr[161] is -17
The value ptr[162] is -1
The value ptr[163] is -1
The value ptr[164] is -1
The value ptr[165] is 127
The value ptr[168] is 66
The value ptr[169] is -17
The value ptr[170] is -1
The value ptr[171] is -1
The value ptr[172] is -1
The value ptr[173] is 127
The value ptr[184] is 3
The value ptr[192] is 64
The value ptr[194] is 32

which is what i expect to get, since the places of the memory are random, their corresponding values should also be random.

However, if I try to inspect the heap memory, all the values pointed by the array of dangling pointers are 0:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 200

int main ()
{
	char* ptr;
	
	ptr = malloc(SIZE);
	
	for (int n = 0; n < SIZE; n++)
	{
		if (*(ptr + n) != 0)
		printf("The value ptr[%d] is %d\n", n, *(ptr + n) );
	}
	
	printf("this is a test\n");
}

I get the following output:

this is a test

which means all the values pointed by the arrays of dangling pointer (since i did not initialize the malloc memory block to anything) are 0.

Why do i get everything 0 for the heap memory? I would expect to read some garbage random values...

Is my understanding of "dangling pointers" correct?

Many thanks!

πŸ‘︎ 15
πŸ’¬︎
πŸ‘€︎ u/reebs12
πŸ“…︎ Sep 01 2019
🚨︎ report
Help my pointer is dangling
πŸ‘︎ 57
πŸ’¬︎
πŸ‘€︎ u/maybeSkywalker
πŸ“…︎ Jan 16 2020
🚨︎ report
Rust’s elegant solution to dangling pointers medium.com/p/2773d49cf86c…
πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/twofiftysix-bit
πŸ“…︎ Jul 04 2020
🚨︎ report
β™«β™«β™« Imagine there's no unsafe blocks β™«β™«β™« It's easy if you try β™«β™«β™« No dangling pointers below us β™«β™«β™« Above us, only sky β™«β™«β™« Imagine all the programmers writing memory-safe... β™«β™«β™« news.ycombinator.com/item…
πŸ‘︎ 50
πŸ’¬︎
πŸ‘€︎ u/cmov
πŸ“…︎ Feb 18 2017
🚨︎ report
dangling pointer !

my question is simple ,according to my knowledge this example shouldn't work considering it returns a pointer to a dead object

const char* f() { const char* p="hi"; return p; }

so do strings get sepecial treatment and they get allocation in heap not in stack (implicitly ??) or is it due to some compiler optimization ? although i've tried to disable these . ps : i'm using g++ 6.3

and thnx :)

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/dringthedreamer
πŸ“…︎ Jul 26 2018
🚨︎ report
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/RCaneBooks
πŸ“…︎ Jun 01 2019
🚨︎ report
Separation Logic AnalYzER ... checks that its input C code doesn't deference dangling pointers, do double frees, nor leak memory. research.microsoft.com/en…
πŸ‘︎ 60
πŸ’¬︎
πŸ‘€︎ u/alexeyr
πŸ“…︎ Jan 08 2011
🚨︎ report
Dangling pointers and undefined behavior kristerw.blogspot.com/201…
πŸ‘︎ 11
πŸ’¬︎
πŸ‘€︎ u/n3xg3n
πŸ“…︎ Apr 25 2016
🚨︎ report
Does my cpp code have dangling pointers?

I have watched many youtube videos on it and still dont really understand. Could I get some input on this https://pastebin.com/s44XAf9M I feel if i delete current and prev every time it will delete the nodes they are pointed too and am just very unsure... thanks :D

πŸ‘︎ 3
πŸ’¬︎
πŸ“…︎ Oct 10 2017
🚨︎ report
[C]Singly-Linked List: Dangling Pointer and Undef. Behaviour.

An abstract question: Say I have a singly-linked list of the nodes:

root->[data|next]->[data|next]->NULL

In C, root is declared:

struct Node *root = NULL;

where *root is the Node pointer 'holding' the address "NULL".

Now, lets say that I want to remove the last node in the linked list, the following code will allow the computer to do such action:

//pop: pop removes last/latter node in linked list and sets new last node to NULL
void pop(struct Node *root){
    struct Node *last;
    struct Node *current = root;    //current point to same node as root

    while(current->next != NULL){
        if(current->next == NULL) {break;}    //premature break in traversing of linked list; will stop at last node
        last = current;    //last points to same node as current
        current = current->next;    //move current to next node
    }

    last->next = NULL;    //point second-to-last node to NULL, making it defacto last node in list
    free(current);    //free last node to heap

}//end pop

after calling pop and passing root to the function, the new linked list looks like this:

root->[data|next]->NULL

If the program calls pop again, we should expect the linked list to look like this:

root->NULL

However, it does not! In the case of a linked list of integer elements in order, we will call pop until we observe strange behaviour:

List: 1 2 3
Call pop
List: 1 2
Call pop
List: 1
Call pop
List 1980765

The above is an example of undefined behavoir caused by a dangling pointer. Now the question is: How can the program avoid this behaviour and produce a side-effect that close to root->NULL from popping all nodes from the linked list until said list is empty?


Update:

struct Node *pop(struct Node *root){

    int index = 0;
    struct Node *last = root;
    struct Node *new;

    while(last->next != NULL){

    	new = last;
    	last = last->next;										//move to next node
    	index++;											//change index flag
    
    }

    if(index > 0){

    	new->next = NULL;										//new last node points to NULL
    	free(last);											//free last node

    }

    if(index == 0){return(NULL);}										//return NULL
 
    return(root);												//return linked list
 
}//end burn
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/LinuxMan_1001
πŸ“…︎ Jul 04 2017
🚨︎ report
A Pointer-Free Path to Object-Oriented Parallel Programming: No pointers, no garbage collection, no dangling pointers. parasail-programming-lang…
πŸ‘︎ 22
πŸ’¬︎
πŸ‘€︎ u/marc-kd
πŸ“…︎ Aug 01 2012
🚨︎ report
Dangling Pointer Clarification

I've been learning about dynamic memory allocation, and have this snippet of code which demonstrates how deallocating memory may cause multiple dangling pointers:

int *ptr = new int;
int *otherPtr = ptr;

delete ptr; 

ptr = nullptr;

otherPtr is a dangling pointer.

If I have understood correctly, ptr was the owner of the memory, so the delete operator was required to return the allocated memory back to the operating system.

otherPtr was not the owner of the memory, so the delete operator is not necessary, it just needs to be set to null.

otherPtr = nullptr;

Is that correct?

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Diablo84
πŸ“…︎ Feb 16 2018
🚨︎ report
A dangling pointer is indeterminate trust-in-soft.com/danglin…
πŸ‘︎ 20
πŸ’¬︎
πŸ‘€︎ u/pascal_cuoq
πŸ“…︎ Jul 07 2014
🚨︎ report
Pwn2Own 2014: AFD.SYS DANGLING POINTER VULNERABILITY (PDF) siberas.de/papers/Pwn2Own…
πŸ‘︎ 81
πŸ’¬︎
πŸ‘€︎ u/digicat
πŸ“…︎ Jul 11 2014
🚨︎ report
Mitigating Dangling Pointer Bugs Using Frame Poisoning weblogs.mozillazine.org/r…
πŸ‘︎ 22
πŸ’¬︎
πŸ‘€︎ u/djpnewton
πŸ“…︎ Oct 16 2010
🚨︎ report
[C] Dangling pointers within an initialisation helper function

Hi there,

I'm currently implementing a priority queue in C, just to get to grips with how the structure works. I have an initialisation helper function, which allocates memory for the structure and returns a pointer to it. When I run the program with valgrind, I get a message saying that a memory leak has occurred in the init_pqueue() method:

by 0x40068D: init_pqueue  

I'm pretty sure I know why this is happening - I'm declaring and initialising the pointer within the function and returning it, so any calls to free() would have to be made within the scope of the function. However, I wouldn't be able to return the pointer if this were the case. (Is this right?)

Could anyone show me how to initialise this structure without the memory leaks? Should I be using a helper function that returns a pointer it has created?

Code is here

Thanks for any help.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Evermage
πŸ“…︎ Feb 23 2015
🚨︎ report
[C++] Wikipedia dangling pointer example
int *a = new int;
int *b = a;
delete b;
/* a and b are now dangling pointers */
*a = 4; /* Memory error: we may be overwriting another pointer's data */

Can someone explain this a bit further? I still don't quite understand what a dangling pointer is or how this is an example of it. What does new do, in this case, to the value of a?

Here's the Wiki article: http://en.wikipedia.org/wiki/Memory_safety#Dangling_pointer

Thanks!

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/HidalgoFelix
πŸ“…︎ Jan 25 2013
🚨︎ report
The last sentence in the Dangling Pointer wikipedia article en.wikipedia.org/wiki/Dan…
πŸ‘︎ 20
πŸ’¬︎
πŸ‘€︎ u/factorysettings
πŸ“…︎ Mar 20 2015
🚨︎ report
I've heard of dangling pointers, but are there also flaccid pointers?

Like so:

#include <studio.h>

main() {
    void *flaccid = 0; // Is this right?
    printf("%p\n", flaccid);
}
πŸ‘︎ 28
πŸ’¬︎
πŸ‘€︎ u/OfTheWater
πŸ“…︎ Feb 27 2013
🚨︎ report
A dangling pointer was trying to address...

But he was just referring to a bunch of garbage.

πŸ‘︎ 11
πŸ’¬︎
πŸ‘€︎ u/BringItOnFellas
πŸ“…︎ Oct 07 2014
🚨︎ report
Undangle: Early Detection of Dangling Pointers in Use-After-Free and Double-Free Vulnerabilities [PDF] software.imdea.org/~juanc…
πŸ‘︎ 16
πŸ’¬︎
πŸ‘€︎ u/rolfr
πŸ“…︎ Sep 25 2012
🚨︎ report
Preventing Use-after-free with Dangling Pointers Nullification by Byoungyoung Lee, Chengyu Song, Yeongjin Jang, and Tielei Wang [PDF] cc.gatech.edu/~blee303/pa…
πŸ‘︎ 4
πŸ’¬︎
πŸ‘€︎ u/turnersr
πŸ“…︎ Jan 17 2015
🚨︎ report
Mitigating Dangling Pointer Bugs Using Frame Poisoning weblogs.mozillazine.org/r…
πŸ‘︎ 9
πŸ’¬︎
πŸ‘€︎ u/wtbw
πŸ“…︎ Oct 17 2010
🚨︎ report
The last time a player had more 3 pointers than 3 assists was on February 9, 2001 against the Boston Celtics.

That's the first time that a player had as many 3 pointers as 3 assists.

Source

πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/SportsFan-Bot
πŸ“…︎ Dec 09 2021
🚨︎ report
SERIOUS: This subreddit needs to understand what a "dad joke" really means.

I don't want to step on anybody's toes here, but the amount of non-dad jokes here in this subreddit really annoys me. First of all, dad jokes CAN be NSFW, it clearly says so in the sub rules. Secondly, it doesn't automatically make it a dad joke if it's from a conversation between you and your child. Most importantly, the jokes that your CHILDREN tell YOU are not dad jokes. The point of a dad joke is that it's so cheesy only a dad who's trying to be funny would make such a joke. That's it. They are stupid plays on words, lame puns and so on. There has to be a clever pun or wordplay for it to be considered a dad joke.

Again, to all the fellow dads, I apologise if I'm sounding too harsh. But I just needed to get it off my chest.

πŸ‘︎ 17k
πŸ’¬︎
πŸ‘€︎ u/anywhereiroa
πŸ“…︎ Jan 15 2022
🚨︎ report
Why can we point to another function stack ?

Hi everyone,

I'm currently following the (great) "Crafting interpreters" book, by Robert Nystrom.

In the 16th chapter, the auteur implements a function (errorToken()) which does something like this:

#include <stdio.h>

typedef struct {
    const char * text;
} Str;


Str build_and_set_str(char* text) {
    Str str = { .text = text };
    return str;
}

Str get_str(void) {
    char* local = "'get_str' local";
    return build_and_set_str(local);  
}

int main(void) {
    Str str = get_str();
    printf("%s\n", str.text);
}

If I compile and run this, I get this:

gcc test.c -o test -O0
./test
'get_str' local

My C is a bit rusty, and I don't understand why it works; in my understanding of C function, stack variables are "dropped" when the function returns, unless they are returned. So a.text should be a dangling pointer when get_str() returns, leading to a segmentation fault.

I don't understand what I'm missing here, can someone help me out ?

EDIT:

I'm impatient and stupid. The author explain it right afterward:

> Again, we need to ensure that the error message sticks around long enough for the compiler to read it. In practice, we only ever call this function with C string literals. Those are constant and eternal, so we’re fine.

So If i understand correctly, char* local = "'get_str' local"; will not be stored on the stack, but on some static piece of memory.

Sorry for wasting your time.

πŸ‘︎ 76
πŸ’¬︎
πŸ‘€︎ u/hanzohatoryv
πŸ“…︎ Dec 18 2021
🚨︎ report
Penis slang

Here is all the penis slang I could find, courtesy of the internet

  1. A

  2. Ace in the hole

  3. Acorn Andy

  4. Action Jackson

  5. Adam Halfpint

  6. Admiral Winky

  7. African black snake

  8. Afro man

  9. AIDS baster

  10. AIDS grenade, The

  11. Alabama blacksnake

  12. Albino cave dweller

  13. All-day sucker

  14. Anaconda

  15. Anal impaler

  16. Anal intruder

  17. Anal Spear

  18. Ankle spanker

  19. Apple-headed monster

  20. Ass blaster

  21. Ass pirate

  22. Ass wedge

  23. Astralgod

  24. Auger-headed gut wrench

  25. B

  26. Ba-donk-a-donk

  27. Baby maker

  28. Baby's arm holding an apple

  29. Baby's arm in a boxing glove

  30. Bacon bazooker

  31. Bacon rod

  32. Badboy

  33. Bagpipe

  34. Bald Avenger, The

  35. Bald butler

  36. Bald-headed beauty

  37. Bald-headed giggle stick

  38. Bald-headed hermit

  39. Bald-headed Jesus

  40. Bald-headed yogurt slinger

  41. Baldy-headed spunk-juice dispenser

  42. Ball buddy

  43. Baloney pony

  44. Banana

  45. Bat and balls

  46. Battering ram

  47. Bayonet

  48. Bavarian Beefstick

  49. Beard splitter

  50. Bearded burglar

  51. Beastus maximus

  52. Beaver buster

  53. Beaver Cleaver

  54. Bed snake

  55. Beef baton

  56. Beef bayonet

  57. Beef belt buckle

  58. Beef bugle

  59. Beef bus

  60. Beef missile

  61. Beef soldier

  62. Beef stick

  63. Beefy McManstick

  64. Bell rope

  65. Belly stick

  66. Best leg of three

  67. (Big) Beanpole

  68. Big Dick & the twins

  69. Big Dickus

  70. Big Jake the ene-eyed snake

  71. Big Jim and the Twins

  72. Big Johnson

  73. Big Lebowski

  74. Big number one

  75. Big Mac

  76. Big red

  77. Big rod

  78. Big Uncle

  79. Biggus Dickus

  80. Bilbo Baggins

  81. Bishop, The

  82. Bishop with his nice red hat

  83. Bitch blaster

  84. Bitch stick

  85. Bits and pieces

  86. Blind butler

  87. Blind snake

  88. Blood blunt

  89. Blood slug

  90. Blood sword

  91. Blow pop

  92. Blowtorch

  93. Blue steel

  94. Blue-veined jackhammer

  95. Blue-veined junket pumper

  96. Blue-veined piccolo

  97. Blue-veined puss chucker

  98. Blue-veiner

  99. Blunt

  100. Bob

  101. Bob Dole

  102. Bob Johnson

  103. Bobo

  104. Bone

  105. Bone phone

  106. Bone rollercoaster

  107. Boneless beef

  108. Boneless fish

  109. Boner

  110. Boney cannelloni

  111. Bone-her

  112. Bop gun

  113. Bottle rocket

  114. Bow-legged swamp donkey

  115. Box buster

  116. Boybrush

  117. Bradford and the pair

  118. Bratwurst

  119. Breakfast burrito

  120. Breakfast wood

  121. Broom

  122. Brutus

  123. Bubba

  124. Bulbulous big-knob

  125. Bumtickler

  126. Bush beater

  127. Bush rusher

  128. Bushwhacker

  129. Buster Hymen

  130. Buster McThunderstick

  131. Butt blaster

  132. Butt pirate

  133. Butter churn

  134. Butt

... keep reading on reddit ➑

πŸ‘︎ 15
πŸ’¬︎
πŸ‘€︎ u/Science205014
πŸ“…︎ Jan 13 2022
🚨︎ report
What are your bengals favourite toys?

My boy Leo doesn't care about toys, aside from little wool balls he can chase around and carry in his mouth. He gives NO hecks about laser pointers, toys with bells, catnip anything, feathers on a string. He particularly doesn't care about interactive toys - he wants to play on his terms, by himself. He'll give you a token swat or two if you dangle something at him, but almost immediately loses interest. Despite this, he seems bored. I would really appreciate favourite toy suggestions that your bengals are obsessed with!

πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/kaylamacdonald
πŸ“…︎ Jan 06 2022
🚨︎ report
Just because it's a joke, doesn't mean it's a dad joke

Alot of great jokes get posted here! However just because you have a joke, doesn't mean it's a dad joke.

THIS IS NOT ABOUT NSFW, THIS IS ABOUT LONG JOKES, BLONDE JOKES, SEXUAL JOKES, KNOCK KNOCK JOKES, POLITICAL JOKES, ETC BEING POSTED IN A DAD JOKE SUB

Try telling these sexual jokes that get posted here, to your kid and see how your spouse likes it.. if that goes well, Try telling one of your friends kid about your sex life being like Coca cola, first it was normal, than light and now zero , and see if the parents are OK with you telling their kid the "dad joke"

I'm not even referencing the NSFW, I'm saying Dad jokes are corny, and sometimes painful, not sexual

So check out r/jokes for all types of jokes

r/unclejokes for dirty jokes

r/3amjokes for real weird and alot of OC

r/cleandadjokes If your really sick of seeing not dad jokes in r/dadjokes

Punchline !

Edit: this is not a post about NSFW , This is about jokes, knock knock jokes, blonde jokes, political jokes etc being posted in a dad joke sub

Edit 2: don't touch the thermostat

πŸ‘︎ 6k
πŸ’¬︎
πŸ‘€︎ u/CzarcasmRules
πŸ“…︎ Jan 23 2022
🚨︎ report
Spirituality is a dangling pointer.
πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/EnsilZah
πŸ“…︎ Jan 08 2021
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.