generating new variables in c [duplicate] - c

This question already has an answer here:
Creating variable names from parameters
(1 answer)
Closed 8 years ago.
Is is possible for your code to generate new variables in c? For example, if I made "example_variable = 15", is there any way to automatically generate 15 new variables such as: "generated_variable_1", "generated_variable_2", "generated_variable_3", all the way to "generated_variable_15"?
I'm very new to c, and I haven't had a proper introduction to it, so I only know the basics, especially when it comes to variables. I am pretty sure this is really high-level stuff, so I'm sorry if the question doesn't make sense. I am open to any suggestions for alternate ways of generating the variables.
I know there are probably answers already out there, but I've had trouble finding them and would like answers specific to what I'm looking for, as opposed to piecing together what I need from what I can find.

What you are talking about - generating variables at runtime - is not possible in C. The reason is that C is a low-level language and does not expose an API for runtime manipulation. In fact, once compiled, C programs don't use variables - are values are stored directly in memory using memory addresses.
The closet equivalent to what you're looking for that's available in C is an "array". To declare an array, you can do:
int var[15];
int var2[n]; // in C99+, n is a variable saying how many elements you want in the array
You can also do this with malloc, but this is a bit more complicated and then you must free the values.

A running C program doesn't use your variable names at all. Those names were useful for the compiler to build the program, but are discarded before you run it. This means that in C (but not in interpreted languages like python):
If you rename your variables, you get the exact same program
If you do strings <your program> you won't see any variable names (unless you retained debugging symbols)
Hence, runtime is too late to create new variables. In C, variables are compile-time only. Of course, you can use arrays, or dictionaries, to simulate run-time variable creation, like the other answer, and a few commenters, suggest.

Related

Which number, 0 or 1, should I define the variabliable of loops for arrays from?

I'm a beginner in data structures. I learnt that SqLists are starting from 1, when the arrays inside SqLists are from 0.
The question is if I create a loop, which of the following representation is better? Will the other one be weird?
//i=0 according to arrays
for(int i=0;i<L->length;++i)
L->data[i]=givenArray[i];
//i=1 according to SqList
for(int i=1;i<=L->length;++i)
L->data[i-1]=givenArray[i-1];
There is nothing wrong with either ways.
With that aside, the first way of writing is more clear and often used when writing programs (in any language).
If you are programming alone, it doesn't really matter, just pick your style and stick to it.
But, if you want to adhere to global programming conventions, and make your code more readable and accessible to other people, stick for the first style.

`System.CopyArray` vs `System.Copy`?

Apart from the obvious difference that the first deals only with arrays, don't they do the same thing? I keep reading the help pages for the two functions, and can't understand when should I use one over the other and why. Internet search seems to indicate only the second is used whatsoever for array copying purposes if not written using loops.
System.Copy is really compiler magic. It's applicable both to strings and to dynamic arrays.
Compiler chooses needed version of intrinsic routine (different for short strings, long strings, dynamic arrays) and substitutes your call of Copy.
For dynamic arrays _DynArrayCopyRange prepares memory, provides reference counting, and calls System.CopyArray for deep copy of elements.
Usually you don't need to call the last procedure expicitly, it is compiler prerogative.

How give array name from string variable in C [duplicate]

This question already has answers here:
Variable with char[] name
(3 answers)
Closed 8 years ago.
I am stuck with a situation where I need to give array name from string variable.
Basically I want to create an array with same name as value in another string variable "name":
char *name="arr_name";
In my case the string being hold by variable name may change. Hence advice accroding.
Thanks!
I think you're looking for some mechanism, similar to the ones found in the higher level languages as in python (introspection), or C# (reflections). C doesn't provide this kind of insight from the runtime, not even the variable names are existing in the bytecode - so basically it's not possible and doesn't make any sense in the terms of the way how C works.
I don't know if that helps, but one thing you could do is to statically (so in the compilation time, not while it's running!) populate char* and create variable with the same name, given that the value of the string is a proper name for the variable (Naming convention for C/C++). You can achieve that by defining a proper macro (#define your_macro(...) code_to_populate_char_and_declare_variable), but I cannot see any point in doing so.

How to use pointers to Ruby objects safely from within C-based extension?

I am considering writing a C-based Ruby gem to speed up text wrapping in Prawn. I've read a very small portion of the C source for MRI before, but don't know the API for building extensions well yet.
Within my C code, I'd like to get a direct pointer to the data within a Ruby String, and walk over it byte by byte. Further to that, I'd like to store pointers within the buffer in my own struct, and use them not only within the scope of a single call, but within subsequent calls into the extension code.
Is this possible? Could the GC move the Strings, rendering my pointers invalid? And also, how can I let Ruby know that I am holding pointers to the Strings in my own structs (so the GC doesn't try to reclaim them)? Can this code be written in a way which is compatible with both MRI 1.8 and 1.9?
And since I'm asking about using pointers safely in a C-based Ruby extension: can I use malloc and free the same as I would in a "regular" C-based project?
The link that matt offers is really good. It would have saved me days if I had found it before.
You can keep references to ruby Strings and pointers into them. I would suggest freezing the String. Then every attempt to change the string will fail. There is a function Data_Wrap_Struct() that lets you wrap your own data structure into a Ruby object. Beside the data structure and the class of the structure, the function takes two function arguments. One of them (mark) is used to show the garbage collector where your structure references other ruby objects.
What took me some time to understand, is that the garbage collector is really scanning the stack of all ruby threads to seek for references to ruby objects. So keeping VALUEs on the stack is also a safe method to keep objects referenced.
Can this code be written in a way which is compatible with both MRI 1.8 and 1.9?
The basic API for extensions didn't change very much (I think) from 1.8 to 1.9. But I've used only 1.9 so far.
can I use malloc and free the same as I would in a "regular" C-based project?
Sure, I cannot think of any reason why this should not possible, as long as you don't expect the garbage collector to keep care of the allocated memory.
I had a hard time, mixing C++ code, compiled with another version of gcc than the version the ruby interpreter was compiled with. If you experience strange startup behavior, I would check for compiler version differences.

Do Perl's sigils have anything to do with memory allocation? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why do Perl variables need to start with $, %,#?
Other scripting languages seems to get along just fine without this or something similiar.
I guess it has something to do with memory allocation and helping the interpreter in order to speed things up, but I couldn't find anything specific on it. $scalar would probably be put into stack, #array into heap and %hash? Into heap as well? And what about ?subroutine?
Could someone help me figure this out or point me to some documentation? I am still trying to grasp some fundamentals and understand how everything works under the hood...
Because it makes it easier to read.
You know which identifiers are nouns, and whether they're singular or plural, because of the sigaldry. It's the same reason in English we have singular and plural determiners and agreement, as in this species is vs these species are. It's nice to know which is which.
Perl stores all data associated with a name in a single symbol table entry. The structure stored there is called a typeglob. The values for $foo, #foo, %foo, and &foo (subroutine reference) are all stored in the typeglob for "foo". The entire typeglob is denoted *foo (where * indicates "all sigils"). All this is explained in the perldata section of the Perl documentation.

Resources