473,508 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overflow and bytes

15 New Member
Sizeof(a) must be 4 bytes, but why does the overflow occur when I enter at least over 32 characters?!
Expand|Select|Wrap|Line Numbers
  1. // HHHe...
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6.     char a[4];
  7.     string str;
  8.  
  9.     cin >> str;
  10.     cin.ignore();
  11.  
  12.     int i;
  13.     for(i=0; i<(str.length()); i++) {
  14.         a[i] = *(str.c_str()+i);
  15.     }
  16.  
  17.     /* char * p = &(a[7])+i;
  18.     cout << *p << endl; */
  19. }
Thanks for help...
Aug 14 '11 #1
5 2236
johny10151981
1,059 Top Contributor
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
Aug 14 '11 #2
Ramsin
15 New Member
Expand|Select|Wrap|Line Numbers
  1. (gdb) run
  2. Starting program: /home/ramsin/Desktop/a.out 
  3. AAAABBBBCCCCDDDD
  4.  
  5. Program exited normally.
  6. (gdb) r
  7. Starting program: /home/ramsin/Desktop/a.out 
  8. AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD 
  9.  
  10. Program exited normally.
  11. (gdb) r
  12. Starting program: /home/ramsin/Desktop/a.out 
  13. AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDF
  14.  
  15. Program received signal SIGSEGV, Segmentation fault.
  16. 0x00272c49 in __libc_start_main () from /lib/libc.so.6
  17.  
Aug 14 '11 #3
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
Aug 15 '11 #4
Ramsin
15 New Member
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.
Aug 15 '11 #5
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
Aug 15 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2606
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...
5
7474
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()...
0
1097
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...
4
4681
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...
0
2394
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...
5
4574
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...
7
22016
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...
49
2659
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; ...
30
2654
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 {...
0
1398
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 : ---------------------------------------------------------...
0
7324
Oralloy
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,...
0
7382
jinu1996
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...
0
7495
tracyyun
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...
0
5627
agi2029
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,...
1
5052
isladogs
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...
0
4707
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...
0
1556
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 ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
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...

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.