LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2024, 02:16 AM   #16
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318

Quote:
Originally Posted by lxs602 View Post
I hope this isn't a stupid question.
No,it isn't.
Quote:
Originally Posted by lxs602 View Post
I know this method is not the best way, but I tried to correct just as an exercise. How can I make ptr NULL to escape the while loop above?
First of all you either use it as a linked list or an array. Do not mix them, that is just confusing.
So as an array:
Code:
for( int a = 0; N > a; ++a) {
    free table[a];
    table[a] = NULL;
}
And as a linked list:
Code:
ptr = table[0];
         
while (ptr != NULL)
{         
    p2 = ptr;
    ptr = ptr->next;
    free(p2);
}
But in this case you need to manage that array additionally
 
Old 04-29-2024, 05:59 AM   #17
murugesandins
Member
 
Registered: Apr 2024
Location: Bangalore Karnataka India
Distribution: CYGWIN_NT
Posts: 61

Rep: Reputation: 0
@lxs602
Following code is a sample code.
Few years ago I have seen following kind of code(ptr/p2/any pointer) being used by different process(using fork)
Code:
for( int a = 0; a < N; ++a) {
    if( NULL != table[a] )
    {
        free table[a];
        table[a] = NULL;
    }
}
// ...
ptr=table[0];
while( NULL != ptr )
{
    p2 = ptr;
    ptr = ptr->next;
    if( NULL != p2 ) // Hence it is better to validate NULL exception since this code in future may be changed to be used by multiple threads/process
    {
        free(p2);
        p2 = NULL;
    }
}
01)
Always use:
a <= N instead of N <= a
I have seen during 2003 to 2008 to view new/oldest developers could have missed < operator and that would report a bug report.
02)
same thing applicable at while( NULL != ptr )
03)
Better to use:
if( NULL != p2 )
{
free(p2);
p2 = NULL;
}
since I have seen free exceptions when my program was using external library using my pointer(rfcsdk and nwrfcsdk) at Linux/HP-UX/SunOS/AIX/CYGWIN_NT at Windows

Last edited by murugesandins; 05-01-2024 at 01:41 AM. Reason: I have written only to help lxs602. Hence removed old comment.
 
Old 04-29-2024, 05:22 PM   #18
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by murugesandins View Post
03)
Better to use:
if( NULL != p2 )
{
free(p2);
p2 = NULL;
}
since I have seen free exceptions when my program was using external library using my pointer(rfcsdk and nwrfcsdk) at Linux/HP-UX/SunOS/AIX/CYGWIN_NT at Windows

Even if you were using some deallocation function which didn't check for NULL, the check here is doubly redundant because it's already checked in the loop condition, and the ptr->next expression would have caused a segfault if it was NULL.
 
1 members found this post helpful.
Old 04-29-2024, 07:23 PM   #19
murugesandins
Member
 
Registered: Apr 2024
Location: Bangalore Karnataka India
Distribution: CYGWIN_NT
Posts: 61

Rep: Reputation: 0
Quote:
Originally Posted by ntubski View Post
redundant because it's already checked in the loop condition,
I don't have the full code which I have seen during 2008 to 2019.
The posted code was a sample code.
I have written the code based on the analysis I have seen to prevent core dump issue based on external library and based on the compiler (g++.exe/gcc.exe/aCC/xlC/CC/cc) at different OS(CYGWIN_NT/HP-UX/SunOS/AIX/*NIX*)
My comment was:
Handle free using related if condition when it is required always since it may create core dump issue at particular OS alone due to usage of external libraries.
external libraries used to inform the error was not from their side which I have seen during 2008 to 2013 => They have closed my bug report only one time out of 13 bugs I reported.
 
Old 04-29-2024, 08:53 PM   #20
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by murugesandins View Post
I don't have the full code which I have seen during 2008 to 2019.
The posted code was a sample code.
...
external libraries used to inform the error was not from their side which I have seen during 2008 to 2013 => They have closed my bug report only one time out of 13 bugs I reported.
Code you saw in previous years but cannot produce here is irrelevant to this thread, as is discussion of unnamed external libraries. Please confine discussion to the questions asked and relevant examples.

And for benefit of those following the discussion, within the context of this thread the point made by ntubski is actually correct based only on the code posted (i.e. without reference to other sources): The NULL test is twice redundant and unnecessary...

Quote:
Originally Posted by murugesandins View Post
@lxs602

Code:
...
while( NULL != ptr )
{
    p2 = ptr;
    ptr = ptr->next;
    if( NULL != p2 ) <- Unnecessary
    {
        free(p2);
        p2 = NULL;
    }
}

Last edited by astrogeek; 04-29-2024 at 09:02 PM. Reason: Added necessary example code highlighted
 
Old 04-30-2024, 07:57 AM   #21
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Very often, code is simply written as while(ptr) ... since NULL corresponds to zero = false.

However – I have no personal objections to clarity in code: if you want to add that "if p2 is not null," then by all means feel free.

FYI, usually I would write code like this ... like this:
Code:
while (ptr) {    // or: while (ptr != NULL) ...
  p2 = ptr->next;
  free(ptr);
  ptr = p2;
}
If you are inside the "while" loop, you know that ptr isn't NULL because you are here.

But again, clarity: "it works," "you can prove it (by testing)," and "it is clear to the human reader." The compiler doesn't really care – it knows how to "optimize away" things anyway.

I explicitly set freed pointers to NULL, as I've said, specifically so that any other code which encounters this pointer won't do the wrong thing. And, now I don't have to care where it might be. In any case, I don't have to "test for NULL" because free() recognizes and ignores NULL.

Last edited by sundialsvcs; 04-30-2024 at 08:00 AM.
 
  


Reply

Tags
beginner, clanguage, pointers



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why a regular grammar cannot be both left-recursive, and right-recursive? ajiten Programming 17 08-15-2023 01:25 PM
[SOLVED] Freeing allocated memory in a recursive data structure Gullible Jones Programming 1 08-29-2013 03:51 AM
[SOLVED] Freeing dynamically allocated memory in a C recursive function marian57 Programming 7 09-11-2011 06:58 PM
non Recursive query and Recursive query prashsharma Linux - Server 1 06-27-2007 09:33 AM
Freeing memory allocated in a function con Programming 4 01-02-2006 03:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration