Ignoring other considerations for a moment and in general ...
Would there be a difference in result (dot oh or execution) of:
A.
vs.
B.
My general understanding is (at least I think my understanding is) that in cases where something is inherently fixed in size then the compiler inserts the literal value of the size of the item at compile time as opposed to leaving an implicit run-time function call.
If my understanding is correct then it seems in scenario A. we'd have strncpy( a, b, 4 ) in one line of execute compared to two lines in B.: c = 4; strncpy( a, b, c ).
Is that not correct?
Am I missing something?
Thanks in advance for any comments.
Geo. Salisbury
Long Valley, NJ
Last edited by GSalisbury; 12-14-2015 at 05:04 PM..
Reason: Grammer
1. & 2. Yes
and
have been included.
3. An example a might be char
.
4. An example c might be
.
5. Using gcc on RedHat Linux boxes.
What I was after is what, if any, would be the virtue of using:
vs.
I'm not a C maven (I can kludge along given good consult) but it seems to me that using the sizeof value in a separate int adds overhead while not altering the execution result.
I appreciate that we're talking fractional nano-seconds and negligible amounts but am interested in clarifying the precept.
Using:
is wrong, but won't actually hurt you unless a contains more bytes than fit in an object of type signed int. If you're using an external object to store the results of the sizeof operator, its type should be size_t; not int.
Note that if you are in a subroutine that has been passed a pointer to an array of characters to be copied into an area of memory pointed to by another pointer to an array of characters, you have to be also be given the size of the destination array, as in:
because using:
gives you the size of the pointer to; not the size of the array of characters pointed to by to.
But, as long as the compiler knows the size of the destination object as in:
there is no need to create a variable to hold the result of the sizeof operator before calling strncpy().
Last edited by Don Cragun; 12-14-2015 at 06:51 PM..
I have to bail on this for now - will pick it back up tomorrow after considering your comments.
Thx.
Geo.
---------- Post updated 12-15-15 at 05:19 PM ---------- Previous update was 12-14-15 at 05:50 PM ----------
Finally able to return ...
The ability of 'c' to hold the
would not be a concern as 'a' is defined as an eight character, fixed-length string
for example.
All is happening in a closed domain of a running application. The present method in place is the one-step
. The two-step approach came up as quote better but it struck me as accomplishing nothing different and "cost" the teensy bit of the size of the default int.
We'll leave the strncpy... as is with the sizeof... as one of the arguments.
Thanks for your thoughts.
Geo.
You seriously don't need to worry about overhead of an extra line of code.
Lets do some rough guess calculation here...
What you got a 2GHZ processor? That's 2000,000,000 cycles per second.
Lets say that extra line adds 20 cycles on the program. That's a 10 millionth of a second.
Reading a few bytes from a disk may well take in the order of milliseconds.
If you think it makes the program clearer then do it. I routinely add extra steps and variables to make my code more readable and I deal with vast amounts of 24 hour streaming data.
And my stuff flies.
FWIW,
The original query was not overtly based upon concerns of overhead or performance per se but, rather, [possible?] quote technical differences in compiled result.
The illustrated fragments using sizeof as arguments were in place in an area that was already selective in execution and time and volume did not come into play.
A discussion had started on the merits of an argument vs. a variable and the posting here was more for enlarging the set of opinions. I, too, will often include additional steps etc. in order to make a sequence more clear or to be able to include some step-wise commentary.
The upshot was we (I) left the use of the sizeof as arguments in place as is principally because we (I<g>) didn't have to do anything (Management 101 - 1st precept - do nothing) and because the use was in a block that already well commented leaving no ambiguity.
Hai
how to find size of a file??
ex : /home/kiran/pdk/sample/calibre this is a path
In that I have to find size of a files in side a calibre(it is the folder) like .results or .summary (1 Reply)
Hi,
I have defined the class and call the sizeof(object to class) to get the size.
# include <iostream>
# include <iomanip>
using namespace std;
class sample
{
private:
int i;
float j;
char k;
public:
sample()
{
} (2 Replies)
hi,
can we install gcc compiler in unix based OS(sun solar,IBM AIX,HP,etc) and also
can we install sun cc compiler in AIX environment and vice versa.
and more ..is linux support cc compiler
regards
Ajay (3 Replies)
Hi,
There are some bewildering sizeof() questions I have in my mind. Could anyone shed some light on this?
int main() {
printf("%d\n", sizeof(main)); // Ans: 1
}
That is, the sizeof() a function identifier though it is treated internally as a pointer gives 1 byte always, why?
... (5 Replies)
The below code throws the error, since the size of x = 19 is not passed to the cstrCopy function.
using namespace std;
static void cstrCopy(char *x, const char*y);
int main ()
{
char x;
const string y = "UNIX FORUM";
cstrCopy(x,y.c_str());
return 0;
}
void cstrCopy(char *x,... (3 Replies)
Hi All,
is it possible to find out the size of an array of structures ( without using 'sizeof' operator). The condition is we have the array of structure instant but we are not aware of the elements inside the structure.
Can someone help me out?
Thanks in advance. (18 Replies)
To make a programming language you need a compiler, so what was the first programming language and how was is created if you need the compiler first?
The compiler itself is considered as a high language comparing to the machine! since the compiler is not created in 1's and 0's...
Eventhough i... (12 Replies)
Hi,
we are converting from IBM-AIX(xl c/c++ compiler) to Linux(GCC complier).
As a part of this i need to change the CFLAGS.
The xl c/c++ complier CFLAGS is
CFLAGS := $(CDEBUG) $(PROJECT_INCLUDE_DIRS) $(COBJECT_MODE) -qcpluscmt -qmakedep -qcheck=all \
-qalign=bit_packed $(LINT_FLAGS)... (0 Replies)
we know that sizeof never returns zero when used with structure
then why in this case it is returning zero
struct foo
{
char c;
};
void main()
{
struct foo f;
cout<<sizeof(f);
}
i am working on solaris 5.8
isn't the above function should return the size of empty structure (7 Replies)