472,365 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,365 software developers and data experts.

this code is memory overflow or not?

#include

char *code;

void main()

{

char buf[8] = "book";

strcpy(code, buf);

}

Mar 16 '06 #1
7 3301
bl*********@gmail.com wrote:
#include

char *code;

void main()

{

char buf[8] = "book";

strcpy(code, buf);

}


See FAQ 7.1 in comp.lang.c:

http://www.faqs.org/faqs/C-faq/faq/index.html

Cheers! --M

Mar 16 '06 #2
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
Mar 16 '06 #3

"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
Mar 16 '06 #4
> "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
Mar 16 '06 #5

"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
Mar 16 '06 #6
> 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
Mar 16 '06 #7

"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
Mar 16 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
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...
2
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...
3
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...
3
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...
8
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...
19
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...
5
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...
22
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: ...
30
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);
5
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;...
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...
0
hi
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...
1
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...
0
Oralloy
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++...
0
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...
0
BLUEPANDA
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...
0
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...
1
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...
0
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.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.