Sizeof(a) must be 4 bytes, but why does the overflow occur when I enter at least over 32 characters?! - // HHHe...
-
#include <iostream>
-
using namespace std;
-
-
int main() {
-
char a[4];
-
string str;
-
-
cin >> str;
-
cin.ignore();
-
-
int i;
-
for(i=0; i<(str.length()); i++) {
-
a[i] = *(str.c_str()+i);
-
}
-
-
/* char * p = &(a[7])+i;
-
cout << *p << endl; */
-
}
Thanks for help...
5 2236
yes your char array variable is 4 bytes. it mean it only can hold 3 character and the 4th character will be used null terminating character. you can also use array a cleverly to hold just 4 byts character.
and why do you think that array will hold 32 character? one byte can hold only one character. One byte mean 8 bit (depending on processor) but its never 8 character
-
(gdb) run
-
Starting program: /home/ramsin/Desktop/a.out
-
AAAABBBBCCCCDDDD
-
-
Program exited normally.
-
(gdb) r
-
Starting program: /home/ramsin/Desktop/a.out
-
AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD
-
-
Program exited normally.
-
(gdb) r
-
Starting program: /home/ramsin/Desktop/a.out
-
AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDF
-
-
Program received signal SIGSEGV, Segmentation fault.
-
0x00272c49 in __libc_start_main () from /lib/libc.so.6
-
Oralloy 988
Recognized Expert Contributor
Ramsin,
What is happening is that you are blindly writing on the stack, and for whatever reason have 30-something bytes that you can destroy before you reach a critical value.
The error is because you have corrupted a stack framing pointer or a return address; thus, when the execution leaves your main() function, the CPU tries to access memory that is not in your assigned memory space. Thus the segmentation fault.
The reason you get 32 characters is system and complier dependant. On some systems you might only get five characters before having troubles.
So yes, you can easily exceed the bounds of variables using C/C++. In fact, some programs intentionally use the programmer's knowledge of memory lay-out in order to function correctly. However, if you aren't writing high-performance binary communication protocols, you probably don't need to do such things. Just say "No!".
I hope this helps you a little.
Cheers,
Oralloy
I thought it was also system/compiler dependant, but I was unsure. Anyway, I must learn about such things, because it's necessary to clearly understand how memory works.
Oralloy 988
Recognized Expert Contributor
Ramsin,
Memory schemes depend on what level you have to know it at. There is a huge difference between using new/delete and what the operating system has to do to allocate virtual memory.
At the risk of confusing you - understand what it is you need to know about memory.
If you are doing application programming, that's one thing.
If you are doing binary network communication, that's another.
If you are writing an operating system, that is yet another.
Stay current with your coursework and you should be fine.
When you write you programs, never assume that you can go outside the boundaries of the memory allocated to your program. Once you do, you immediately enter the realm of system and compiler specific behaviour.
Good Luck!
Oralloy
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: poifull |
last post by:
Hi All,
What is the proper way to read a binary file into a byte?
I am using BinaryReader to read from a Stream and call the ReadByte method
of the BinaryReader object.
The method I'm using...
|
by: Tim |
last post by:
Hi,
I'm experiencing some problem with the following code:
st = File.Open(sFilename, FileMode.Open, FileAccess.ReadWrite)
br = New BinaryReader(st)
Do Until br.PeekChar = -1
Dim buffer()...
|
by: writeson |
last post by:
Hi all,
I have PHP version 4.4 installed on a Soaris 2.6 system and I'm getting
strange results doing math/boolean operators. I have a hashing
algorithm that does multiplication, bitwise...
|
by: Lee Crabtree |
last post by:
I need to shift all of the values in a byte array by more than 8 bits,
meaning that values should flow from one byte to another. Since I don't
know in advance how many bits will be shifting, I...
|
by: Sam Durai |
last post by:
Hello, A particular select query took unusually long time to execute
hence I took an app.snap to find out what happens internally and I
found out that tablequeue buffers are overflowing on a...
| |
by: Mike |
last post by:
Hello All,
Please, if anyone can point me to the problem, I'd sure appreciate it!
I am very new to VB programming and not a programmer to begin with.
This is part of a Visual Basic 2005 Express...
|
by: amit.atray |
last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc)
having some Old C-Code (ansi + KR Style) and code inspection shows
some big size variable (auto) allocated (on stack)
say for ex.
char...
|
by: seema |
last post by:
Hi all,
How to calculate upper 4 bytes and lower 4 bytes of a value? Can
somebody please explain??
say for example,
unsigned int scnlen_lo;
int scnlen_hi;
unsigned long long scnlen;
...
|
by: Angel Tsankov |
last post by:
Hello!
Does the C++ standard define what happens when the size argument of void*
operator new(size_t size) cannot represent the total number of bytes to be
allocated? For example:
struct S
{...
|
by: Db2NewComer |
last post by:
Hi all,
I'm currently facing a problem with a Db2 database.
When I try to activate / connect to it, following error occurs :
---------------------------------------------------------...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |