473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1822
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**********@hotmail.com> wrote in message news:f9**************************@posting.google.c om...
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
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 = ... #...
11
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...
6
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...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
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...
44
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...
18
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) ...
41
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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
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...

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.