jenglee
jenglee

Reputation: 353

C memory issue with char*

I need help with my C code. I have a function that sets a value to the spot in memory to the value that you have input to the function.

The issue that I am facing is if the pointer moves past the allocated amount of memory It should throw an error. I am not sure how to check for this issue.

 unsignded char the_pool = malloc(1000);
 char *num = a pointer to the start of the_pool up to ten spots
 num[i] =  val;
num[11] = val; //This should throw an error in my function which 

So how can I check to see that I have moved into unauthorized memory space.

Upvotes: 0

Views: 166

Answers (6)

m1o2
m1o2

Reputation: 1629

if you are using the Visual Studio 2010 ( or 2011 Beta ) it will till you after u try to free the allocated memory.

there is advanced tools to check for leaked memory.

in you example, you have actually moved to unauthorized memory space indeed. your indexes should be between 0 to ( including ) 999.

Upvotes: -1

BQiao
BQiao

Reputation: 14

I guess you could use sizeof to avoid your array access out of bound index. But c allows you to access some memory out of your array bound. That's OK for c compiler, and OS will manage the behavior when you do that. C/C++ doesn't actually do any boundary checking with regards to arrays. It depends on the OS to ensure that you are accessing valid memory. You could use array like this: type name[size];

Upvotes: 0

Crashworks
Crashworks

Reputation: 41404

C will not catch this error for you. You must do it yourself.

For example, you could safely wrap access to your array in a function:

typedef struct
{
   char *data; 
   int length;
} myArrayType;

void MakeArray( myArrayType *p, int length )
{
   p->data = (char *)malloc(length);
   p->length = length;
}

int WriteToArrayWithBoundsChecking( myArrayType *p, int index, char value ) 
{
   if ( index >= 0 && index < p->length ) 
   { 
      p->data[index] = value;
      return 1; // return "success"
   }
   else 
   {
      return 0; // return "failure"
   }
}

Then you can look at the return value of WriteToArrayWithBoundsChecking() to see if your write succeeded or not.

Of course you must remember to clean up the memory pointed at by myArrayType->data when you are done. Otherwise you will cause a leak.

Upvotes: 3

sarnold
sarnold

Reputation: 104070

Common C compilers will not perform array bounds checking for you.

Some compilers are available that claim to support array bounds -- but their performance is usually poor enough compared to the normal compilers that they are usually not distributed far and wide.

There are even dialects of C intended to provide memory safety, but again, these usually do not get very far. (The linked Cyclone, for example, only supports 32 bit platforms, last time I looked into it.)

You may build your own datastructures to provide bounds checking if you wish. If you maintain a structure that includes a pointer to the start of your data, a data member that includes the allocated size, and functions that work on the structure, you can implement all this. But the onus is entirely on you or your environment to provide these datastructures.

Upvotes: 0

Guido
Guido

Reputation: 2634

dont you mean?

num[11] = val

Yes there is no way to check that it is beyond bounds except doing it yourself, C provides no way to do this. Also note that arrays start at zero so num[10] is also beyond bounds.

Upvotes: 3

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

The standard defines this as Undefined behavior.

It might work, it might not, you never know, when coding in C/C++, make sure you check for bounds before accessing your arrays

Upvotes: 1

Related Questions