473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

A possible memory overwriting.

Recently I came across a code which is quite similar to one shown
below:

#include <stdio.h>
#include <string.h>

typedef struct node
{
int data;
int arr[10];
struct node *next;
} node;

int main(void)
{
node block;
memset( &block, 0 , sizeof(block) );
printf("Main exiting...\n");
return 0;
}

In the above code, the memset function will make the "sizeof(block)
elements" in memory being pointed by &block equal to zero. But, isn't
the memset function overwriting the memory being pointed by &block
since the memory beyond the one being pointed by &block might be
belonging to something else?
Please help me to pin-point my mistake.

Nov 15 '05 #1
9 2304
ja**********@yahoo.com wrote:
typedef struct node
{
int data;
int arr[10];
struct node *next;
} node;
<snip>
node block;
memset( &block, 0 , sizeof(block) );
<snip>
In the above code, the memset function will make the "sizeof(block)
elements" in memory being pointed by &block equal to zero.
Yes.
But, isn't
the memset function overwriting the memory being pointed by &block
Yes.
since the memory beyond the one being pointed by &block might be
belonging to something else?
The memory &block points to is block, which of course belongs to you.
The only problem with the memset() call is that it probably expects to
set next to NULL - it probably will work, but all-bits-0 is not
guaranteed to be a null pointer.
Please help me to pin-point my mistake.


You didn't make one in the code you posted.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #2
ja**********@yahoo.com wrote:

# In the above code, the memset function will make the "sizeof(block)
# elements" in memory being pointed by &block equal to zero. But, isn't

sizeof block bytes, not elements.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So basically, you just trace.
Nov 15 '05 #3

SM Ryan wrote:
ja**********@yahoo.com wrote:

# In the above code, the memset function will make the "sizeof(block)
# elements" in memory being pointed by &block equal to zero. But, isn't

sizeof block bytes, not elements.


But the code for memset() indicates that it is sizeof(block)
"elements" and not "bytes" and in that case the program should crash
beacuse of the overwriting of the return address.
void *memset(void *s, register int c, register size_t n)
{
register char *s1 = *(char*)s;

while( n>0 )
{
*s1++ = c;
--n;
}

return s1;
}

Nov 15 '05 #4
ja**********@yahoo.com wrote:
SM Ryan wrote:
ja**********@yahoo.com wrote:

# In the above code, the memset function will make the "sizeof(block)
# elements" in memory being pointed by &block equal to zero. But, isn't

sizeof block bytes, not elements.
But the code for memset()


What code for memset()? There is no _the_ code for memset(). There is only
_the_ specification for memset(), and it states this:

# The memset function copies the value of c (converted to an unsigned char)
# into each of the first n characters of the object pointed to by s.
^^^^^^^^^^

Note: characters, so bytes. Not elements. It gets a void pointer, anyway, so
how is it to know in the first place how large an "element" is for the
original pointer?
void *memset(void *s, register int c, register size_t n)
{
register char *s1 = *(char*)s;

while( n>0 )
{
*s1++ = c;
--n;
}

return s1;
}


This is _a_ possible implementation for memset(), and it copies into the
first n characters (ergo bytes), not "elements", whatever they are.
Note, btw, that it converts c into a char, not explicitly into an unsigned
char. This is only valid for some (although many) implementations, not for
all.

Richard
Nov 15 '05 #5
Richard Bos wrote:

ja**********@yahoo.com wrote:
void *memset(void *s, register int c, register size_t n)
{
register char *s1 = *(char*)s;

This is _a_ possible implementation for memset(),


No, it isn't.
1 The pointer s1 is assigned a char value
instead of a pointer value.
2 The type of s1 should be (unsigned char *).

--
pete
Nov 15 '05 #6
pete <pf*****@mindspring.com> wrote:
Richard Bos wrote:

ja**********@yahoo.com wrote:

void *memset(void *s, register int c, register size_t n)
{
register char *s1 = *(char*)s;

This is _a_ possible implementation for memset(),


No, it isn't.
1 The pointer s1 is assigned a char value
instead of a pointer value.
2 The type of s1 should be (unsigned char *).


*Sigh* Yes. As I noted in the next sentence. So this is _a_ possible way
to code memset() for many, but not all, C implementations. As I noted.

Richard
Nov 15 '05 #7
Richard Bos wrote:

pete <pf*****@mindspring.com> wrote:
Richard Bos wrote:

ja**********@yahoo.com wrote:
> void *memset(void *s, register int c, register size_t n)
> {
> register char *s1 = *(char*)s;

This is _a_ possible implementation for memset(),


No, it isn't.
1 The pointer s1 is assigned a char value
instead of a pointer value.


*Sigh* Yes. As I noted in the next sentence.
So this is _a_ possible way
to code memset() for many, but not all, C implementations. As I noted.


No!!!
*(char*)s
should be
s

Those are two completely different initializations.
In order for the first one to work, the first byte
of the target object would have to contain a char value
that converts to a pointer to itself,
which besides being implementation defined,
is super extremely unlikely.
memset does not depend upon the value of the first byte
of the taget object, which is what (*(char*)s) is.

--
pete
Nov 15 '05 #8
ja**********@yahoo.com wrote:
#
# SM Ryan wrote:
# > ja**********@yahoo.com wrote:
# >
# > # In the above code, the memset function will make the "sizeof(block)
# > # elements" in memory being pointed by &block equal to zero. But, isn't
# >
# > sizeof block bytes, not elements.
#
# But the code for memset() indicates that it is sizeof(block)
# "elements" and not "bytes" and in that case the program should crash
# beacuse of the overwriting of the return address.

You're casting to a character pointer, so its elements are
bytes. But in something like

int x[2]; memset(x,0,sizeof x);

Assuming 4-byte ints, sizeof x is 8, and memset clears 8 bytes,
not 8 ints.

# void *memset(void *s, register int c, register size_t n)
# {
# register char *s1 = *(char*)s;
#
# while( n>0 )
# {
# *s1++ = c;
# --n;
# }
#
# return s1;
# }
#
#
#

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So basically, you just trace.
Nov 15 '05 #9
SM Ryan wrote:

ja**********@yahoo.com wrote:
#
# SM Ryan wrote:
# > ja**********@yahoo.com wrote:
# >
# > # In the above code, the memset function will make the
# > # "sizeof(block)
# > # elements" in memory being pointed
# > # by &block equal to zero. But, isn't
# >
# > sizeof block bytes, not elements.
#
# But the code for memset() indicates that it is sizeof(block)
# "elements" and not "bytes" and in that case the program should crash
# beacuse of the overwriting of the return address.

You're casting to a character pointer, so its elements are
bytes. But in something like
It's much more fucked up than that.
The value, >>>not the address<<<,
of the first byte of the target is being assigned to s1.
Even if it were to return s instead of s1, it still wouldn't work.
int x[2]; memset(x,0,sizeof x);

Assuming 4-byte ints, sizeof x is 8, and memset clears 8 bytes,
not 8 ints.

# void *memset(void *s, register int c, register size_t n)
# {
# register char *s1 = *(char*)s;
#
# while( n>0 )
# {
# *s1++ = c;
# --n;
# }
#
# return s1;
# }


void *mem_set(void *s, int c, size_t n)
{
unsigned char *p = s;

while (n-- != 0) {
*p++ = (unsigned char)c;
}
return s;
}

--
pete
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: hall | last post by:
Hi. I have some problems with a class i've written that allocates memory dynamicaly. I want to put these objects into a std::vector, but it does not work. My class looks (simplified) like this: ...
10
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
1
by: Tony Selke | last post by:
Forgive my rather wide posting of this note, but I was uncertain of where I would find the best help. I am working on a class that is basically providing a secure location for name/value pairs...
19
by: tweak | last post by:
I have been messing around with buffers, and I found it peculiar that the code below will run without a segmentation fault. As far as I know, overwriting the allocated space from a call to...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
18
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but...
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
23
by: sam_cit | last post by:
Hi Everyone, I have the following program unit, #include <stdlib.h> int main() { char *p = (char*)malloc(100); if(p==NULL)
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.