473,795 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointers to pointers // exception handling error

I'm writing a function that isn't showing as an error during
compilation, but it is at run time. The exception it is throwing
informs me that it is due to my two pointers in that function. It is
an access violation error, where the memory can't be read. I'll list
the things I've tried in order to resolve the problem so that no one
will suggest the same thing. I've tried initialising AN[4] as a just a
character pointer, then just as a character variable, neither works as
the rest of the function requires it to be both a pointer array
variable. I've tried referencing BAN =& AN[4], which neither worked
and rightly so as the function checks to see whether AN[i] is isalpha
or a space, if encountered the next element in the array requires
checking, and the one preceeding it. BAN is used to get either the
preceeding element or the one following it.

Thank you in advance for your help in this matter.

bool CheckAddressN( char* record )
{

char* AN[4];
char* BAN;

strncpy(AN[4], &record[27], 4);
AN[4] = '\0';
for(int i=0; i < 4; i++)

if(AN[i] = " ") // check that last character is a number and the
next character
{
//is a char.
BAN = (AN[i] + 1 ) ;
if(!isalpha(BAN[i])){}

}
return false;

if(AN[i] = " ") // check that last character is a number and the
next character
{
//is a char.
BAN = (AN[i] - 1);
if(!isdigit(BAN[i])){};

}
return false;
if(AN[i] = ";")
{

BAN = (AN[i] - 1);

if(!isdigit(BAN[i])){};
}
return false;

if(AN[i] = ";")
{
BAN = (AN[i] + 1);

if(!isalpha(BAN[i])){};

}
return false;

if(AN[i] = ";")
{
BAN = (AN[i] + 1);

for(int i=0; i < 17; i++)
if(!isalpha(BAN[i]))
return false;
}

return true;

}
Jul 19 '05 #1
3 1844
muser wrote:
I'm writing a function that isn't showing as an error during
compilation, but it is at run time. The exception it is throwing
informs me that it is due to my two pointers in that function. It is
an access violation error, where the memory can't be read. I'll list
the things I've tried in order to resolve the problem so that no one
will suggest the same thing. I've tried initialising AN[4] as a just a
character pointer, then just as a character variable, neither works as
the rest of the function requires it to be both a pointer array
variable. I've tried referencing BAN =& AN[4], which neither worked
and rightly so as the function checks to see whether AN[i] is isalpha
or a space, if encountered the next element in the array requires
checking, and the one preceeding it. BAN is used to get either the
preceeding element or the one following it.

Thank you in advance for your help in this matter.

bool CheckAddressN( char* record )
{

char* AN[4];
All right, you have an array of four pointers to char, each of which
is of indeterminate value.
char* BAN;
Now you have another pointer to char, also of indeterminate value.

strncpy(AN[4], &record[27], 4);
Now you're trying call `strncpy()' using as a destination a value
that doesn't even exist! (in `char* AN[4]' valid indices range from
0 to 3).

You've invoked undefined behavior -- so *anything* can happen.
AN[4] = '\0';
for(int i=0; i < 4; i++)

if(AN[i] = " ") // check that last character is a number and the
next character

Erm, no. You're *assigning* the address of the string literal
'" "' to AN[i]. All well and good (unlike the above), but
certainly not what you intended to do. Besides, this test *cannot fail*.
{
//is a char.
BAN = (AN[i] + 1 ) ;
if(!isalpha(BAN[i])){}

}
return false;

if(AN[i] = " ") // check that last character is a number and the
next character
See above. You're doing the same thing.
{
//is a char.
BAN = (AN[i] - 1);
if(!isdigit(BAN[i])){};

}
return false;
if(AN[i] = ";")
{

BAN = (AN[i] - 1);

if(!isdigit(BAN[i])){};
}
return false;

if(AN[i] = ";")
{
BAN = (AN[i] + 1);

if(!isalpha(BAN[i])){};

}
return false;

if(AN[i] = ";")
{
BAN = (AN[i] + 1);

for(int i=0; i < 17; i++)
if(!isalpha(BAN[i]))
return false;
}

return true;

}


I'd be surprised if there aren't even more errors here.
Rethink what you're doing, get a good C book (see
http://www.accu.org for suggestions) and post back if you run into
problems.

Unfortunately, you've got a ways to go.

HTH,
--ag

--
Artie Gold -- Austin, Texas

Jul 19 '05 #2
muser wrote:
I'm writing a function that isn't showing as an error during
compilation, but it is at run time. The exception it is throwing
informs me that it is due to my two pointers in that function. It is
an access violation error, where the memory can't be read. I'll list
the things I've tried in order to resolve the problem so that no one
will suggest the same thing. I've tried initialising AN[4] as a just a
character pointer, then just as a character variable, neither works as
the rest of the function requires it to be both a pointer array
variable. I've tried referencing BAN =& AN[4], which neither worked
and rightly so as the function checks to see whether AN[i] is isalpha
or a space, if encountered the next element in the array requires
checking, and the one preceeding it. BAN is used to get either the
preceeding element or the one following it.

Thank you in advance for your help in this matter.

bool CheckAddressN( char* record )
{

char* AN[4];
char* BAN;

strncpy(AN[4], &record[27], 4);
Hugh ?

You're copying to an unallocated, uninitialized pointer.
AN[4] = '\0';


This would write to byte 5 ! The size is 4.

I'll stop reading the rest.
What are you trying to do ?

Jul 19 '05 #3

"muser" <ch**********@h otmail.com> wrote in message news:f9******** *************** ***@posting.goo gle.com...
I'm writing a function that isn't showing as an error during
compilation,
Undefined behavior is rarely caught at compile time.
char* AN[4];
char* BAN;

strncpy(AN[4], &record[27], 4);
AN[4] = '\0';
Yoiu have two major problems here. First, AN[4] is one past the end of the
array. It's undefine behavior to access it.

Second, AN is an array of four uninitialized pointers. You don't want pointers
here it all it looks like. What you want is an array of 4 (well 5 actually) characters.

char AN[5];
strncpy(AN, &record[27], 4);
AN[4] = '\0';

if(AN[i] = " ") // check that last character is a number and the
Again you have more problems. = is assignment NOT a test
for equality. A single character is surrounded by apostrophes
not quotes. The only reason this compiels at all is because of
the misdeclaration of AN above.
BAN = (AN[i] + 1 ) ;
I can't even begin to understand what this is supposed to be.
I suspect you wanted BAN to be just a char.

BAN = AN[i] + 1 ; // Add a logical one to the character value at AN[i].>
or
BAN = AN[i+1]; // character value at 1 past the index?
if(!isalpha(BAN[i])){}
What on earth is the index for here?
if(!isalpha(BAN ))


I'm giving up. You need to learn the difference between:

A character.
A pointer.
A string.

Frankly, you should abandon the current scheme and switch to useing
std::string. and it's member accessing and substr methods.
Jul 19 '05 #4

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

Similar topics

13
1547
by: Aggelos I. Orfanakos | last post by:
Hello. In a program, I want to ensure that a socket closes (so I use try ... finally), but I also want to catch/handle a socket exception. This is what I have done: try: try: s = ... # socket opens
11
2888
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses return code (not generating error/exception) so it is more compatible with other programming language.
6
2342
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which assemblies are loaded before shutting down. In one case, some of my DB-accessing code didn't handle a NULL value properly. But try...catch wouldn't catch the exception and keep going. I'd just get the error message and then it would shut the...
7
6007
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2752
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you please critique the following exception handling mechanism in terms of my requirements ?
44
4230
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
18
2981
by: Denis Petronenko | last post by:
Hello, in the following code i have segmentaion fault instead of exception. Why? What i must to do to catch exceptions in such situation? Used compiler: gcc version 3.3.6 (Debian 1:3.3.6-13) int main() { try{ int* p = NULL; *p = 4;
41
3081
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must handle all of them. I don't care about which one happened, except to write out exception.Message to a log file. It seems verbose to write out three handlers that all do the same thing. So, I could just catch Exception. But, is that...
2
2203
by: Carol | last post by:
Exception may be thrown in the code inside the try block. I want to handling the SqlException with State == 1 in a special way, and for all others I want to use a general way to handle. Which of the following options is better? ----------------------------------------------------- Option 1: try{...} catch (System.Data.SqlClient.SqlException ex) { if (ex.State == 1)
0
9673
marktang
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...
0
10448
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10167
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,...
0
10003
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 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...
1
7544
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 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...
0
6784
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
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...
1
4114
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
3
2922
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.