Public Group

# what languages force you to declare your variables in a data segment

This topic is 4605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm learning assembly, and very unlike c++ I can't just declare variables anywhere in my code, well I can but it will cause all sorts of weird crashes, which I don't really understand. I heard if your using a c only compiler, all your variables need to be declared at the top of a code block segment, is this true? I'm curious to what other languages force you to declare your variables in a special section of your code.

##### Share on other sites
Some versions of C (depending which revision) force you to declare your data at the beginning of an instruction block (not necesarly at the beginning of a function).
Later C revisions will let you declare it allover, but I personally think that is a bad practice.

As for ASM, depending on the CPU you are using and the OS, the data doesn't have to be in the data segment, it can be in any segment. That's how you can write self modifying code, or how you load a program from a file to execute.

##### Share on other sites
Thanks raduprv, could you please elaborate on why you think its bad practice for c to be able to declare its variables anywhere?

##### Share on other sites
masm forces u to declare your varibles in the .data area.
whereas fasm does not you can declare them wherever you want.

jmp end_Data
; data here
end_Data:

that way fasm doesn't try to use my varibles as instructions.
i haven't done inline asm yet. so i don't know how that goes.

i don't like declaring varibles all over the place because, if one varible holds a bad value. or gets modified incorrectly, you then have to hunt that varible down within god knows how many lines of code.
its cleaner and easier to declare them in an include file.
or at the beginning of functions/ start of the program.

but each to their own.

##### Share on other sites
Iirc, COBOL and Fortran force you to declare variables in special sections of code. Visual Basic does too.

##### Share on other sites
In C or C++, (global and static) variables are physically placed in the data section regardless of where you declare them.

In many other languages, variables are created dynamically.

Assembly is not like this.

In assembly, you don't really declare a variable, you're just reserving some memory and declaring a label. A label is not intrinstically a variable, rather just a pointer to somewhere in memory, which may be code or data (or anything else). A label is only a variable if you use it as one.

Mark

##### Share on other sites
Quote:
 Later C revisions will let you declare it allover, but I personally think that is a bad practice.

Quote:
 Thanks raduprv, could you please elaborate on why you think its bad practice for c to be able to declare its variables anywhere?

Some programmers think that the code becomes harder to read if the variables declaration are spread along a function, while having them all grouped in the same place is easier. As for me, I'm half-half: I group the variables declaration into logical units in a function, and put them at the beginning of their unit.
But I find usefull to let the programmer choose where to declare the variables:
void foo(int *buffer){if(!buffer) return; SlowConstructor a(2.0, "this class has a complex and slow constructor", new vector3D(1.0, 2.0, 3.0));SlowConstructor b(2.0, "this class has a complex and slow constructor", new vector3D(1.0, 2.0, 3.0)); SlowConstructor c(2.0, "this class has a complex and slow constructor", new vector3D(1.0, 2.0, 3.0));...

In this case, it would be bad (IMO) to declare a,b,c befor checking the pointer, and whenever the utilization of a var depends from a condition (which is not said that uses a code block, like in this example), it is better to declare the variables after the condition. In addition, often one needs many vars in a function, and I find annoying to have 20 vars in the head of a function.

##### Share on other sites
There are two reasons why I like them at the beginning of the function:

1. It is easier to see, at a glance, what variables are used in the function.
2. Makes it a little bit harder to define a variable in two places in the same function. For example:
function_blah(){//stuffint i;//use i for some stuff//later on:while(blah){int i=something;}}

##### Share on other sites
In NASM, you can do something like
[section .TEXT]; code here[section .DATA]dd variable[section .TEXT]; more code

and nasm sorts them out. You could code a macro to simplifly the process.
DISCLAIMER: I don't know if this is correct NASM syntax.

##### Share on other sites
Quote:
 Original post by LessBreadIirc, COBOL and Fortran force you to declare variables in special sections of code. Visual Basic does too.

COBOL yes. Fortran, I don't know. VB: Not since I've started using it (6.0).

• 17
• 11
• 12
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999756
×