LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2009, 01:26 AM   #1
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
problem with memset and char pointer


Hi,

hi teckkies, a very cool and head ditching problem of char pointer
OS:ubuntu
language : c++

Code:
#include <iostream>
using namespace std;
#include <string.h>
#define BUFF 12
int main()
{

	char *str;
        cout<<"enter name"<<endl;
	cin>> str;
	cout<<str<<endl;
	
return 0;
}
Running steps:
g++ file.cpp

./a.out
enter name
naveen(i entered my name)
segmentation fault


1)why this segmentation fault here?
2)can i use memset for char* as follows?

Code:
memset(str, 0, sizeof(str));
where str is declared as char* str;

and tell me which one is better?
memset(str, 0, sizeof(str));
memset(str, 0, 12);

if we declare str as char* str, Is that necessary to do memset?


please make me clear in this memset and char*
 
Old 09-23-2009, 01:30 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
You never defined a storage location for your string. You defined a place to put the address of that storage location, but you never defined the location itself.

You defined a BUFF but never used it. You need to malloc some memory. str=malloc(BUF); should do it.
 
Old 09-23-2009, 01:42 AM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
you tried to store your string in nothing
instead of char *str use char str[14]
 
Old 09-23-2009, 04:37 AM   #4
blues1207
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Rep: Reputation: 0
allocate memory
 
Old 09-23-2009, 06:58 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by naveenisback View Post
char *str;
My best guess at what you're trying to accomplish with that would be correctly done by
char *str = new char[BUFF+1];

Quote:
cin>> str;
Quote:
1)why this segmentation fault here?
Because str was an uninitialized pointer.

Quote:
and tell me which one is better?
memset(str, 0, sizeof(str));
memset(str, 0, 12);
The first is wrong because sizeof(str) will always be the size of the pointer, not the size of the buffer pointed to.
The second would be right if str pointed to a big enough buffer.

Quote:
if we declare str as char* str, Is that necessary to do memset?
That depends on the method used to allocate the buffer and I don't recall whether new char[] gives you a buffer that is reliably zero filled when allocated.

Quote:
Originally Posted by jiml8 View Post
You need to malloc some memory. str=malloc(BUF); should do it.
In C++ it is generally better to consistently use new, rather than use malloc in places like this where a corresponding C program would have used malloc.

It is also better to use std::string for text input, rather than matching the crude C approach of hard coding a guess at the required buffer size. So all the discussion here about how to use char* correctly is just because that is what the OP seems to have asked. It should not leave the impression that using char* is a good approach. Again I'm still just guessing at what the OP really wants to do, but I'm pretty sure it would be better done with std::string rather than with char*.

Last edited by johnsfine; 09-23-2009 at 07:04 AM.
 
Old 09-23-2009, 01:10 PM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
It is also better to use std::string for text input, rather than matching the crude C approach of hard coding a guess at the required buffer size. So all the discussion here about how to use char* correctly is just because that is what the OP seems to have asked. It should not leave the impression that using char* is a good approach. Again I'm still just guessing at what the OP really wants to do, but I'm pretty sure it would be better done with std::string rather than with char*.
i use cin>> and cout<< all the time with char * with borland turbo C++ and g++
i see no reason to make it more complex to do C operations from withen C++
 
Old 09-23-2009, 02:57 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by smeezekitty View Post
i use cin>> and cout<< all the time with char * with borland turbo C++ and g++
That probably means you were competent in C before you learned C++ (or maybe you had a really bad instructor for C++ that didn't know how to let go of C). I was competent in C long before C++ was invented, so I understand the viewpoint even if I don't agree with it.

Quote:
i see no reason to make it more complex to do C operations from withen C++
Beginners in C usually make mistakes with char* exactly like the OP in this thread made. Any language feature responsible for such a high number of beginner mistakes is a hint that something should be taught differently.

At some point, a C++ programmer (like a C programmer), needs to develop a good understanding of pointers. But I think it is better for a beginner to build up to that understanding though examples where the meaning and value of using a pointer is more obvious.

I think text is something a beginner should be able to work with even before they develop that good understanding of pointers.

Using char* when the conceptual object you are thinking about is a text string, not a character pointer, is a flaw in C and an unnecessary distraction in C++.

The object the programmer want to input is a text string, not a character pointer. Then I expect the object he wants to manipulate is a text string, not a character pointer. A C programmer must learn how to use char* when he intends string. A C++ programmer doesn't need to.

Quote:
Originally Posted by smeezekitty View Post
instead of char *str use char str[14]
or char str[BUFF];

That is a valid alternative for the trivial situation of the original example. I realized later that I may have been confusing matters when I ignored that post and said:

Quote:
Originally Posted by johnsfine View Post
The first is wrong because sizeof(str) will always be the size of the pointer, not the size of the buffer pointed to.
In fact, when str isn't a pointer, sizeof(str) isn't "always" the size of a pointer. So one of the advantages of declaring str the way smeezekitty suggested is that sizeof(str) will have the meaning a beginner expects.

Last edited by johnsfine; 09-23-2009 at 03:29 PM.
 
Old 09-23-2009, 04:33 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by smeezekitty View Post
i use cin>> and cout<< all the time with char * with borland turbo C++ and g++
i see no reason to make it more complex to do C operations from withen C++
i think i made myself unclear
i mean i use char * after i allocate it such as
[code]
char *p = new char[10];
cout<<"Enter your name:";
cin>>p;
cout<<"Hello " <<p;
delete p;
[/cpde]
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
warning: passing argument 2 of ‘memset’ makes integer from pointer without a cast nasim751 Programming 3 01-30-2008 09:16 AM
C : Function to modify pointer to char introuble Programming 2 06-21-2006 12:24 PM
c char* pointer function question true_atlantis Programming 9 04-14-2006 01:29 PM
memset vs char arr initialization syseeker Programming 4 03-29-2006 09:01 AM
is *- -p = '\n' valid? can i set \n char to a pointer? feetyouwell Programming 1 10-01-2004 01:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration