#include
char *code;
void main()
{
char buf[8] = "book";
strcpy(code, buf);
} 7 3301 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: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |