Solved

memset

Posted on 2011-02-11
14
290 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
Industry Leaders: 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 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