declaration

How can I declare a variable at an absolute address with GCC?

偶尔善良 提交于 2019-12-13 02:30:09
问题 We are looking at how the linker works in one of my courses and one of the assignments is a little exercise involving the nm command. essentially we just want to match the type and the value printed by nm for each variable. for example: char* B = NULL; would give the address (irrelevant) then B B. I've done this successfully for all the labels we needed to except for A. I have read that this simply means the value is absolute and cannot be changed by the linker. I have experimented with many

In C,why is definition of a global variable in a separate statement raising warning,but is OK for a local variable?

£可爱£侵袭症+ 提交于 2019-12-12 18:42:37
问题 In the following code, why does the definition of the global variable "x" show the warning "data definition has no type or storage class" but the same thing works fine for the local variable "y"?All I am doing for each variable is first declare them in one statement and then define them in another statement.What is the difference that it works fine for one but shows warning for the other? #include<stdio.h> int x; x=303; int main(void) { int y; y=776 ; printf("The value of x is %d,and of y is

Understanding C++ std::shared_ptr when used with temporary objects

自古美人都是妖i 提交于 2019-12-12 17:19:06
问题 I am trying to understand the use of a shared_ptr p when it is used in the construction of an unnamed shared_ptr and the effects this has on p . I was toying with my own examples and wrote the following piece of code: shared_ptr<int> p(new int(42)); cout << p.use_count() << '\n'; { cout << p.use_count() << '\n'; shared_ptr<int>(p); cout << p.use_count() << '\n'; } cout << p.use_count() << '\n'; Output: 1 1 0 1 Is it correct that line 5, uses p to create a temp. shared_ptr (i.e an unnamed

Problem with declaration of an array of pointers

扶醉桌前 提交于 2019-12-12 16:50:07
问题 When i execute this code #include<stdio.h> int main() { int (*x)[5]; printf("\nx = %u\nx+1 = %u\n&x = %u\n&x + 1 = %u",x,x+1,&x,&x+1); } This is the output in C or C++: x = 134513520 x+1 = 134513540 &x = 3221191940 &x + 1 = 3221191944 Please explain. Also what is the difference between: int x[5] and int (*x)[5] ? 回答1: int x[5] is an array of 5 integers int (*x)[5] is a pointer to an array of 5 integers When you increment a pointer, you increment by the size of the pointed to type. x+1 is

C++: Linking files with GCC compiler

僤鯓⒐⒋嵵緔 提交于 2019-12-12 16:41:10
问题 I have three files : myh.h; my.cpp; use.cpp. Here are the contents of the files: myh.h extern int foo; void print_foo(); void print(int); my.cpp #include "myh.h" #include <iostream> void print_foo() { std::cout<<foo<<std::endl; } void print(int i) { std::cout<<i<<std::endl; } use.cpp #include "myh.h" int main() { foo=7; print_foo(); print(99); return 0; } GCC spews out the following error: my.o:my.cpp:(.text+0x7): undefined reference to `foo' use.o:use.cpp:(.text+0x10): undefined reference to

C++ standard definition of conflicting declarations

柔情痞子 提交于 2019-12-12 15:17:39
问题 Where does the standard define what a conflicting declaration is? For example, if I have, at namespace scope, the following declarations: extern const int a; extern int a; this would be an example of conflicting declarations . 回答1: According to [dcl.type], the cv-qualifier const is part of the type, therefore const int x; and int x; constitute different declarations of the variable x . Then we arrive at [over]/1, which states that: When two or more different declarations are specified for a

Typescript declaration: Merge a class and an interface

送分小仙女□ 提交于 2019-12-12 14:48:09
问题 I have two models Model and its subclass ClientModel an ambient module. Now I want to declare a set of attributes of ClientModel from an interface so called Client . How can I do it? I can imagine something like this: interface Client { name: string; email: string; } declare class ClientModel extends Model implements Client { // with name and email here without redeclare them } 回答1: You can use declaration merging. If the class and the interface are declared in the same namespace/module and

Why C++ variable doesn't need defining properly when it's a pointer?

僤鯓⒐⒋嵵緔 提交于 2019-12-12 14:27:49
问题 I'm completely new to the C++ language (pointers in particular, experience is mainly in PHP) and would love some explanation to the following (I've tried searching for answers). How are both lines of code able to do exactly the same job in my program? The second line seems to go against everything I've learnt & understood so far about pointers. char disk[3] = "D:"; char* disk = "D:"; How am I able to initialize a pointer to anything other than a memory address? Not only that, in the second

Scientific Fortran Compile Error

安稳与你 提交于 2019-12-12 12:10:40
问题 I'm working on scientific modelling program and have yet to get my program to even compile. I haven't touched the code which my professor insists previously worked, only the makefile. After many attempts, the furthest I've gotten was this error: Error on line 1112: Declaration error for xxmf: adjustable dimension on non-argument upcase: intrpl: splin: mtrnpr: My Professor insists that it's merely a compiling problem and that there should be some option involving global variables that I can

Should I declare system call functions in C?

我的梦境 提交于 2019-12-12 12:06:58
问题 I read this answer: Must declare function prototype in C? My question is more specific: In a program that uses system calls like access() , open() , creat() , write() , read() ... Must I declare every system call function? Is that how C works? Because I'm getting the following: hw1.c: In function ‘main’: hw1.c:50:9: warning: implicit declaration of function ‘access’ [-Wimplicit-function-declaration] hw1.c:131:9: warning: implicit declaration of function ‘lseek’ [-Wimplicit-function