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 2246
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 leads to the second question. I got the "Conversion
buffer overflow" error when I run the following code:
Stream s = openFileDialog1.OpenFile();
|
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() As Byte = br.ReadBytes(1024)
...
|
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 operations and mod() calls
interatively on the characters of a filename. On a linux platform the
multiplications start to overflow (as I expect) and the overflow is
just dropped. The lower three bytes are masked off like this:
$result = $result &...
|
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 can't do something
easy like putting the bytes into a long or uint and shifting that. Let
me give an example:
If I want to shift this:
10010110 00001101
|
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 particular node
of my logically partitioned db having 8 partitions.
Current number of tablequeue buffers overflowed = 147737
Total number of tablequeue buffers overflowed = 190224
Maximum number of tablequeue buffers overflowed = 189683
...
| |
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 Edition program to control a remote basketball
scoreboard display unit.
All I'm trying to do is add 5 byte variables and store the result in an integer
variable. I added a Try/Catch block to take look at things.
This exception occurs only when...
|
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 a;
(this type of code and other complex mallc/free etc is used frequently
and total approx 450 files, each file is of approx/avg 1000 line,
|
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;
scnlen=888888;
|
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
{
char a;
};
|
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 :
---------------------------------------------------------
"SQL1226N The maximum number of client connections are already
started.
SQLSTATE=57030"
---------------------------------------------------------
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |