#include
char *code;
void main()
{
char buf[8] = "book";
strcpy(code, buf);
} 7 3415 bl*********@gmail.com wrote: #include
It seems your newsreader ate your include statement. I will assume it
is <string.h> (or even <cstring> with appropriate using declarations).
char *code;
You never initialize nor allocate any memory for code.
void main()
int main()
main() ALWAYS returns an int in conforming code.
{
char buf[8] = "book";
strcpy(code, buf);
You never initialized code, so it is pointing to garbage.
}
Here is a working example, though prefer std::string to raw char*'s.
#include <cstring>
char* code;
int main()
{
char buf[] = "book";
code = new char[std::strlen(buf) + 1];
std::strcpy(code, buf);
delete[] code;
}
--
Marcus Kwok
"Marcus Kwok" <ri******@gehennom.net.invalid> wrote in message
news:dv**********@news-int2.gatech.edu... bl*********@gmail.com wrote: #include
It seems your newsreader ate your include statement. I will assume it is <string.h> (or even <cstring> with appropriate using declarations).
char *code;
You never initialize nor allocate any memory for code.
void main()
int main() main() ALWAYS returns an int in conforming code.
{
char buf[8] = "book";
strcpy(code, buf);
You never initialized code, so it is pointing to garbage.
}
Here is a working example, though prefer std::string to raw char*'s.
#include <cstring>
char* code;
int main() { char buf[] = "book"; code = new char[std::strlen(buf) + 1]; std::strcpy(code, buf); delete[] code; } -- Marcus Kwok
Another way that is simpler is:
#include<iostream>
using std::cout;
using std::endl;
#include <cstring>
int main()
{
char* code;
char buf[] = "book";
code = buf;
cout << code << endl;
return 0;
}
Regards,
JB
> "Marcus Kwok" <ri******@gehennom.net.invalid> wrote in message #include <cstring>
char* code;
int main() { char buf[] = "book"; code = new char[std::strlen(buf) + 1]; std::strcpy(code, buf); delete[] code; }
Jeffrey Baker <tb******@earthlink.net> wrote: Another way that is simpler is:
#include<iostream>
using std::cout;
using std::endl;
#include <cstring>
int main()
{
char* code;
char buf[] = "book";
code = buf;
cout << code << endl;
return 0;
}
Yes, but then if you change an element of code, it also changes the
corresponding element of buf. In my version, code and buf are distinct
entities (that happen to have the same content), so if you change one,
the other is unaffected.
--
Marcus Kwok
"Marcus Kwok" <ri******@gehennom.net.invalid> wrote in message
news:dv**********@news-int.gatech.edu... "Marcus Kwok" <ri******@gehennom.net.invalid> wrote in message #include <cstring>
char* code;
int main() { char buf[] = "book"; code = new char[std::strlen(buf) + 1]; std::strcpy(code, buf); delete[] code; }
Jeffrey Baker <tb******@earthlink.net> wrote: Another way that is simpler is:
#include<iostream>
using std::cout;
using std::endl;
#include <cstring>
int main()
{
char* code;
char buf[] = "book";
code = buf;
cout << code << endl;
return 0;
}
Yes, but then if you change an element of code, it also changes the corresponding element of buf. In my version, code and buf are distinct entities (that happen to have the same content), so if you change one, the other is unaffected.
-- Marcus Kwok
I agree the new operator is good to use. I see a similarity between both
ways. In the "new" you delete the pointer so it no longer exists. In the
other eg. code = buf; , code no longer points to buf like "new" no longer
points to code. In my example code stays pointed to the object till code is
reassigned. That is like reasigning code with new after delete[] code; is
used. code is reassigned.
In both cases there needs to be a number of pointers in my example and with
new it seems to be the same to keep the data otherwise it is destroyed.
Regards,
JB
> news:dv**********@news-int.gatech.edu... "Marcus Kwok" <ri******@gehennom.net.invalid> wrote in message char* code;
char buf[] = "book"; code = new char[std::strlen(buf) + 1]; std::strcpy(code, buf); Jeffrey Baker <tb******@earthlink.net> wrote: char* code; char buf[] = "book"; code = buf;
"Marcus Kwok" <ri******@gehennom.net.invalid> wrote in message Yes, but then if you change an element of code, it also changes the corresponding element of buf. In my version, code and buf are distinct entities (that happen to have the same content), so if you change one, the other is unaffected.
Jeffrey Baker <tb******@earthlink.net> wrote: I agree the new operator is good to use. I see a similarity between both ways. In the "new" you delete the pointer so it no longer exists. In the other eg. code = buf; , code no longer points to buf like "new" no longer points to code. In my example code stays pointed to the object till code is reassigned. That is like reasigning code with new after delete[] code; is used. code is reassigned.
In both cases there needs to be a number of pointers in my example and with new it seems to be the same to keep the data otherwise it is destroyed.
I'm not quite sure what you're getting at here, but here is a program
that demonstrates what I am talking about :
#include <iostream>
#include <cstring>
int main()
{
std::cout << "My way:\n";
char* code;
char buf[] = "book";
code = new char[std::strlen(buf) + 1];
std::strcpy(code, buf);
std::cout << "code: " << code << ", buf: " << buf << '\n';
code[0] = 'n';
std::cout << "code: " << code << ", buf: " << buf << '\n';
delete[] code;
std::cout << "\nYour way:\n";
code = buf;
std::cout << "code: " << code << ", buf: " << buf << '\n';
code[0] = 'n';
std::cout << "code: " << code << ", buf: " << buf << '\n';
}
Output:
My way:
code: book, buf: book
code: nook, buf: book
Your way:
code: book, buf: book
code: nook, buf: nook
--
Marcus Kwok
"Marcus Kwok" <ri******@gehennom.net.invalid> wrote in message
news:dv**********@news-int.gatech.edu... news:dv**********@news-int.gatech.edu... "Marcus Kwok" <ri******@gehennom.net.invalid> wrote in message > char* code; > > char buf[] = "book"; > code = new char[std::strlen(buf) + 1]; > std::strcpy(code, buf);
Jeffrey Baker <tb******@earthlink.net> wrote: char* code; char buf[] = "book"; code = buf;
"Marcus Kwok" <ri******@gehennom.net.invalid> wrote in message Yes, but then if you change an element of code, it also changes the corresponding element of buf. In my version, code and buf are distinct entities (that happen to have the same content), so if you change one, the other is unaffected.
Jeffrey Baker <tb******@earthlink.net> wrote: I agree the new operator is good to use. I see a similarity between both ways. In the "new" you delete the pointer so it no longer exists. In the other eg. code = buf; , code no longer points to buf like "new" no longer points to code. In my example code stays pointed to the object till code is reassigned. That is like reasigning code with new after delete[] code; is used. code is reassigned.
In both cases there needs to be a number of pointers in my example and with new it seems to be the same to keep the data otherwise it is destroyed.
I'm not quite sure what you're getting at here, but here is a program that demonstrates what I am talking about:
#include <iostream> #include <cstring>
int main() { std::cout << "My way:\n"; char* code; char buf[] = "book"; code = new char[std::strlen(buf) + 1]; std::strcpy(code, buf); std::cout << "code: " << code << ", buf: " << buf << '\n'; code[0] = 'n'; std::cout << "code: " << code << ", buf: " << buf << '\n'; delete[] code;
std::cout << "\nYour way:\n"; code = buf; std::cout << "code: " << code << ", buf: " << buf << '\n'; code[0] = 'n'; std::cout << "code: " << code << ", buf: " << buf << '\n'; } Output: My way: code: book, buf: book code: nook, buf: book
Your way: code: book, buf: book code: nook, buf: nook
-- Marcus Kwok
Thanks,
There is definitely more stability with the new operator.
Regards,
JB This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: MFA |
last post by:
Hi All
Thanks to all who replied to my question dated 16/10/2003 with the same
subject..
In all replies I got every one is saying that I am using cint() etc. But i
am not using any convert...
|
by: Mark |
last post by:
Could someone please give me a hand?
I have created a navigation bar for a Web site I assist with.
Everything works fine if I use the files while offline, i.e., the files that I
created are...
|
by: Prabh |
last post by:
Hello,
I am wondering whats wrong with the following code. It is crashing on
Linux, but works fine on AIX.
The function basically get DbNm@Srvr string and then return DbNm and
Srvr back to...
|
by: Sourin |
last post by:
Hi all,
I am trying to write code for my experiments. My work involves huge
datasets, and as such my code needs to be memory efficient. I did some
hand calculations regarding the amount of...
|
by: ais523 |
last post by:
I use this function that I wrote for inputting strings. It's meant to
return a pointer to mallocated memory holding one input string, or 0 on
error. (Personally, I prefer to use 0 to NULL when...
| |
by: Sharath A.V |
last post by:
I had an argument with someone on wheather this piece of code can
invoke undefined bahaviour.
I think it does not invoke any undefined behaviour since there is
sufficient memory space of 9...
|
by: Snis Pilbor |
last post by:
Howdy,
Forgive me for not looking this up in the manuals or just trying
it outright, as I don't have access to a shell right now. Anyway, I
thought of this while juggling earlier: it would be...
|
by: Chris Thomasson |
last post by:
I am thinking about using this technique for all the "local" memory pools in
a paticular multi-threaded allocator algorithm I invented. Some more info on
that can be found here:
...
|
by: Filimon Roukoutakis |
last post by:
Suppose that we have a function
f(Object*& obj)
and have declared a global std::vector<Object*vec;
Is it valid to do
void g() {
vec.push_back(new Object);
|
by: ertis6 |
last post by:
Hi all,
I need to calculate a value inside 8 nested for loops. 2 additional
for loops are used during calculation. It was working fine with 4
loops. My code is like this:
...
for(int i1=0;...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |