By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

char In structures

P: n/a
Hi,
I'm getting some weird results for the following code:

#include "stdafx.h"
#include <stdlib.h>
typedef struct ALLO
{
char a[3];
char b[4];
} Allo;

int _tmain(int argc, _TCHAR* argv[])
{
Allo al;
strcpy(al.a, "aloe");
strcpy(al.b, "vcxzv");
return 0;
}

I get an error "stack around arround the 'al' variable was corrupted". I
think that strcpy in this case isn't good but I can't realy tell... If it is
how could I set my char in my structure else than with strcpy?

TIA, Max.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <os*******************@wagner.videotron.net>,
"duramax" <no@email.com> wrote:
Hi,
I'm getting some weird results for the following code:

#include "stdafx.h"
#include <stdlib.h>
typedef struct ALLO
{
char a[3];
char b[4];
} Allo;

int _tmain(int argc, _TCHAR* argv[])
{
Allo al;
strcpy(al.a, "aloe");
strcpy(al.b, "vcxzv");
return 0;
}

I get an error "stack around arround the 'al' variable was corrupted". I
think that strcpy in this case isn't good but I can't realy tell... If it is
how could I set my char in my structure else than with strcpy?


strcpy works. Given that fact where do you think the problem might be?
Let's look at what strcpy does:

char* strcpy( char* out, const char* in ) {
char* result = out;
while ( *in ) {
*out++ = *in++
}
*out = 0;
return result;
}

The block of memory pointed to by 'out' is 3 bytes, while the block of
memory pointed to by 'in' is 5 bytes. See the problem yet?
Jul 22 '05 #2

P: n/a
"duramax" <no@email.com> wrote in message
news:os*******************@wagner.videotron.net
Hi,
I'm getting some weird results for the following code:

#include "stdafx.h"
#include <stdlib.h>
typedef struct ALLO
{
char a[3];
char b[4];
} Allo;
This typedef is necessary in C but not in C++. You can just make it

struct Allo
{
char a[3];
char b[4];
};

int _tmain(int argc, _TCHAR* argv[])
{
Allo al;
strcpy(al.a, "aloe");
strcpy(al.b, "vcxzv");
return 0;
}

I get an error "stack around arround the 'al' variable was
corrupted". I think that strcpy in this case isn't good but I can't
realy tell... If it is how could I set my char in my structure else
than with strcpy?

"aloe" has 4 characters plus a terminating nul character. Thus you need 5
characters to store it. al.a only has three. The problem with the second
strcpy is similar.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead) TIA, Max.

Jul 22 '05 #3

P: n/a

"aloe"

char ape[5] = { 'a', 'l', 'o', 'e', '\0' };
"vcxzv"

char monkey[6] = { 'v', 'c', 'x', 'z', 'v', '\0' };
-JKop

Jul 22 '05 #4

P: n/a
duramax wrote:
Hi,
I'm getting some weird results for the following code:

#include "stdafx.h"
#include <stdlib.h>
typedef struct ALLO
{
char a[3];
char b[4];
} Allo;

int _tmain(int argc, _TCHAR* argv[])
{
Allo al;
strcpy(al.a, "aloe");
strcpy(al.b, "vcxzv");
return 0;
}

I get an error "stack around arround the 'al' variable was corrupted". I
think that strcpy in this case isn't good but I can't realy tell... If it is
how could I set my char in my structure else than with strcpy?


strcpy uses "C" strings where a string as a "nul" terminating character.

Hence, "aloe" is actually a string of length 5 !

i.e.
#include <iostream>
int main()
{
std::cout << sizeof( "aloe" );
}

will print 5

However you have allocated only 3 char for ALLO::a and 4 char's for
ALLO::b yet are trying to copy 5 and 6 char arrays into them !

Try this instead
int main()
{

::memcpy( al.a, "aloe", sizeof( al.a ) );
::memcpy( al.b, "vcxzv", sizeof( al.b ) );
}

But now you have the problem of possibly trying to copy more data than
there is - e.g.

::memcpy( al.b, "v", sizeof( al.b ) );

In the memcpy above, unpredictable things *may* happen because you may
be accessing memory that is unallocated (bytes after the "v" may or may
not be valid memory).

BTW - the _TCHAR and _tmain stuff is non-standard and I'd steer away
from it.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.