LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-10-2007, 09:01 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
warning in c code


Hi all, hope someone can help me out with this problem. I've written some code which works fine. However I get an warning which I'm unsure how to correct.
Basically I have an array of structures (actually screen set up structures) which I turn off from a kill screen function. However the code produces the following warning

LibaryFilesVer006/SetUpScreen.c: In function ‘killGLWindow’:
LibaryFilesVer006/SetUpScreen.c:170: warning: value computed is not used
LibaryFilesVer006/SetUpScreen.c: In function ‘InitScreenWindows’:
LibaryFilesVer006/SetUpScreen.c:315: warning: value computed is not used

These are called when I step through the array of pointers for each screen connect to my PC so for example the error on line 170 is from the line#

**screenlist++;

in the function

void killGLWindow(GLXScreen **screenlist)
{
while(*screenlist!=EOF_SCREENS)
{

if ((*screenlist)->ctx)
{
if (!glXMakeCurrent(DisplayPointer, None, NULL))
{
printf("Could not release drawing context.\n");
}
glXDestroyContext(DisplayPointer, (*screenlist)->ctx);
(*screenlist)->ctx = NULL;
}
/* switch back to original desktop resolution if we were in fs */
if ((*screenlist)->fs)
{
XF86VidModeSwitchToMode(DisplayPointer, (*screenlist)->screen, &(*screenlist)->deskMode);
XF86VidModeSetViewPort(DisplayPointer, (*screenlist)->screen, 0, 0);
}

**screenlist++;
}

XCloseDisplay(DisplayPointer);
}



Can someone tell me how to get rid of this warning? Thanks
 
Old 08-10-2007, 09:30 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I think it's because you are double de-referencing
**screenlist++
and not using it.

I think you could be lucky to get away with it working

I think you are having trouble understanding how pointers work,
you wouldn't ever de-reference twice a la **

**screenlist is an array of screenlist pointers?
you would typically do *(screenlist +1) or screenlist[1] or something to
use the contents.

get a debugger like ddd and have a root around till you understand
how pointers work.

Last edited by bigearsbilly; 08-10-2007 at 09:33 AM.
 
Old 08-10-2007, 09:41 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Well you would dereference twice if you have a pointer of a pointer :P
 
Old 08-10-2007, 09:53 PM   #4
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by knobby67
LibaryFilesVer006/SetUpScreen.c:315: warning: value computed is not used


**screenlist++;
The compiler already told you pretty exactly what is wrong.

You are incrementing screenlist, which is also what you probably want to do here, but additionally, you dereference screenlist. Twice. Without using the value you get from dereferencing. Why?

A simple
Code:
screenlist++;
will do.
 
Old 08-10-2007, 11:55 PM   #5
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Unless, of course, you mean (**screenlist)++;, but that wouldn't seem to make much sense. I think you want screenlist++ or (*screenlist)++.
 
Old 08-13-2007, 02:49 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
deleted duplicate

Last edited by bigearsbilly; 08-13-2007 at 02:55 AM.
 
Old 08-13-2007, 02:50 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Well you would dereference twice if you have a pointer of a pointer :P
are you sure?


observe:

Code:
#include <stdio.h>

main(int argc, char ** argv)
{
        printf("prog %s got:%s\n", *argv++, *argv);
}
Code:
billym.primadtpdev>1 hello
prog 1 got:hello

Last edited by bigearsbilly; 08-13-2007 at 02:54 AM.
 
Old 08-20-2007, 03:21 AM   #8
larvyde
LQ Newbie
 
Registered: Jul 2007
Posts: 4

Rep: Reputation: 0
Quote:
Originally Posted by bigearsbilly View Post
are you sure?


observe:

Code:
#include <stdio.h>

main(int argc, char ** argv)
{
        printf("prog %s got:%s\n", *argv++, *argv);
}
Code:
billym.primadtpdev>1 hello
prog 1 got:hello
AFAIK this is not portable across compilers, you could instead do

Code:
#include <stdio.h>

main(int argc, char ** argv)
{
        printf("prog %s got:%s\n", argv[0], argv[1]);
}
and get the same result
 
Old 08-20-2007, 03:34 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it should be portable, it's perfectly valid code.
maybe you have a rubbish compiler ?
 
Old 08-24-2007, 12:58 AM   #10
larvyde
LQ Newbie
 
Registered: Jul 2007
Posts: 4

Rep: Reputation: 0
it is valid but the result is undefined, different compilers give different results.
 
Old 08-24-2007, 04:14 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
what exactly are you saying is undefined?

I think the meaning of the postfix and prefix operators is
very clear.
 
Old 08-24-2007, 04:36 PM   #12
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Code:
#include <stdio.h>

main(int argc, char ** argv)
{
        printf("prog %s got:%s\n", *argv++, *argv);
}
has two problems (well, three as far as the standard is concerned since omitting the return value of main is not valid).

First, the order of the evaluation of the function arguments is undefined. This is true for both C and C++.

Second, and this is the more sinister problem, the code evaulates argv *and* modifies it for a purpose other than generating a new value without an intervening sequence point. This is undefined behaviour.

I was sure about the argument evaluation order but I was unsure about the sequence point error, so I forwarded this question to a group which is known for having several gurus. This is their reply in a nutshell.

Last edited by Hivemind; 08-24-2007 at 04:38 PM.
 
Old 08-28-2007, 03:29 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
First, the order of the evaluation of the function arguments is undefined. This is true for both C and C++.
OK you are correct.
Quote:
Second, and this is the more sinister problem, the code evaulates argv *and* modifies it for a purpose other than generating a new value without an intervening sequence point. This is undefined behaviour.
nonsense

in the c-faq
Quote:
If you want to break rule 1, make sure that the several objects being modified are distinctly different, and try to limit yourself to two or at most three modifications, and of a style matching those of the following examples. (Also, make sure that you continue to follow rule 2 for each object modified.) The expression

c = *p++

is allowed under this rule, because the two objects modified (c and p) are distinct. The expression

*p++ = c

is also allowed, because p and *p (i.e. p itself and what it points to) are both modified but are almost certainly distinct.

Last edited by bigearsbilly; 08-28-2007 at 03:31 AM.
 
Old 08-28-2007, 03:52 AM   #14
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
No, you are talking nonsense. The problem is not *argv++ by itself but that you also have *argv. There is no intervening sequence point here, hence undefined behaviour. Here's the relevant part from the comp.lang.c FAQ:
http://www.c-faq.com/expr/evalorder1.html

The folks at comp.lang.c agreed totally that this is undefined behaviour, it wasn't even a discussion and I think comp.lang.c is the best place to go if one has questions regarding standard C. No other forum beats the level of expertise exhibited by its regulars. Same goes for comp.lang.c++ when it comes to c++. When one has platform specific questions, like posix, this forum good, however.
 
Old 08-28-2007, 03:59 AM   #15
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
agreed,
I was saying there is nothing wrong with *argv++,
you seemed to me to be implying *argv++ is undefined,
I agree with the point is an issue only because of the
function argument evaluation order.
which is the only issue by point of fact.

we won't mention the lack of return
 
  


Reply



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
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
LXer: Code Craft: The Practice of Writing Excellent Code LXer Syndicated Linux News 0 01-09-2007 04:03 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
gcc warning compiling C code vose Programming 2 09-29-2004 10:58 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM

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

All times are GMT -5. The time now is 10:02 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