memset

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

learningunixAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
>> 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
 
Infinity08Commented:
>> 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
 
learningunixAuthor Commented:
so if I do names[0] = "some string"  
if "some string" is greater than 256 in length, is that valid?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
pasoloConnect With a Mentor Commented:
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
 
learningunixAuthor Commented:
That's why I asked initially if I am corrupting memory or not.

Do I need to malloc just before
names[] = "TESTING";
0
 
learningunixAuthor Commented:
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
 
pasoloCommented:
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
 
pasoloConnect With a Mentor Commented:
And since you are using std, the vector approach i mentioned earlier is always preferable and cleaner
0
 
Infinity08Commented:
>>  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
 
learningunixAuthor Commented:
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
 
learningunixAuthor Commented:
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
 
Infinity08Connect With a Mentor Commented:
>> 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
 
pasoloCommented:
>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
 
learningunixAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.