473,699 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

segmentation fault when comparing string with NULL in strlen

1 New Member
Hi,

i am getting segmentation fault with the below code.

size_t strlen(const char *str) {
const char *str1 = str;
if (str == NULL)
return(0);

while (*str)
str++;

return(str - str1);
}

I am getting segmentation fault in the line if(str == NULL). str has the value "P\302n+K\1 77"
Please suggest.
Aug 8 '16 #1
2 2121
weaknessforcats
9,208 Recognized Expert Moderator Expert
Post the code that calls this function.

This function assumes a non-zero str contains a valid address.

I want to see the call to verify that str is not screwed up before the call.
Aug 8 '16 #2
donbock
2,426 Recognized Expert Top Contributor
Depending on the details of your debugger, it might be difficult to distinguish if the exception occurs for the str1=str assignment or the if statement on the next line. Suggest you separate them a little more:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. const char *str1;
  3. if(str==NULL)
  4.     return(0);
  5. str1 = str;
  6. ...
Gggggg
Aug 9 '16 #3

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

Similar topics

14
11677
by: deancoo | last post by:
Hello all, I have a class member, which calls another member from the same class. The problem I'm experiencing is that the nested function executes fine, but causes a segmentation fault as it exits. I've traced it to the point in the code just before the line following the nested function is to be called, or conversely right after the last line of the nested function. What might be my problem? I'm hoping someone can give me some...
5
9330
by: sandwich_eater | last post by:
I get a segmentation fault in my program when resizing a vector. This seems strange because it is the second resize that causes the error. std::vector<double> a; std::vector<double> b; a.resize(ndata - 1); b.resize(ndata - 1); // fault occurs here
10
2383
by: Cocy | last post by:
Hi, This might be a sort of FAQ, but I don't see why, so I would someone help me to understand what's wrong? I've just created following code which wold trim white space(s) in a (given) string. But, it resulted the Segmentation fault, and so as when running in gdb (saying "Program received signal SIGSEGV, Segmentaion fault at *p++ = *st++"). The platform is Linux kernel 2.4.27, gcc version
2
1239
by: francescomoi | last post by:
Hi. I've got a C program which calls a function: ---------------- printf("TEST1\n"); int value = my_function(param); printf("TEST2\n"); int my_function(int my_param) {
12
2301
by: nae zot bba la | last post by:
Hi, very short code here. Basically I want to open a file called instruction in a++ mode and then write data of %s type to it, then read from the same file and print to screen. The code compiles but when I execute the binary and enter some string to expect the program to print them for me to screen, segmentation fault occurs. Any ideas anyone? #include <stdio.h> #include <stdlib.h> char *add;
5
6745
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size() const { return _bdl.size(); }
6
2473
by: Wes | last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++. The idea of the program is to open one file as input, read bytes from it, do some bitwise operations on the bytes, and then write them to this second file. However, when the second file is 15360 bytes long, the program dies with a "Segmentation Fault (core dumped)" error! I checked with gdb, and it says the last function to run was memcpy() from libc, which would...
3
4668
by: krista | last post by:
Hi, I am beginner of C++. When I try to read a file and put it into X and Y variables. I got that segmentation fault. the data file is data.txt: 4.4 6.8 3.2 -5.5 3.3 0.9
1
2526
by: Brendan Wing | last post by:
Howdy, I was trying pass a file pointer from main to another function, so i can use the same file stream to perform certain task instead of closing stream on main then open a new one on the called function. the code basically looks like this: int func(FILE* pfile) { //whatever task i need to perform }
2
2365
by: Charlie Starks | last post by:
Please review my pastebin. I commented exactly where it occurs, and how GDB will not access my variable. I made a program to prove that you can store an address into an unsigned int and use it as a pointer. It works on old 32bit machine, but not on my 64 bit. I can only assume it is because of my compiler?? //I am trying to figure out why I get a segmentation fault at "*((char *) hacky_nonpointer)" // //This works on a 32 bit older gcc...
0
8630
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,...
0
9055
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8944
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
8899
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...
0
5889
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
4391
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...
0
4638
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3075
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
2
2364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.