Click here to Skip to main content
15,849,487 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: DSA Pin
Richard Deeming28-Aug-23 22:34
mveRichard Deeming28-Aug-23 22:34 
GeneralRe: DSA Pin
ZAID razvi28-Aug-23 22:52
ZAID razvi28-Aug-23 22:52 
GeneralRe: DSA Pin
Andre Oosthuizen29-Aug-23 3:41
mveAndre Oosthuizen29-Aug-23 3:41 
GeneralRe: DSA Pin
Richard Deeming29-Aug-23 4:11
mveRichard Deeming29-Aug-23 4:11 
GeneralRe: DSA PinPopular
Richard MacCutchan28-Aug-23 22:56
mveRichard MacCutchan28-Aug-23 22:56 
GeneralRe: DSA Pin
Dave Kreskowiak29-Aug-23 3:04
mveDave Kreskowiak29-Aug-23 3:04 
GeneralRe: DSA Pin
Richard Deeming29-Aug-23 4:12
mveRichard Deeming29-Aug-23 4:12 
GeneralRe: DSA Pin
CPallini30-Aug-23 3:57
mveCPallini30-Aug-23 3:57 
Quote:
How is that possible sir! Second code is written by a renowned youtuber 'code_with_harry'.
I know. But here is renowed expert CPallini Big Grin | :-D .
If you run Harry's code using Valgrind, then you get (something similar to)
==3144== LEAK SUMMARY:
==3144==    definitely lost: 64 bytes in 4 blocks
==3144==    indirectly lost: 0 bytes in 0 blocks
==3144==      possibly lost: 0 bytes in 0 blocks
==3144==    still reachable: 0 bytes in 0 blocks
==3144==         suppressed: 0 bytes in 0 blocks
==3144== 
==3144== For lists of detected and suppressed errors, rerun with: -s
==3144== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)


On the other hand, if you run the following, alternative code
C
#include <stdio.h>
#include <stdlib.h>


struct Node
{
  int val;
  struct Node * next;
};


void traversal(const struct Node * p)
{
  printf("------------\n");
  while (p)
  {
    printf("%d ",p->val);
    p = p->next;
  }
  printf("\n------------\n");
}

// isEmpty is not strictly needed
// isFull is meaning-less

struct Node * enqueue(struct Node *p, int val)
{

  struct Node * prev = NULL;
  while (p)
  {
    prev = p;
    p = p->next;
  }
  p = malloc(sizeof(struct Node ));
  if ( p )
  {
    p->val = val;
    p->next = NULL;
    if ( prev )
      prev->next = p;
  }
  return p;
}

int dequeue(struct Node ** pp)
{
  int val = -1;
  struct Node * p = *pp;
  if ( p )
  {
    val = p->val;
    *pp = p->next;
    free(p);
  }
  return val;
}

int main()
{
  struct Node * ph = enqueue(NULL, 11);
  enqueue(ph, 22);
  enqueue(ph, 33);
  enqueue(ph, 44);
  traversal(ph);
  dequeue(&ph);
  dequeue(&ph);
  traversal(ph);
  dequeue(&ph);
  dequeue(&ph);
  return 0;
}

then Valgrind is somewhat happier:
==3153== HEAP SUMMARY:
==3153==     in use at exit: 0 bytes in 0 blocks
==3153==   total heap usage: 5 allocs, 5 frees, 1,088 bytes allocated
==3153== 
==3153== All heap blocks were freed -- no leaks are possible
==3153== 
==3153== For lists of detected and suppressed errors, rerun with: -s
==3153== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

"In testa che avete, Signor di Ceprano?"
-- Rigoletto

QuestionInvalid application of 'sizeof' to incomplete type 'struct Node' Pin
ZAID razvi22-Aug-23 21:20
ZAID razvi22-Aug-23 21:20 
AnswerRe: Invalid application of 'sizeof' to incomplete type 'struct Node' Pin
Victor Nijegorodov22-Aug-23 21:41
Victor Nijegorodov22-Aug-23 21:41 
GeneralRe: Invalid application of 'sizeof' to incomplete type 'struct Node' Pin
ZAID razvi25-Aug-23 0:45
ZAID razvi25-Aug-23 0:45 
AnswerRe: Invalid application of 'sizeof' to incomplete type 'struct Node' Pin
CPallini22-Aug-23 22:06
mveCPallini22-Aug-23 22:06 
PraiseRe: Invalid application of 'sizeof' to incomplete type 'struct Node' Pin
ThatsAlok23-Aug-23 18:01
ThatsAlok23-Aug-23 18:01 
GeneralOT Pin
CPallini23-Aug-23 20:59
mveCPallini23-Aug-23 20:59 
GeneralRe: Invalid application of 'sizeof' to incomplete type 'struct Node' Pin
ZAID razvi25-Aug-23 0:43
ZAID razvi25-Aug-23 0:43 
GeneralRe: Invalid application of 'sizeof' to incomplete type 'struct Node' Pin
CPallini25-Aug-23 0:47
mveCPallini25-Aug-23 0:47 
Questionconversions in 64 bit mode Pin
wizQ21-Aug-23 19:35
wizQ21-Aug-23 19:35 
AnswerRe: conversions in 64 bit mode Pin
CPallini21-Aug-23 21:41
mveCPallini21-Aug-23 21:41 
AnswerRe: conversions in 64 bit mode PinPopular
Randor 22-Aug-23 2:13
professional Randor 22-Aug-23 2:13 
GeneralRe: conversions in 64 bit mode Pin
wizQ22-Aug-23 18:46
wizQ22-Aug-23 18:46 
GeneralRe: conversions in 64 bit mode Pin
Richard MacCutchan22-Aug-23 22:42
mveRichard MacCutchan22-Aug-23 22:42 
Questiongenerate PDF file Pin
wizQ18-Aug-23 19:13
wizQ18-Aug-23 19:13 
AnswerRe: generate PDF file Pin
Victor Nijegorodov18-Aug-23 22:03
Victor Nijegorodov18-Aug-23 22:03 
AnswerRe: generate PDF file Pin
jschell21-Aug-23 3:37
jschell21-Aug-23 3:37 
QuestionGet LVN_MARQUEEBEGIN in CListCtrl Pin
Li Lin 202317-Aug-23 23:04
Li Lin 202317-Aug-23 23:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.