how do i check if a variable (symbol) already exists (C)

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2565 Views - Last Post: 28 August 2013 - 08:31 PM Rate Topic: -----

#1 temp_0001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 25-August 13

how do i check if a variable (symbol) already exists (C)

Posted 26 August 2013 - 06:52 PM

I can't find what I'm looking for online, so maybe one of you know the solution.
I want to check of a variable or symbol has been typedef'ed.

Windows includes the SIZE_T type, but *nix uses size_t.
Regardless of whether size_t is defined in Windows, how do I check if it exists?

I'm using gcc as my compiler and ld as the linker. (i believe ld is the linker for gcc, but i'm not sure)
I know the linker is ld, because I'm using Linux.

Is This A Good Question/Topic? 0
  • +

Replies To: how do i check if a variable (symbol) already exists (C)

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: how do i check if a variable (symbol) already exists (C)

Posted 26 August 2013 - 07:19 PM

The type size_t is a standard type, both Windows and Linux should have this type defined.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 temp_0001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 25-August 13

Re: how do i check if a variable (symbol) already exists (C)

Posted 26 August 2013 - 07:40 PM

View Posttemp_0001, on 26 August 2013 - 06:52 PM, said:

Regardless of whether size_t is defined in Windows, how do I check if [a symbol/variable] exists?

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: how do i check if a variable (symbol) already exists (C)

Posted 26 August 2013 - 07:54 PM

You would compile your code. If the code fails to compile due to an undefined symbol, then you have your answer.

Seriously though, if you are advertising your code to be "cross platform" or "platform independent", you should really setup the other platforms, build, run, and test your code on those platforms. If I picked up your code base and found out that you were depending on strangers on the Internet to tell you whether a symbol exists or not, I would not have a lot of confidence in the code and I would go use some other code base.

Right now, it seems like you are trying to get by the compiler issues. I hope and pray that you've also covered issues like:
- support for atomic operations;
- bitness: 16 vs 32 vs 64;
- endianess: big endian vs little endian vs both;
- stack size: *nix tends to give you smaller stacks versus Windows;
- default charsets;
- file system legal characters;
- file system path separators;
- wildcard expansion;
- environment variables;
- permissions;
- character encoding (eg. ANSI vs. Unicode vs. UTF-8 vs. etc.);
and a host of other issues.
Was This Post Helpful? 2
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: how do i check if a variable (symbol) already exists (C)

Posted 26 August 2013 - 08:31 PM

Quote

temp_0001, on 26 August 2013 - 06:52 PM, said:
Regardless of whether size_t is defined in Windows, how do I check if [a symbol/variable] exists?


The easiest way is to open one of the several standard header files that define this standard typedef and look (search) for it.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 temp_0001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 25-August 13

Re: how do i check if a variable (symbol) already exists (C)

Posted 26 August 2013 - 09:37 PM

Skydiver, it isn't just about my code not being cross-platform.
If i have a variable that has been typedef'ed, and there's another library that the developer is using that uses the same symbol, then their code won't compile even if mine does and is cross platform.

// mylib
typedef int myVar;

//other lib
typedef unsigned int myVar;

i have the issues you listed done in my library already. why would the stack size be an issue? i can have one program use all of my ram, so i don't understand why the stack has anything to do with compatibility. in fact, that's up to the developer to use memory (the heap and the rest of the stack), not me. if you mean i should be worried about using about 0xffffffff amount of memory, then i doubt my library would ever use all of that memory on the stack. (i know that isn't the right amount, but i still believe i won't get close to using anywhere near to most of the memory on the stack)

Jim, opening 100+ header files is too... inefficient. I could make a header file search program that checks for variables and opens the files defined by #include though. However that doesn't eliminate the fact that another library i don't have won't conflict with mine.

I'm going to read the gcc manual and see if i find anything.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: how do i check if a variable (symbol) already exists (C)

Posted 26 August 2013 - 10:53 PM

Did you even try to Google size_t? Here is the first link from trying: size_t.

As you can easily see this standard typedef is defined in one of only 5 files. So why would you need to search hundreds of files? Or are you really talking about other typedefs other than the one you "seem" to be worried about?

You do know that this standard typedef could be different with different compilers, right? One system may define this as an unsigned int, while another may define it as an unsigned long. Also don't forget the basic types may be of different sizes as well. An int on one system may be 2 bytes and on another 4 or even 8 bytes.

Quote

If i have a variable that has been typedef'ed, and there's another library that the developer is using that uses the same symbol, then their code won't compile even if mine does and is cross platform.

What language are you trying to use to write your special library? If you're using C++ you could solve most of your perceived problems by properly using namespaces. As far as their code not compiling because of duplicate symbols goes, the only thing you can really do to prevent this problem is to thoroughly document your library so others will not use variables, functions, typedefs, macros, ect that your library has defined.

Jim
Was This Post Helpful? 2
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: how do i check if a variable (symbol) already exists (C)

Posted 26 August 2013 - 11:27 PM

View Posttemp_0001, on 27 August 2013 - 04:37 AM, said:

Skydiver, it isn't just about my code not being cross-platform.
If i have a variable that has been typedef'ed, and there's another library that the developer is using that uses the same symbol, then their code won't compile even if mine does and is cross platform.

// mylib
typedef int myVar;

//other lib
typedef unsigned int myVar;


Why not just use your imagination to create unique names that are unlikely to be in other libraries.

calling things MyVar is not the way to go, be descriptive.

What does your Library do ? More information would be helpful.

Also if you used something like CMake to package your Library
you can get CMake to check if a function exists.

check_function_exists ( size_t HAVE_SIZE_T )

for example.

Regards

Snoopy.

This post has been edited by snoopy11: 26 August 2013 - 11:34 PM

Was This Post Helpful? 2
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,097
  • Joined: 19-March 11

Re: how do i check if a variable (symbol) already exists (C)

Posted 27 August 2013 - 12:04 AM

I think the most important word that's been used here so far is "namespace". Once you understand that, this "problem" melts into air.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: how do i check if a variable (symbol) already exists (C)

Posted 27 August 2013 - 06:26 AM

View Posttemp_0001, on 27 August 2013 - 12:37 AM, said:

why would the stack size be an issue? i can have one program use all of my ram, so i don't understand why the stack has anything to do with compatibility. in fact, that's up to the developer to use memory (the heap and the rest of the stack), not me.


If you had code that looked like this:
void SomeRecursiveFunction(int depth, ...)
{
    char buffer[512];
    // other variables

    if (depth == 0)
        return;

    // Do work with buffer

    SomeRecursiveFunction(depth - 1, ...)
}

:

SomeRecursiveFunction(MAX_DEPTH, ...)



On a 16 bit DOS system, a Windows 3.1 box, or a *nix box, the maximum value you can pass in to depth is going to be less that what you can pass in on 32 bit Windows box. This is because the default stack sizes on those systems are smaller, and that 512 byte buffer being used on each recursive call will eat into that small stack faster.

View Postjon.kiparsky, on 27 August 2013 - 03:04 AM, said:

I think the most important word that's been used here so far is "namespace". Once you understand that, this "problem" melts into air.


Quite true, but since he is using C, he'll have to create his own namespaces by creative use of identifier names. I remember working on a C project once where all the identifiers were prefixed with TH_ . :)
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: how do i check if a variable (symbol) already exists (C)

Posted 27 August 2013 - 06:32 AM

View Posttemp_0001, on 27 August 2013 - 12:37 AM, said:

Skydiver, it isn't just about my code not being cross-platform.
If i have a variable that has been typedef'ed, and there's another library that the developer is using that uses the same symbol, then their code won't compile even if mine does and is cross platform.

// mylib
typedef int myVar;

//other lib
typedef unsigned int myVar;


Let's say for a moment that you could detect conflicting identifier names at compile time. What would you do? As far as I know there is nothing you could during compile time to modify your code, so you'll have to come up with something pre-compile time. Would your library come with a 'sed' script that goes and renames your identifiers so that the conflict doesn't exist?
Was This Post Helpful? 1
  • +
  • -

#12 temp_0001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 25-August 13

Re: how do i check if a variable (symbol) already exists (C)

Posted 27 August 2013 - 11:53 AM

View PostSkydiver, on 27 August 2013 - 06:32 AM, said:

Let's say for a moment that you could detect conflicting identifier names at compile time. What would you do? As far as I know there is nothing you could during compile time to modify your code, so you'll have to come up with something pre-compile time. Would your library come with a 'sed' script that goes and renames your identifiers so that the conflict doesn't exist?


i would simply remove the variable (or not use it) with a sed script.

i stopped using the stack for arrays. i use the heap instead.
int *pTest = malloc(512*sizeof(int));
if(!pTest) doSomething;


i think it's bad practice to use the stack for arrays. it's more convient, but it takes up stack space...
i'm interested about how much stack space i can use. hm...

Skydiver, I saw that you're a moderator. I checked out your profile, and you've been programming for 33 years? That's impressive. I had a question regarding post titles. Is it possible to change my original post's title to "[solved] previous title"?

This post has been edited by temp_0001: 27 August 2013 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: how do i check if a variable (symbol) already exists (C)

Posted 27 August 2013 - 12:01 PM

Quote

think it's bad practice to use the stack for arrays. it's more convient, but it takes up stack space...


Stack space is inteaded to be used and it's not bad practice to use it. Only time heap allocation should happen is when ...

1) You don't know that amount of memory until run time
2) The size of the memory is relativly large compared to the overall stack space

Allocating unecessary dynamic memory is bad practice, espeically when you know the relativly small amount of memory needed at compile time.

Quote

i'm interested about how much stack space i can use. hm...

It's very unlikely that you would ever blow the top (or really the bottom) off your stack without some sort of infinite recursion or allocating massave amounts of memory on your stack. I wouldn't worry about it.

This post has been edited by jjl: 27 August 2013 - 12:06 PM

Was This Post Helpful? 1
  • +
  • -

#14 temp_0001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 25-August 13

Re: how do i check if a variable (symbol) already exists (C)

Posted 27 August 2013 - 04:57 PM

checking header files for a specific string is easy to do with grep
$ grep -R "size_t" /usr/include



skydiver mentioned that stack space could be small. i don't have a problem with this, because I usually dynamically allocate data if i need a buffer or array.

there is no way to check if a variable is in use using a compiler, so i'll need to just hope that my variables don't clash with other libraries or i can use unique names.

View Postsnoopy11, on 26 August 2013 - 11:27 PM, said:

Also if you used something like CMake to package your Library
you can get CMake to check if a function exists.

check_function_exists ( size_t HAVE_SIZE_T )

for example.


I haven't heard of CMake before. I've heard of Make, but not CMake. Thanks snoopy11. : )
Was This Post Helpful? 0
  • +
  • -

#15 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: how do i check if a variable (symbol) already exists (C)

Posted 27 August 2013 - 05:06 PM

Hmm, your sig says:

Quote

Do you have C or Assembly related questions, PM me
You know Assembly, but have no idea how the stack works? It does not matter if you have 4 gazillion bytes of RAM, the stack does not use your memory like that. Each linker sets the stack size according to the OS your program will run on. I believe the default stack size for Windows is 1MB but this can be changed when you use your linker with command line options.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2