0

Suppose I have

void * space= malloc(500) //alloc a block of memory

and I need to write two strings and an int: "Hello world", "Goodbye friend",5 in memory address 50,150,380, correspondance to space.I tried the approach:

int * insert = (int *)(space +380);
*insert = 5;

char * insert2 = (char *)(space+50);
*insert2 = "Hello world";

char * insert3 = (char *)(space + 150);
strcpy(insert3,"Goodbye friend");

the int has no problem, but the two string throws error messages. so what's the correct method to do this? Also, how can you check if there is an overlap in memory between those inputs (the string inputed can be arbitarily long?

user2304942
  • 41
  • 2
  • 6

2 Answers2

0

Firstly, using strcpy i could make your code compile.

void * space= malloc(500); //alloc a block of memory
int * insert = (int *)(space +380);
*insert = 5;

char * insert2 = (char *)(space+50);
strcpy(insert2 , "Hello world");

char * insert3 = (char *)(space + 150);
strcpy(insert3,"Goodbye friend");

I would use strlen to get the length of the string and then sizeof(char). To get how much to allocate:

malloc(sizeof(int)+sizeof(char)*(1+strlen(string1))); // 1+ since you need to count the \0. 

http://www.cplusplus.com/reference/cstring/strlen/

CodeTower
  • 6,293
  • 5
  • 30
  • 54
0

Looking at this, I'm pretty sure that you trying to store "H" (which is a constant in C) that is of type int into insert2. In other words, your trying to store a type int (sizeof("H") == 2 on my compiler) into a char (sizeof(char) == 1). From there, it is up to your compiler to allow this behavior or throw an error. Plus "Hello world" is an immutable string that is located only in ROM, so you can't directly modified the string anyways.

To demonstrate, I ran:

printf("val:%d\n", "H");
printf("val:%d\n", "He");
printf("val:%d\n", "He");\\note 2nd instance the same
printf("val:%d\n", "Hel");
printf("val:%d\n", "Hell");
printf("val:%d\n", "Hello");
printf("val:%d\n", "Hello ");
printf("val:%d\n", "Hello W");
printf("val:%d\n", "Hello Wo");
printf("val:%d\n", "Hello Wor");
printf("val:%d\n", "Hello Worl");
printf("val:%d\n", "Hello World");

Yields:

val:4196404
val:4196406
val:4196406\\note 2nd instance the same
val:4196409
val:4196413
val:4196418
val:4196424
val:4196431
val:4196439
val:4196448
val:4196458
val:4196469

As @Rasmus was saying:

char * insert2 = (char *)(space+50);
strcpy(insert2 , "Hello world\0");// the '\0' if changing strings

Should resolve the issue.


BUT, I do not see much practical use for this, besides being vague/indecipherable, and not to mention wasteful of memory (unless you calculate it all out), so I recommend just using a struct:

struct blockOfMem{
    int num;
    char* str1;
    char* str2;
};
....
struct blockOfMem space;
space.num = 5;
strcpy(space.str1, "Hello world\0");// the '\0' if changing strings
strcpy(space.str2, "Goodbye friend\0");// the '\0' if changing strings

This makes you code much more readable and practical to use.

Community
  • 1
  • 1
SGM1
  • 968
  • 2
  • 12
  • 23