LinuxQuestions.org
Visit Jeremy's Blog.
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 02-22-2019, 05:22 AM   #1
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Rep: Reputation: Disabled
Variable declartion inside structure in C


Hi,

Is it valid to have structure variable declartion inside compiler flag in C.
For exnample ENABLE_IPV6 is compiler option
struct {
int a;
#ifdef ENABLE_IPV6
char *ipv6;
#endif
}ipv6;
 
Old 02-22-2019, 07:33 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I don't know, but that looks to defeat itself. checking for a define of same within it before its valid.
 
Old 02-22-2019, 07:41 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
It is valid (you could as well try it), but some terms should be corrected:
ENABLE_IPV6 is a 'preprocessor symbol', not a 'compiler option'
'char *ipv6;' is a 'structure-member definition, not a 'variable definition'
(Also it is really unclear what this structure is supposed to represent.)
 
2 members found this post helpful.
Old 02-22-2019, 08:00 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
As NevemTeve says, it is entirely legal.

Assuming you have already tried that code to verify that, by the way?

I have seen this in code which is supposed to be compiled for both Linux as well as Windows.

Without looking it up, there's a primary #define like _WIN32_, or something like that, which is defined when compiling for Windows.

Certain code requires certain variable types which are specific to Windows versus how you'd declare them otherwise.

An example is something like a socket descriptor in Linux is merely an int, but for Windows, again not looking it up, it might be like WINSOCK.

Sorry for not being precise with the #define names, it's been a while.

That seems to be the most common code use for something like that, which I've seen in the past.
 
Old 02-22-2019, 08:30 PM   #5
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
Hi,
Its causing random crash when i am using this strucutre in another source file.Isit due to preporocessoring is hapening first in source file then in header file?
Source code where i am using this structure is also under preporocessor directive.
 
Old 02-22-2019, 10:23 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
http://teaching.csse.uwa.edu.au/unit...gdb-intro.html
 
1 members found this post helpful.
Old 02-25-2019, 10:30 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by RohitB View Post
Hi,
Its causing random crash when i am using this strucutre in another source file.Isit due to preporocessoring is hapening first in source file then in header file?
Source code where i am using this structure is also under preporocessor directive.
Crashes, by my definition occur in code when you try to run it. But you could have a different meaning.

A point I'd like to clarify here would be:
  1. Is the code compiling, and this crash is when you try to run?
  2. Or is the code failing to compile, and your complaint is about that?
If (a) is the situation, then Nevemteve's link shows you information about the debugger. Also there are some links in my signature for blogs about the same topic.

If (b) is the situation, then perhaps you can post the compile errors here.
 
Old 02-25-2019, 06:26 PM   #8
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
Hi,

Its a crash that means runtime error not compiler error.
 
Old 02-25-2019, 09:19 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Have you debugged it?
 
Old 02-25-2019, 09:45 PM   #10
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
Yes,Its random places it is crashing so whether if we use compiler flag in structure it will cause some issues?
 
Old 02-25-2019, 10:27 PM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You have already been asked to debug (gdb) your program. Also Electric Fence might be useful, as your problem might very well related with malloc'ed memory.
https://en.m.wikipedia.org/wiki/Electric_Fence
 
1 members found this post helpful.
Old 02-25-2019, 10:42 PM   #12
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
Its not related to malloc or calloc.It seems compiler optmization creating problem when structure variable declartion were put in compoler flag
 
Old 02-25-2019, 11:20 PM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by RohitB View Post
Its not related to malloc or calloc.
How do you know that?

Quote:
Originally Posted by RohitB View Post
It seems compiler optmization creating problem when structure variable declartion were put in compoler flag
How do you know that?

You have not provided any details at all about the problem, but simply insist it is a preprocessor or compilet related problem, but without giving any useful information that would help others understand the problem.

Please use the links provided above to debug the program and learn what is really happening.

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!
 
1 members found this post helpful.
Old 02-26-2019, 12:25 AM   #14
RohitB
LQ Newbie
 
Registered: Jan 2019
Posts: 20

Original Poster
Rep: Reputation: Disabled
I remove all malloc calloc in that code even pointer also.But when i put compiler option it is rashing and in other case it is not.
Strucutre declariion is present in header file and that header file included in muliple source files.
Source files also have macro to use those strcutre variable.
 
Old 02-26-2019, 02:20 AM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, you have a bug (most likely numerous bugs) in your program. That cannot be solved without seeing the code. Problem is your code is way too big to simply insert here, so I suggest creating an mcve. https://stackoverflow.com/help/mcve
(Or just compress your source into a .tgz file and attach as file).
 
2 members found this post helpful.
  


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
Calculation using variable inside variable unclesamcrazy Linux - Newbie 7 04-21-2015 09:24 AM
structure inside a structure in C batman4 Programming 3 09-13-2012 05:52 AM
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 11:31 AM
can we use variable inside another variable amemyum Linux - Newbie 13 11-16-2009 03:44 PM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM

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

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