Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


problems with memset


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 26 March 2013 - 01:19 PM

Here's the code:

 

class gg{
public:
	char ce[10];
	gg(char j = ' ') { memset(ce,j,sizeof(ce));};
};
int main() {
	gg x('m');
	cout << x.ce << endl;

    
}

 

 

It outputs all those 10 ms,and then a series of weird symbols appear,why does that happen?



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8454

Like
4Likes
Like

Posted 26 March 2013 - 01:22 PM

Your string is not null terminated and so the stream is printing whatever follows the last element until the first null character. Add a '\0' at the end to mark the end of the string.



#3 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 26 March 2013 - 01:26 PM

Thank a lot



#4 Norman Barrows   Crossbones+   -  Reputation: 2205

Like
0Likes
Like

Posted 26 March 2013 - 01:30 PM

perhaps the easiest thing to do is:

 

char ce[10];       // declare string

ce[0]=0;             // set string to ""


Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 


#5 Paradigm Shifter   Crossbones+   -  Reputation: 5412

Like
4Likes
Like

Posted 26 March 2013 - 01:40 PM

Uh, no. He's about to overwrite the 10 chars in ce with 10 'm' characters so making it the empty string before you do that isn't very useful.

 

You need to do this

 

memset(ce, j, sizeof(ce) - 1);

ce[sizeof(ce)-1] = '\0';

 

You'll want to up the array size to 11 if you want to store 10 'm' characters in the array though as well as the null terminator.

 

Note: this only works since sizeof char is defined as 1.


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#6 swiftcoder   Senior Moderators   -  Reputation: 10242

Like
1Likes
Like

Posted 26 March 2013 - 05:24 PM

Note: this only works since sizeof char is defined as 1.

You can solve this by using sizeof(char):

memset(ce, j, sizeof(ce) - sizeof(char));

 

However, unless I'm very mistaken, sizeof(char) is guaranteed to be 1.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#7 Bacterius   Crossbones+   -  Reputation: 9105

Like
0Likes
Like

Posted 26 March 2013 - 05:28 PM

Note: this only works since sizeof char is defined as 1.

You can solve this by using sizeof(char):

 

memset(ce, j, sizeof(ce) - sizeof(char));

 

However, unless I'm very mistaken, sizeof(char) is guaranteed to be 1.

 

Only if the char array is previously initialized to zero, which I believe is not the case for local variables (their value, and, for pointers, the memory they point to, is undefined).

 

EDIT: never mind that. And, yes, sizeof(char) is defined to be 1 by the C and C++ standard. Don't have the references with me now, though. Of course it's best to get in the habit of using vectors (and strings) if using C++, there's no overhead and it's just simpler and safer to use, not to mention more idiomatic.


Edited by Bacterius, 26 March 2013 - 05:30 PM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 Paradigm Shifter   Crossbones+   -  Reputation: 5412

Like
0Likes
Like

Posted 26 March 2013 - 05:29 PM

Yeah I know, that's why I said it was DEFINED as 1 ;)


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS