Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

memset

Posted on 2011-02-11
14
Medium Priority
?
293 Views
Last Modified: 2012-05-11
Is this valid? or am I crossing the boundary of memory here causing memory corruption?
const char* names[256];
   std::memset(names, 0, 256);

for (int i = 0; i < 100; i++) {
   names[i] = "TESTING";
}

Open in new window

0
Comment
Question by:learningunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
14 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 34871926
>> const char* names[256];
>>    std::memset(names, 0, 256);

A char* is probably bigger than 1 byte, so this isn't going to write past the bounds of the array (it'll write way less than the total array in fact).

Related though : this is NOT the good way to initialize an array to all NULL pointers. Rather do :

        const char* names[256] = { 0 };
0
 

Author Comment

by:learningunix
ID: 34872016
so if I do names[0] = "some string"  
if "some string" is greater than 256 in length, is that valid?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 34872065
>> if "some string" is greater than 256 in length, is that valid?

Yes.

The 256 is the AMOUNT of strings that can be stored in the array. It says nothing about the SIZE of each individual string.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Assisted Solution

by:pasolo
pasolo earned 500 total points
ID: 34872290
You declared an array of pointers to char, you have no memory allocated to anything else and memory will likely corrupt within the loop. This is not an array of strings, you have not declared any string.:
Try something like this:

vector <string> myvect;
myvect.reserve(256);
for (int i=0; i<256;i++)
myvect.push_back("TESTING");

You have an array of 256 strings now.
0
 

Author Comment

by:learningunix
ID: 34872325
That's why I asked initially if I am corrupting memory or not.

Do I need to malloc just before
names[] = "TESTING";
0
 

Author Comment

by:learningunix
ID: 34872506
Or I can also do this
std::string names[256];

for (int i = 0; i < 100; i++) {
   names[i].assign("TESTING");
}

This way I don't have to worry about allocating and delallocating of memory at all

Open in new window

0
 
LVL 3

Expert Comment

by:pasolo
ID: 34872626
You can, or, assuming no "string" is going to be larger than say 10 chars do something like this (using part of your code, but not using array of pointers, just allocate the space on the stack):

char names[256][10];
   std::memset(&names, 0, sizeof(names));

for (int i = 0; i < 100; i++) {
   strcpy((char*)&names[ i ],"TESTING");
   }

You end with 256 "TESTING" strings here
0
 
LVL 3

Assisted Solution

by:pasolo
pasolo earned 500 total points
ID: 34872650
And since you are using std, the vector approach i mentioned earlier is always preferable and cleaner
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34872959
>>  That's why I asked initially if I am corrupting memory or not.

As I said before : the code you showed is not corrupting memory.

>> Do I need to malloc just before

Unless you want to copy the string literal, you don't need to allocate memory.

A string literal (like "TESTING") has its own storage, you can just let a const char* point to it without issues :

        const char* str = "TESTING";       /* <--- no need to allocate memory */
0
 

Author Comment

by:learningunix
ID: 34873072
I think so I am planning to go with string class.
This way.

1. I don't have to worry about freeing the memory in the end. Once the class is destroyed the static memory assoicated string is gone.
2. also I don't have to worry about the value of length assigned to each strings.

std::string names[256];
int position = 0;
void insert (const char *inName)
{
   names[position++].assign(inName);
   // I'll add a check to see position is > 256 or not
}

Open in new window

0
 

Author Comment

by:learningunix
ID: 34873090
Just one thing I want to clarify.
if std::string names[256]; is member of class A.
When I do delete A, this will free up all names also ?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 500 total points
ID: 34873139
>> 1. I don't have to worry about freeing the memory in the end. Once the class is destroyed the static memory assoicated string is gone.

String literals don't need to be freed. They will be in memory for the duration of your application. You don't need to manage them. You can just point to them and use them.


>> 2. also I don't have to worry about the value of length assigned to each strings.

Don't know what you mean by that. But the length of a string literal can be optained with strlen, just like any other string.


>> if std::string names[256]; is member of class A.
>> When I do delete A, this will free up all names also ?

Yes.


Note that you could use std::string instead if you wish, but it would no longer behave in the same way as your original code. You'd be working with a COPY of the string, rather than with a string literal. Then again, maybe that's what you wanted all along :)
0
 
LVL 3

Expert Comment

by:pasolo
ID: 34873217
>if std::string names[256]; is member of class A

The destructor will be invoked and each name will be destroyed.
PS: But try the vector because it allocates more space if needed.
0
 

Author Closing Comment

by:learningunix
ID: 34918098
Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question