LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2017, 10:00 AM   #1
Linusovic
LQ Newbie
 
Registered: Apr 2016
Posts: 11

Rep: Reputation: Disabled
Strings in gtk+ (c)


Hello!

I'm currently having a trouble with strings and allocation of memory ( i think ).

float effect = calc_power_r(); //calc_power is my own function.
char StartText[] = "Effekt: ";
char* effectString = (char *)malloc(sizeof(float));
char* effectLabel = (char *)malloc(sizeof(effectString)+sizeof(StartText));
snprintf(effectLabel, strlen(effectString), "%f", effect);
g_stpcpy(effectLabel, StartText);
strcat(effectLabel, effectString);
gtk_label_set_text(GTK_LABEL(test->EffectResultLabel),effectLabel);
free(effectLabel);
free(effectString);

I've noticed if i change the allocation of effectString to a static string like
char effectString[50];
and
snprintf(effectLabel, 50, "%f", effect);

it works like a charm. So i suppose I'm doing something wrong with the allocation of the string or this line.
snprintf(effectLabel, strlen(effectString), "%f", effect);

//Best Regards, Linusovic
 
Old 06-24-2017, 11:06 AM   #2
af7567
Member
 
Registered: Nov 2012
Posts: 293

Rep: Reputation: 106Reputation: 106
I think it is because of the effectString allocation too. effectString is a string made of chars so instead of sizeof(float) which would just return the size of the float datatype you need to allocate enough memory for the number of characters that will be used when the float is converted to a string.

Something like this would allocate enough memory for a 50 char string, similar to what you did with "char effectString[50];"
Code:
char* effectString = (char *)malloc(50 * sizeof(char));
I don't know how long your "effect" number is going to be, but I guess less than 50 characters long (including the . ). Not sure how to work out the length in characters of the number though so you might just have to allocate enough for whatever the maximum length of a float can be.
 
Old 06-24-2017, 07:51 PM   #3
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
I think af7567 hit it. That's the first thing I noticed too.

If you want to store a string representation of a float, you don't do something like:

Code:
char *myFloatString = (char *)malloc(sizeof(float)); // WRONG!
If you want a string representation of a float, you'd do something like this:

Code:
#include <stdio.h>
#include <math.h>

int main (int argc, char *argv[]) {
    char myFloatString[10];
    sprintf (myFloatString, "%f", M_PI);
    printf ("%s\n", myFloatString);
    return 0;
}
Output:

Code:
$ ./floattest
3.141593
If you want more digits, you need to use a double or double double and allocate more characters.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
replace strings in a file using strings from another file xpto09 Linux - Newbie 3 01-28-2016 06:11 PM
BASH: replace strings in on file by the strings in another one cristalp Programming 5 10-28-2011 09:47 AM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
Where are UCS Unicode strings for GTK? donnied Linux - Desktop 0 08-11-2008 10:19 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration