Anonymous
how about this ? Assignment in
char c;
int * ip = & c; / * FALSE * /
is in error; you cannot mix char pointers and int pointers
like this.
How, then, is it possible to write?
char * cp = malloc (10);
int * ip = malloc (sizeof (int));
without error on any line?
vinícius*
vinícius*
char * cp = malloc (10); works as intended because (in most systems) a char is a byte in size
su
vinícius*
so that 10 = 10 * sizeof(char)
Wisenky
src/car.o:car.cpp:(.bss+0x0): multiple definition of `Map::map'
src/main.o:C:\..\workspace\vcar_cpp_logic/inc/map.h:66: first defined here
collect2.exe: error: ld returned 1 exit status what does that mean ?
V01D
Wisenky
su
vinícius*
Wisenky
Can I share source with you by paste ?
V01D
su
su
sometimes it is a good to go
ifndef _map_h_
define _map_h
some shit
endif
su
or pragma
su
also there are some shitty trics from microsoft
su
if you are using one
su
in general use cmake, that helps a lot
Wisenky
pastebin.com
https://pastebin.com/3CKG5G7Q : car.cpp https://pastebin.com/LHw3N9Tz : map.h https://pastebin.com/V8z3zTFP : car.h https://pastebin.com/ium1yeY5 : main.cpp
Anonymous
/warn
Wisenky
Wisenky
Emir
#include <iostream>
struct A {
A() {
std::cout << "ctor\n";
ptr = new int;
}
~A() {
std::cout << "dtor\n";
delete ptr;
}
int *ptr;
};
int main()
{
A * ptr = new A;
delete ptr;
}
is this code have a memory leak?
vinícius*
vinícius*
==12963== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Emir
Wisenky
su
su
Wisenky
Anonymous
Anonymous
Output is this could u explain (for(j=n;j>i;j--)part , i did not understand the algorithm
Dima
these are simple loops. in the second nested loop you can see that it starts from ’n’ index, which is the main loop
Anonymous
I know these are simple but why is it ok to ise j twice,i expected it to cause a problem
Shashank
in the first part, you are initialising it to a value
Shashank
In the end you tell it what to do every iteration(cycle of operation)
Hadaward 'Solly'
you control the process (your program) memory. As long as scope (and types, in C and other language's case) are respected, you can reassign values to a variable.
Hadaward 'Solly'
the hard part is actually knowing how this benefits you. One benefit is "variable reusability".
Hadaward 'Solly'
that's why it's called computer "science"
Dima
Anonymous
Hadaward 'Solly'
always verify declaration. If it's declared inside a loop. After loop escapes, bye bye variable. That might not be true in certain environments and languages. But in C this is how it works
Hadaward 'Solly'
for instance compare these two:
1.
for (int i = 0; i < 10; i++) {}
2.
int i;
for (i = 0; i < 10; i++) {}
Anonymous
Hadaward 'Solly'
"some of gcc version" sounds too imprecise and will give you a lot of headache
Hadaward 'Solly'
you have to check the docs whenever necessary, newbie or not
Anonymous
Hadaward 'Solly'
we are back to scopes
Hadaward 'Solly'
the value of variable will be lost after leaving that scope if its declaration was inside it
Hadaward 'Solly'
since you mentioned memory allocation, i assume you already touch malloc. In that case, mallocing without deleting in the end of scope will result in memory leaks, if i'm not mistaken
Hadaward 'Solly'
i said delete but i mean free smh
Hadaward 'Solly'
C++ bad influence got me good
Anonymous
Wisenky
olli
a function prototype is a forward declaration, however a forward declaration can also declare variables or types.
olli
between what?
olli
one is a subset of the other
olli
> a function prototype is a forward declaration, however a forward declaration can also declare variables or types.
olli
you can forward declare types, variables or functions (function prototype)
olli
these are both forward declarations, one of them is a function prototype.
struct Foo;
struct Foo* Bar();
Anonymous
It's a definition
Anonymous
extern int a;
That is a declaration
Anonymous
olli
Well, both are definitions
if that would be the case, why are 2 defintions allowed but not 3?
int a;
int a = 123;
int a = 123;
<source>:3:5: error: redefinition of 'a'
3 | int a = 123;
| ^
<source>:2:5: note: previous definition of 'a' was here
2 | int a = 123;
| ^
Compiler returned: 1
olli
exactly, so I'd argue int a; cannot be treated as defintion in this example, because the following is valid C.
int a;
int a = 123;
olli
do you compile it as C++ or C? it's an error in C++
Anonymous
olli
olli
https://godbolt.org/z/vo78Gv
olli
https://godbolt.org/z/7j9odx
can't confirm, GCC, Clang and MSVC do not report any warnings and compile it fine
Anonymous
olli
So stupid
I won't deny - C can be weird
Vlad
olli
fwiw https://en.cppreference.com/w/c/language/extern
Anonymous
I won't deny - C can be weird
$ cat t.c
int a;
int a = 123;
int main(void){
}
$ gcc t.c
$ g++ t.c -std=c++17
clang-10: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
t.c:2:5: error: redefinition of 'a'
int a = 123;
^
t.c:1:5: note: previous definition is here
int a;
^
1 error generated.
That's why I like C++