[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

How to read a string from memory in C?

hello group,

I've two pointers which one points to beginning of a string in memory and also a 2nd pointer pointing to the end of it. Of course, using while() loop I can read it but how can I store it into a dynamic array or variable?

thanks,
ak
0
akohan
Asked:
akohan
3 Solutions
 
akohanAuthor Commented:


Ok I have done it this way but when I'm trying to close it by contatenating '\0' it causes segmentation error. Any idea?



range = end_addr-start_addr
char* p = malloc(range);
 
strncpy(p, res1, range);
strcat(p, '\0');        /* this causes segmentation error !*/

Open in new window

0
 
peetmCommented:
Well, depeding upon the difference between the addresses, you could store it in a a piece of memory that's the sizeof whatever the pointers point to [type], do some subtraction, ans then love he result?
0
 
akohanAuthor Commented:

Ok, for this issue I also did as following in snippet. Any idea if this is a safe way?

Thanks.


char* p = '\0';
p = ((char*) malloc(range));
 
strncpy(p, start_addr, range);
 
//now the string is pointed by p.

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
peetmCommented:
>>char* p = '\0';
p = ((char*) malloc(range));
 
strncpy(p, start_addr, range);
 
//now the string is pointed by p.

---

The cast on p = ((char*) malloc(range)); isn't necessary

And you should check that p, after the alloc isnt NULL

Whats range?

If all of the above works, you might check that theres the not the classic off by one  a Google test.


0
 
akohanAuthor Commented:


Hi thanks for the heads up. One thing that your last line is mixed with some unicode characters and had made it hard to read. Can you please explain what it means?

regarding your question, range is the difference between starting and ending memory space string lies in.

Regards,
ak
0
 
sunnycoderCommented:
>char* p = '\0';
>p = ((char*) malloc(range));
 >strncpy(p, start_addr, range);

If your string was "ab" - what would be your range? If your end_ptr was pointing to b then it would be 1 ... If it was pointing to \0 following b then it would be 2. You need 3 chars to store this string so you may have to add 2 or 1 to your range depending on where end_ptr points

>strcat(p, '\0');
second argument of strcat has to be char * or const char * ... you are passing a char - a low value that is being treated by strcat is an address - hence the segmentation fault.

You would rather do something like *(p+n) = '\0';
0
 
Infinity08Commented:
>> strncpy(p, res1, range);
>> strcat(p, '\0');        /* this causes segmentation error !*/

Note also that you probably didn't copy a trainling '\0' character with the strncpy, so you cannot use strcat after that, since it depends on the '\0' already being there.

Second :

>> char* p = malloc(range);

You only allocated memory for 'range' characters - you need one more for the trailing '\0' character.
0
 
akohanAuthor Commented:

Thank you all for your advice.

Regards.
0

Featured Post

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now