Solved

memset

Posted on 2011-02-11
14
291 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 125 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:pasolo
pasolo earned 125 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 125 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 125 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

724 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